Jason Delport Home

Blog Feed Blog Feed

Paxmodept Website Paxmodept Website

Contact Information


Jason Delport

+44(0)7931445721

jason@paxmodept.com

Download vCard


Mobile Portal


~mobile.paxmodept.com~
QR Code

Blog Archive & Stats


MonthPosts
June 2010(2)
May 2010(1)
April 2010(4)
February 2010(1)
January 2010(2)
December 2009(1)
November 2009(3)
October 2009(3)
September 2009(7)
August 2009(1)
July 2009(2)
June 2009(4)
May 2009(7)
April 2009(5)
March 2009(10)
February 2009(10)
January 2009(19)
December 2008(11)
November 2008(16)
October 2008(28)
September 2008(7)
August 2008(19)
July 2008(17)
June 2008(13)
May 2008(11)
April 2008(11)
March 2008(18)
February 2008(17)
January 2008(19)
December 2007(8)
November 2007(29)
October 2007(38)
September 2007(30)
August 2007(50)
July 2007(46)
June 2007(38)
May 2007(20)
April 2007(16)
March 2007(35)
February 2007(28)
January 2007(36)
December 2006(26)
November 2006(42)
October 2006(39)
September 2006(26)
August 2006(16)
July 2006(4)
Total796


© 2010 Jason Delport

Using XML Resources in Android

There are a lot of different ways to give UI components a custom look and feel in android. You can use bespoke raster images, you can use NinePatch images or you can use Android XML resources. Android XML resources can be vector based graphical components (the Android SPOD is created this way) or colour/drawable state lists (ColorStateLists and StateListDrawables). You can combine these XML resources together to create some really interesting user interfaces.

As an example of some of these techniques I'm going to extend the UI I originally created here (on the left of the image below) to become something slightly more interesting (on the right of the image below).

Android XML Drawables

Stage One

Modify the StateListDrawable of each tab background (in the background_states.xml file) to point to a XML drawable instead of a colour.

 
<?xml version="1.0" encoding="utf-8"?> 
<selector 
	xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
    	android:state_focused="true" 
    	android:drawable="@drawable/navigation_focussed" />    	
    <item 
    	android:state_pressed="true" 
    	android:drawable="@drawable/navigation_pressed" /> 
    <item 
    	android:drawable="@drawable/navigation_off" /> 
</selector>

Stage Two

Create the XML drawable files in the res/drawable folder. The below file is called navigation_on.xml. It creates a white rectangle shape with curved top corners. You some reason you can't set the corner radius values to 0 so I had to use 0.1.

 
<?xml version="1.0" encoding="utf-8"?>
<shape 
	xmlns:android="http://schemas.android.com/apk/res/android" 
	android:shape="rectangle">
    <stroke 
    	android:width="3dip" 
    	android:color="@android:color/white" />
    <solid 
    	android:color="@android:color/white"/> 
    <corners 
	android:bottomRightRadius="0.1dip" 
	android:bottomLeftRadius="0.1dip" 
	android:topLeftRadius="15dip" 
	android:topRightRadius="15dip" />
</shape>

Stage Three

Add some space around each TextView component (tab) in the navigation.xml file via the layout_margin parameter.

 
<TextView android:id="@+id/option2"
        android:layout_height="70dip" 
        android:layout_width="80dip"	 
        android:layout_margin="4dip"
        android:text="Title 2" 
        android:textSize="10dip" 
	android:textStyle="bold" 
	android:textColor="@drawable/text_states" 
        android:clickable="true"
        android:focusable="true" 
        android:background="@drawable/backgound_states" 
        android:drawableTop="@android:drawable/ic_menu_zoom" 
        android:gravity="center" 
        android:layout_weight="1"/>
        

Stage Four

Create another XML resource file called navigation_bg.xml. This file takes a PNG file (navigation_bg_img.png) and tiles it across the entire background area. (The stars background in the top image).

 
<?xml version="1.0" encoding="utf-8"?> 
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/navigation_bg_img"
    android:tileMode="repeat"
    android:dither="true" />
    

Stage Five

Edit the Toolbar.java file. Set the background of the custom component to the newly created navigation_bg.xml file. Set the background drawable of the selected tab to the navigation_on.xml file defined above.

 
public class Toolbar extends LinearLayout {

	public Toolbar(final Context context) {
		super(context);
	}
	
	public Toolbar(final Context con, AttributeSet attrs) {
		super(con,attrs);		
		setOrientation(HORIZONTAL);
		setBackgroundDrawable(getResources().
			getDrawable(R.drawable.navigation_bg));

		LayoutInflater inflater = (LayoutInflater) 
		con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.navigation, this);
		
		TypedArray a = con.obtainStyledAttributes(attrs, 
			R.styleable.Toolbar);
		String option = a.getString(R.styleable.Toolbar_textViewId);
		
		String resourceId = "com.paxmodept.demo:id/"+option;
		int optionId = getResources().getIdentifier(resourceId,null,null);        		
		TextView currentOption = (TextView) findViewById(optionId);
		currentOption.setBackgroundDrawable(getResources().
			getDrawable(R.drawable.navigation_on));
		currentOption.setTextColor(getResources().
			getColor(android.R.color.black));
		currentOption.requestFocus(optionId);
		currentOption.setFocusable(false);
		currentOption.setClickable(false);
		
		TextView option1 = (TextView) findViewById(R.id.option1);
		option1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				CharSequence txt = "Hello!";
				int len = Toast.LENGTH_SHORT;
				Toast toast = Toast.makeText(con, txt, len);
				toast.show();
			}
		});
	}
}

That's it. Not very pretty but functional. One problem with using XML resources is that the official documentation is seriously sparse however someone has created the "Missing Manual on Drawables XML in Android".




~Add Comment~

Name
Email
Website
Website
Website
Comment (No HTML)
Human? Human?