I want to design a page in which i just want to show only one tab. But when i add only one tab, it takes the full width available but i want that tab only takes small space and remaining space is left empty. No other tabs i want to add. Only one tab is displayed all time. Code is following
public class TabViewActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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, FirstTab.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("firstTab").setIndicator("First Tab")
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
Use tabHost.getTabWidget().getChildAt(0).getLayoutParams().width =(int) 30; also set the layout_width="wrap_content"
In your xml hide the tab widget by setting the visibility to gone. and instead use a button. And in the button handler you can use code like
public void tabHandler(View target){
artistButton.setSelected(false);
albumButton.setSelected(false);
if(target.getId() == R.id.artist_id){
tabHost.setCurrentTab(0);
artistButton.setSelected(true);
} else if(target.getId() == R.id.album_id){
tabHost.setCurrentTab(1);
albumButton.setSelected(true);
}
}
Related
I have an app with a MainActivity that extends TabActivity (i know it's deprecated but too much to do to change whole app).
So in my app I use tabhost to create 3 tabs like this:
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
TabSpec thirdTabSpec = tabHost.newTabSpec("tid3");
firstTabSpec.setIndicator("tab1").setContent(
new Intent(this, tab1.class));
secondTabSpec.setIndicator("tab2").setContent(
new Intent(this, tab2.class));
thirdTabSpec.setIndicator("tab3").setContent(
new Intent(this, tab3.class));
/* Add tabSpec to the TabHost to display. */
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
tabHost.addTab(thirdTabSpec);
//Changing the tabs text color on the tabs
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.parseColor("#ffffff"));
}
// remove divider
tabHost.getTabWidget().setDividerDrawable(null);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#90a4ae"));
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#607d8b"));
tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#607d8b"));
So my code creates the 3 tabs that link to 3 different activities and sets the color of the tabs. First tab has at first load a different color than the other two.
I want the color of the tabs to change depending on which one is selected.
So when I press the second tab i want the first to get #607d8b color and the second to get #90a4ae.
Same for the third one.
Tried to implement a OnTabChangeListener but couldn't get it to work.
Tried to to this:
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#90a4ae"));
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#607d8b"));
tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#607d8b"));
with changed colors inside each loaded tab activity but I get error that it can't resolve tabhost (as it should, since it's defined in MainActivity.
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#54C4C6")); // unselected
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#114C5A")); // selected
}
});
I have two tabs in my tabwidget and each tab display a list view.
ay
Initially my first tab is displayed as selected but below list correspond to second tab.
Once i click the tabs, i am getting correct display.
private static final String LIST_TAB_TAG1 = "UpcomingEvents";
private static final String LIST_TAB_TAG2 = "PastEvents";
tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG1)
.setIndicator(LIST_TAB_TAG1)
.setContent(new TabContentFactory() {
public View createTabContent(String arg) {
return listView1;
}
}));
tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG2)
.setIndicator(LIST_TAB_TAG2)
.setContent(new TabContentFactory() {
public View createTabContent(String arg) {
return listView2;
}
}));
tabHost.setCurrentTab(0);
LIST_TAB_TAG1 is highlighted when this sctivity is launched but the list displayed is listview2. This problem is only when activity starts. Upon clicking tabs its working fine
Can please help me in fixing this. Thanks for your time
I generally use the code given in docs which works fine for me ,try this one .
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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, UpcomingEvents.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("UpcomingEvents").setIndicator("UpcomingEvents",getResources().getDrawable(R.drawable.homebutton1)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this,PastEvents.class);
spec = tabHost.newTabSpec("PastEvents").setIndicator("PastEvents",
getResources().getDrawable(R.drawable.bank_transaction1))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
I have TabActivity with tabs on bottom which is composed of several different activities like this:
public class HomeScreen extends TabActivity {
private TabHost mTabHost;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
try{
setContentView(R.layout.hometabs);
Resources res = getResources(); // Resource object to get Drawables
mTabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
String strName;
// Create an Intent to launch an Activity for the tab (to be reused)
strName=Central.Res.getString(R.string.tab_Books);
intent = new Intent().setClass(this, BookList.class);
spec = mTabHost.newTabSpec(strName).setIndicator("",
res.getDrawable(R.drawable.ic_menu_database))
.setContent(intent);
mTabHost.addTab(spec);
// Create an Intent to launch an Activity for the tab (to be reused)
strName=Central.Res.getString(R.string.tab_Questions);
intent = new Intent().setClass(this, QuestionsList.class);
intent.putExtra("NoteType", UserDataSQLHelper.NOTE_TYPE_QUEST );
spec = mTabHost.newTabSpec(strName).setIndicator("",
res.getDrawable(R.drawable.ic_menu_dialog))
.setContent(intent);
mTabHost.addTab(spec);
My problem is that I have the same window title for every tab. This window title is set to application name.
I need to be able to change this title to the name of the tab.
I am trying to do it by calling setTitle("MyTitle"); in onCreate() of corresponding activity and by overriding TabActivity onChildTitleChanged:
public void onChildTitleChanged(Activity childActivity, CharSequence title)
{
View tabView = mTabHost.getCurrentTabView();
int idTitle = android.R.id.title;
TextView tvTitle = (TextView) tabView.findViewById(idTitle);
tvTitle.setText(title);
}
But the result I get isn't what I need - it sets text to tab under the tab icon.
Could you please help me?
try with following code,
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
Log.i("Tab id", ""+tabId);
setTitle(tabId);
}
});
setIndicator (CharSequence label, Drawable icon).
Use this function and the label will be shown as a title.
I need to set different Images as tab background on different states. I have set one image as background for default but how to switch to other one when tab is selected. Below is my code.
public class HelloTabWidget extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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 tw = getTabWidget();
for (int i = 0; i < tw.getChildCount(); i++) {
View v = tw.getChildAt(i);
v.setBackgroundDrawable(getResources().getDrawable
(R.drawable.tab_artist));
}
//First tab
intent = new Intent().setClass(this, FirstActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("First").setIndicator("First")
.setContent(intent);
tabHost.addTab(spec);
getTabHost().getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabselected);
//Second tab
intent = new Intent().setClass(this, SecondActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Second").setIndicator("Second")
.setContent(intent);
tabHost.addTab(spec);
getTabHost().getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabselected);
//third
intent = new Intent().setClass(this, ThirdActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Third").setIndicator("Third")
.setContent(intent);
tabHost.addTab(spec);
getTabHost().getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tabselected);
}
}
/*tab_artist.xml*/
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:background="#drawable/tabselected" android:state_selected="true" />
<!-- When not selected, use white-->
<item android:background="#drawable/tabunselected" />
</selector>
public class HelloTabWidget extends TabActivity implements OnTabChangeListener{
.....
mTabHost. setOnTabChangedListener(this);
#Override
public void onTabChanged(String tabId) {
// Here in tabId you will get the name of the Tab from that you can check and set the background
// of the requirement tab according to need.
}
}
Implement onTabChangeListener() and there modify their backgrounds. Cheers
http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html
Edit:
Use the tabHost to implement the method. You can implemented where ever you want. Let's say do it after you set all the TabWidgets. Its good practice to use ids of the tab like you've set them "First", "Second" etc etc.
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
if(tabId.equals("First"){
//do something
}else if(tabId.equals("Second"))
{
//do something
}// etc etc etc
}});
this may help you
Tabhost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
for(int i=0;i<tb.getTabWidget().getChildCount();i++)
{
tb.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabunselected); //unselected
}
tb.getTabWidget().getChildAt(tb.getCurrentTab()).setBackgroundResource(R.drawable.tabselected);
}});
I need little bit help related to android tabhost. I have 3 tabs and 3 activities. MainTab activity is "extends from TabActivity" and other 2 activities are "extends from Activity".
In MainTab activity there are two buttons Radio and CheckBox, when user click radio button then i want to display radio button view in tab2. when user click check box i want to display checkbox view. Can some gives me an idea how I can achieve this?? Below is my code.
public class MainTabHost extends TabActivity implements OnTabChangeListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.homeui);
TabHost tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Main.class);
spec = tabHost.newTabSpec("Main Tab").
setIndicator("Main Tab")
.setContent(intent);
spec.setIndicator("", this.getResources().getDrawable(R.drawable.maintabicon) );
tabHost.addTab(spec);
intent = new Intent().setClass(this, TabA.class);
spec = tabHost.newTabSpec("Tab A").
setIndicator("Tab A")
.setContent(intent);
spec.setIndicator("", this.getResources().getDrawable(R.drawable.tabaicon) );
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
#Override
public void onTabChanged(String tabId) {
}
}
You can use SharedPreference for this. So that when you choose radio button you can change your shared value to radio and when you use Check box change value to check box and use the view in your second tab according to the shared value.