Skip to main content


Showing posts from September, 2012

Drawables in Android - Layer drawable

Let us see how to use layer drawable. You can have two or more bitmaps on different layers to create such a drawable

Using xml:

You should use layer-list in your xml file to create layerdrawable. Here is layer.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android=""> <item> <bitmap android:src="@drawable/whiteicon" android:gravity="top|left"/> </item> <item> <bitmap android:src="@drawable/blueicon" android:gravity="top|left"/> </item> <item> <bitmap android:src="@drawable/redicon" android:gravity="top|left"/> </item> </layer-list>
We are using three different bitmaps whiteicon.png, redicon.png and blueicon.png which are present in /res/drawable/mdpi folder. All these are of different sizes and aligned to top left. This drawab…

Spinner and its settings

Spinner is quite a handy widget when you have a set of options to select from.

In the layout file, add the spinner widget with its width and height. Dropdown items of spinner can be taken from an array.

Use an array adapter to attach these values to the spinner.

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
              this, R.array.lorem, android.R.layout.simple_spinner_item);

But I faced a  problem that text color and gravity for spinner. Text color was  black and gravity was left even after setting proper values in xml file.

Now I created a theme in /res/values/styles.xml file as follows

<style parent="@android:style/Theme" name="my_theme">
 <item name="android:textColor">@android:color/white</item> 
<item name="android:gravity">center_horizontal</item> 

And then a…

Drawables in android - Part I

Have you ever gushed over beautiful , very beautiful UI of your android apps? Sorry, most of the times these are iphone apps. That does not mean you can try to create beautiful UIs in android app.

You can create a gradient background to your button of layout using xml easily. But today let us consider how it is done using code.

int colors2[] = newint[]{Color.CYAN, Color.WHITE,Color.CYAN};
GradientDrawable grDr =new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors2);

This is how the button would look.

The code is almost self explanatory. The constructor of GradientDrawable takes the orientation of gradient and array of colors as parameter. Orientation can be top to bottom, bottom to top, left to right etc, or TL_BR or TR_BL (top left to bottom right - diagonal and top right to bottom left). Unlike xml, the colors can be more than 3. …