how should i change the background color of tab in android TabActivity
this is my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
final TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.getTabWidget().setBackgroundColor(Color.BLUE);
tabHost.setCurrentTab(0);
}
`
If I understand you correctly and you want to change the color of the tabhost, not the view that loads when you tap a tab, you can do it like this
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.RED);
Related
I want to get rid of the default indicator to achieve the same result when a tab is selected (no indicator). I've tried:
TabHost.TabSpec specs = tabHost.newTabSpec("").setIndicator("").setContent(intent);
TabHost.TabSpec specs = tabHost.newTabSpec("").setIndicator("",null).setContent(intent);
But none of this worked. How can I remove that indicator?
Thanks for your time.
TabHost tabHost = getTabHost();
TabSpec spec;
Intent intent;
//Home Tab
View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.home, null);
intent = new Intent(MainActivity.this, Firstclass.class);
spec = tabHost.newTabSpec("HOME").setIndicator(view1)
.setContent(intent);
tabHost.addTab(spec);
In this way you can get rid of it...
Just delete his, try this:
TabHost.TabSpec specs = tabHost.newTabSpec("").setContent(intent);
TabHost.TabSpec specs = tabHost.newTabSpec("").setContent(intent);
private void addTab(String labelId, int drawableId, Class<?> c) {
Intent intent = new Intent(this, c);
tabHost = getTabHost();
TabHost.TabSpec spec = tabHost.newTabSpec(labelId);
icon.setImageResource(drawableId);
spec.setContent(intent);
tabHost.addTab(spec);
}
private static void addTab(TabMainActivity activity, TabHost tabHost,TabHost.TabSpec tabSpec, TabInfo tabInfo)
{
Drawable indicator = mContext .getResources().getDrawable( R.drawable.red_box );
tabSpec.setIndicator(tag,indicator);
tabHost.addTab(tabSpec);
}
I have a support action bar that works fine if I trigger the event with an intent manually. But if I leave it up to the tabhost to call it then the actionbar returned from getSupportActionbar() is null.
I've heard this referenced on Stack in another question, but no one has supplied and answer. (Apparently it only occurs on Android 3 and above). Does anyone have any ideas?
My tabhost:
public class NavTab extends TabActivity {
TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Initialize a TabSpec for each tab and add it to the TabHost
intent = new Intent().setClass(this, SummaryPage.class);
spec = tabHost.newTabSpec("Summary");
spec.setIndicator("Account", getResources().getDrawable(R.drawable.tab_icon_summary));
spec.setContent(intent);
tabHost.addTab(spec);
//Feedback
intent = new Intent().setClass(this, FeedbackPage.class);
spec = tabHost.newTabSpec("Feedback");
spec.setIndicator("Feedback", getResources().getDrawable(R.drawable.tab_icon_summary));
spec.setContent(intent);
tabHost.addTab(spec);
//Payment Locations
intent = new Intent().setClass(this, PaymentLocationsActivity.class);
spec = tabHost.newTabSpec("Payment Locations");
spec.setIndicator("Pay Loc", getResources().getDrawable(R.drawable.tab_icon_summary));
spec.setContent(intent);
tabHost.addTab(spec);
//Usage Alert
intent = new Intent().setClass(this, UsageAlertPage.class);
spec = tabHost.newTabSpec("Usage Alerts");
spec.setIndicator("Alerts", getResources().getDrawable(R.drawable.tab_icon_summary));
spec.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
}
}
My activity
public class PageWithActionBar extends SherlockActivity implements ActionBar.OnNavigationListener {
private static String TAG = "mymeter-Main";
private List<Account> accounts = new LinkedList<Account>();
private LocationAdapter locationAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setDisplayShowTitleEnabled(false);
accounts.add(new Account("123456789", "4-15 Rose Rd", "Auckland 1021"));
accounts.add(new Account("0987654321", "49 Ronaki Rd", "Auckland 1043"));
locationAdapter = new LocationAdapter(this, accounts);
actionBar.setListNavigationCallbacks(locationAdapter, this);
}
}
Your TabActivity needs to extend SherlockActivity, check out the ActionBarSherlock samples on Tabs
TabNavigation
FragmentTabs
I hope you can help, I can't seem to get the current tab to reload if a tab is currently selected and then a user click on it again, could someone point out what I'm doing and perhaps modify my code to show me what I'm doing wrong as I have viewed a lot of threads on here and via google but now one seems to know the answer, that or I'm just dumb :D thank you :)
public class HelloTabWidget extends TabActivity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
final TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
// Log.d(debugTag, "onTabChanged: tab number=" + mTabHost.getCurrentTab());
switch (tabHost.getCurrentTab()) {
case 0:
//do what you want when tab 0 is selected
test();
break;
case 1:
//do what you want when tab 1 is selected
break;
default:
break;
}
}
});
}
public void test (){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Warning");
alert.setMessage("You are about to self-destruct!");
alert.show();
}
First create one variable in application class like below:
public class GlobalClass extends Application {
public int displayTab = 0;
public int getDisplayTab() {
return displayTab;
}
public void setDisplayTab(int displayTab) {
this.displayTab = displayTab;
}
}
and modify your Activity as below:
public class HelloTabWidget extends TabActivity implements OnClickListener {
GlobalClass globel;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
globel = (GlobalClass) getApplication();
Resources res = getResources(); // Resource object to get Drawables
final TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(globel.getDisplayTab());
//Here Handling first tab click
getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
test();
Log.d("Clickedddddddddddd", "1"
+ getTabHost().getCurrentTabTag());
HelloTabWidget.this.finish();
globel.setDisplayTab(0);
Intent i = new Intent(HelloTabWidget.this, HelloTabWidget.class);
startActivity(i);
}
});
}
public void test (){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Warning");
alert.setMessage("You are about to self-destruct!");
alert.show();
}
}
I have an application with 3 tab (tab1, tab2, tab3) How can I move to the tab2 if a press a bottom in tab1.
I have this....
Resources res= getResources();
TabHost tabHost= getTabHost();
TabHost.TabSpec spec;
Intent intent;
// initialize a TabSpect for each tab and add it to the TabHost
intent= new Intent().setClass(this, StockMarket.class);
spec= tabHost.newTabSpec("1").setIndicator("Stock Market", res.getDrawable(R.drawable.grafica))
.setContent(intent);
tabHost.addTab(spec);
intent= new Intent().setClass(this, Info.class);
spec= tabHost.newTabSpec("2").setIndicator("Data", res.getDrawable(R.drawable.puzzle)).setContent(intent);
tabHost.addTab(spec);
intent= new Intent().setClass(this, Profile.class);
spec= tabHost.newTabSpec("3").setIndicator("Profile", res.getDrawable(R.drawable.toolbox)).setContent(intent);
tabHost.addTab(spec);
intent= new Intent().setClass(this, Graphic.class);
spec= tabHost.newTabSpec("Graphic").setIndicator("Graphic", res.getDrawable(R.drawable.chart)).setContent(intent);
tabHost.addTab(spec);
thanks
Try this, It was worked for me.
set a method to my main class, which extends TabActivity let's call it "MainActivity"
public TabHost getMyTabHost() {
return tabHost;
}
Then add my tab activity class;
MainActivity ta = (MainActivity) this.getParent();
TabHost t = ta.getMyTabHost();
t.setCurrentTab(0);
you can set tabs with int in setCurrentTab(0).
This might help you -
getTabHost().setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
int i = getTabHost().getCurrentTab();
Log.i("TAB NUMBER", + i);
}
});
I have created a tab view. What i want is that when i switch between 2 views of my tab a progress bar is generated. How can i do so.....
Here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
TabWidget tabs = new TabWidget(this);
tabs.setId(android.R.id.tabhost);
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ImageExercise.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("images").setIndicator("Images",
res.getDrawable(R.drawable.imageicon))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AudioPlay.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.audioicon))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, VideoActivity.class);
spec = tabHost.newTabSpec("videos").setIndicator("Videos",
res.getDrawable(R.drawable.videoicon))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}