android, Tutorial 029-2, UI, Tab at the bottom of the screen


Adding tabs in android and placing them at the bottom of the screen

I was looking for a way to launch activities using android tabs that could be customized and placed at the bottom of the screen. After a lot of googling I could not find a workable solution to do so purely from code without using a layout file, so, after a bit of trial and error I was able to use TabActivity class for spawning activities and customize the existing TabHost structure which by default came with a “LinearLayout” element embedded while I required a “RelativeLayout” element within TabHost.
<a href="http://www.powenko.com/en/wp-content/uploads/2011/08/device-2011-08-14-213921.png"><img class="alignnone size-full wp-image-249" title="device-2011-08-14-213921" src="http://www.powenko.com/en/wp-content/uploads/2011/08/device-2011-08-14-213921.png" alt="" width="228" height="403" /></a>
package com.powenko.Tutorial_UI_Tab_Buttom_TabActivity;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TabHost.OnTabChangeListener;

public class Tutorial_UI_Tab_Buttom_TabActivityActivity extends TabActivity {
    /** Called when the activity is first created. */
	private TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {

    	 super.onCreate(savedInstanceState);

         Resources res = getResources(); // Resource object to get Drawables
         tabHost = getTabHost();  // The activity TabHost
         TabHost.TabSpec spec;  // Resusable TabSpec for each tab
         Intent intent;  // Reusable Intent for each tab
         tabHost.removeAllViews();
         TabWidget tabs = new TabWidget(this);
         tabs.setId(android.R.id.tabs);
         RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
         params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
         tabs.setLayoutParams(params);
         FrameLayout content = new FrameLayout(this);
         content.setId(android.R.id.tabcontent);
         content.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
         RelativeLayout relative = new RelativeLayout(this);
         relative.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
         relative.addView(content);
         relative.addView(tabs);
         tabHost.addView(relative);
         tabHost.setup();

         // Create an Intent to launch an Activity for the tab (to be reused)

     //   tabHost = getTabHost();
      //  Intent mtab1 = new Intent(this,info.class);      s
        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("tab1", getResources().getDrawable(R.drawable.icon ))
                .setContent(new Intent(this, info.class)));

        tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator("tab2", getResources().getDrawable(R.drawable.icon))
                .setContent(new Intent(this, info.class)));

        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("tab3", getResources().getDrawable(R.drawable.icon))
                .setContent(new Intent(this, info.class)));

      tabHost.setCurrentTab(1);
      tabHost.setOnTabChangedListener(new OnTabChangeListener() {
  //        @Override
      public void onTabChanged(String arg0) {
        	  if (arg0.equals("tab3"))
        	  {
        		  tabHost.refreshDrawableState();
        	  }
      }    

     });
    }
}

<h3>info.java</h3>

package com.powenko.Tutorial_UI_Tab_Buttom_TabActivity;

import android.app.Activity;
import android.os.Bundle;

public class info extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

}

<h3>AndroidManifest.xml</h3>

<!--?xml version="1.0" encoding="utf-8"?-->

<h3>sample code:</h3>
please download from <a href="http://code.google.com/p/powenko-android-tutorial/">here</a>, “
Tutorial_UI_Tab_Buttom_TabActivityActivity

” ,
http://code.google.com/p/powenko-android-tutorial/

Leave a Reply