Payday Loan Payday Loan

The following code will be commented so pay attention in code what does every line so
that you can reuse the code for your purpose. The code shows you how to make tabs,
and each tab to open a different activity, and have a custom icon,and a custom background,
not that default gray. Please see the code because the lines are commented and explain
where you can set a custom background for a tab on Android.

package com.tabdemo;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
 
public class Main extends TabActivity {
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Drawable mySelector = getResources().getDrawable(R.drawable.background);
Drawable mySelector1 = getResources().getDrawable(R.drawable.background);
Drawable mySelector2 = getResources().getDrawable(R.drawable.background);
Drawable mySelector3 = getResources().getDrawable(R.drawable.background);
Drawable mySelector4 = getResources().getDrawable(R.drawable.background);
 
//here we set the tab background that must be in the drawable folder
Resources res = getResources(); // Resource object to get Drawables
 
TabHost tabHost = getTabHost();
 
// The activity TabHost
TabHost.TabSpec spec;  // Resusable TabSpec for each tab
final Intent intent1;
final Intent intent2;
Intent intent3; // Reusable Intent for each tab
Intent intent4;
Intent intent5;
 
// Create an Intent to launch an Activity for the tab (to be reused)
intent1 = new Intent().setClass(this,FirstClass.class);
intent2 =new Intent().setClass(this, SecondClass.class);
intent3= new Intent().setClass(this,ThirdClass.class);
intent4= new Intent().setClass(this,Fourth.class);
intent5= new Intent().setClass(this,FifthClass.class);
 
TabWidget tab=getTabWidget();
 
spec = tabHost.newTabSpec("firstclass").setIndicator("FirstClass",
res.getDrawable(R.drawable.calculator_tab))
.setContent(intent1);//this line sets the content of the tab being intent1...
 
//intent1 is declared upper like FirstClass.java
 
//R.drawable.calculator_tab ....this is the icon used for the tab
tabHost.addTab(spec);
 
tab.getChildTabViewAt(0).setBackgroundDrawable(mySelector);
 
// here is set the background for the current tab.
 
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("secondclass").setIndicator("SecondClass",
res.getDrawable(R.drawable.procedural_tab))
.setContent(intent2);
 
tabHost.addTab(spec);
tab.getChildTabViewAt(1).setBackgroundDrawable(mySelector1);
 
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("thirdclass").setIndicator("ThirdClass",
res.getDrawable(R.drawable.icu_tab))
.setContent(intent3);
 
tabHost.addTab(spec);
tab.getChildTabViewAt(2).setBackgroundDrawable(mySelector2);
 
//here we set Background drawable that we declared at the begining of the program.
 
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("fourthclass").setIndicator("FourthClass",
res.getDrawable(R.drawable.reference_tab))
.setContent(intent4);
 
tabHost.addTab(spec);
tab.getChildTabViewAt(3).setBackgroundDrawable(mySelector3);
 
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("safety").setIndicator("Safety ",
res.getDrawable(R.drawable.safety_tab))
.setContent(intent5);
 
tabHost.addTab(spec);
tab.getChildTabViewAt(4).setBackgroundDrawable(mySelector4);
 
tabHost.setCurrentTab(0);
 
}
 
}

I hope this help you and i think this is easy to understand and implement.
Please add your comment if something goes wrong.

||||| 0 I Like It! |||||

Search

Popular

Sponsors