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

iPhone Like Toolbar for Android

Recently my company has been getting a few projects where people ask us to build Android versions of their iPhone applications. As a result of these projects I needed to create a simple iPhone like Toolbar component for Android. I extended the original component so that it could also double as a more flexible Tab component. Here are the results.

Android Toolbar

Layout for default activity - main.xml

Note the last component. This points to the class below. I also use a custom attribute to indicate which tab is currently selected.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.paxmodept.demo" 
    android:orientation="vertical"
    android:background="@android:color/white"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  
	<TextView  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:layout_weight="1"
	    android:text="@string/hello" 
	    android:gravity="center"/>    
    
    <com.paxmodept.demo.Toolbar 
	    android:layout_width="fill_parent"
	    android:layout_height="70dip" 
	    app:textViewId="option1"/>
    
</LinearLayout>

Class - Toolbar.java

This is the class that controls the component. These custom components are a nice way to re-use code efficiently. Note how I inflate the navigation layout file.

public class Toolbar extends LinearLayout {

	public Toolbar(final Context context) {
		super(context);
	}
	
	public Toolbar(final Context con, AttributeSet attrs) {
		super(con,attrs);		
		setOrientation(HORIZONTAL);
		setBackgroundColor(getResources().
				getColor(android.R.color.transparent));

		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.setBackgroundColor(getResources().
				getColor(android.R.color.white));
		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();
			}
		});
	}
}

Include File - navigation.xml

This creates the navgation bar. It uses compound drawables in a TextView component to minimise the number of components needed. Note that the background and textColor attributes of each TextView component are ColourStateLists. This gives you a great deal of flexibility in terms of creating a custom look and feel.

<merge 
	xmlns:android="http://schemas.android.com/apk/res/android">

	<TextView android:id="@+id/option1"
        android:layout_height="70dip" 
        android:layout_width="80dip"	 
        android:text="Title 1" 
        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_edit" 
        android:gravity="center"
        android:layout_weight="1"/>
        
	<TextView android:id="@+id/option2"
        android:layout_height="70dip" 
        android:layout_width="80dip"	 
        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"/>

	<TextView android:id="@+id/option3"
        android:layout_height="70dip" 
        android:layout_width="80dip"	 
        android:text="Title 3" 
        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_add" 
        android:gravity="center"
        android:layout_weight="1"/>

	<TextView android:id="@+id/option4"
        android:layout_height="70dip" 
        android:layout_width="80dip"	 
        android:text="Title 4" 
        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_help" 
        android:gravity="center"
        android:layout_weight="1"/>
	                
</merge>

I am sure there are alternate ways of creating the navigation bar but this strategy worked for me.

Source code updated 11/08/2010 Please note that the source code is now more comprehensive but is slightly different from the tutorial displayed above.

Download the source code for the entire project.




~Comments~

Dano Hart declares...

I think this is amazing. This is just what I needed. But it seems that I cannot get the application to switch tabs....I'm very new at this, but I didn't know if you could help?

Date Mon, 02 Aug 2010 at 05:06:38

Jason declares...

You need a new top level Activity for each tab. I did this I prefer a separate Activity for each application feature.

Date Mon, 02 Aug 2010 at 12:50:43

Dano Hart declares...

So to duplicate a similar action like displaying text like in the demo, am I duplicating the Toolbar.java file or the main.xml? Or both? Obviously giving them different names...

Date Thu, 05 Aug 2010 at 04:43:00

Premier declares...

I've same dubt.

Can you give me sample-application that use your navigation bar?

Thanks

Date Thu, 05 Aug 2010 at 17:19:33

Dano Hart declares...

Nothing? I'm really wanting this to work....it's really good looking, and I want to get it functional to my needs.

Date Mon, 09 Aug 2010 at 03:11:35

Jason declares...

I've been on vacation. I'll update the project this evening.

Date Tue, 10 Aug 2010 at 10:38:59

Jason declares...

I've updated the project to show how the tab Activities need to be implemented.

Date Wed, 11 Aug 2010 at 10:07:10

Dano Hart declares...

That is great. Perfect. You are a java genius! Much appreciation from this android dev newbie.

Date Sat, 14 Aug 2010 at 00:53:16

~Add Comment~

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