How to customize the tab-widget in Fragment Activity? - android

I am using Fragment Activity to customize the tab-widget, before I enter into Fragment Activity I use TabActivity, now it is deprecated. So I am going to update it to Fragment Activity, but I don't know how to customize the tab bar and adding the content of each tab. Can any one know help me to solve the issue.
Java Coding
private void setTabs()
{
addTab("tab1", R.drawable.tab1, tab1.class);
addTab("tab2", R.drawable.tab2, tab2.class);
addTab("tab3", R.drawable.tab3, tab3.class);
addTab("tab4", R.drawable.tab4, tab4.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
FragmentTabHost tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, null, false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
tabHost.addTab(spec);
}

Edited ...
use this line to inflate the fragment tab :)
mTabHost.getTabWidget().getChildAt(0).inflate(getActivity(), R.layout.tab_layout, null);
Cheer up
It's Easy my friend: The answer lies in your Tabhot. see you part of code.
tabHost.addTab(newTab(Constants.TAG_MainActivity, R.string.hello_world, R.id.tab1));
tabHost.addTab(newTab(Constants.TAG_Search, R.string.hello_world, R.id.tab2));
tabHost.addTab(newTab(Constants.TAG_Maps_Location, R.string.hello_world, R.id.tab3));
tabHost.addTab(newTab(Constants.TAG_More_Page, R.string.hello_world, R.id.tab4));
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.footer_home_selector);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.footer_search_selector);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.footer_camera_selector);
tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.footer_more_selector);
private TabSpec newTab(String tag, int labelId, int tabContentId) {
Log.d(TAG, "buildTab(): tag=" + tag);
TabSpec tabSpec = mTabHost.newTabSpec(tag);
tabSpec.setIndicator("");
tabSpec.setContent(tabContentId);
return tabSpec;
}

To add tab, you have to use tabhost object.
After setting up your tabhost try adding new tab as :
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1",getResources().getDrawable(R.drawable.tab1)),Tab1.class, null);
This will create tab along with tab title and icon.
If you wish to set background for tab then use :
View v = mTabHost.getTabWidget().getChildAt(i);
v.setBackgroundResource(R.drawable.tab_bg);
Here , i is position of your tab.

Related

How to change tabs images ( not in pressed )?

I'm already built custom tabs only with images and now i need to change this tabs images when user switch from Arabic\English button from another activity and
here is my code to get tabs from tab activity
mainTabs = ((TabActivityMy) getParent()).getTabHost();
mainTabs.getTabWidget().setDividerDrawable(null);
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = mainTabs.newTabSpec(null);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, ((TabActivityMy) getParent()).getTabHost().getTabWidget(), false);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
mainTabs.addTab(spec);
when i do this app add new tabs i know this code is do that but i just need to change images for all current tabs
One way would be to remove all the views from tabhost first:
mainTabs = ((TabActivityMy) getParent()).getTabHost();
mainTabs.removeAllViews()
mainTabs.getTabWidget().setDividerDrawable(null);
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = mainTabs.newTabSpec(null);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, ((TabActivityMy) getParent()).getTabHost().getTabWidget(), false);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
mainTabs.addTab(spec);
Try removing allviews from TabWidget:
mainTabs.getTabWidget().removeAllViews();
There is function:
mainTabs.clearAllTabs();
This should work in your case
You can check the value of your language switch and set the image resources based on it..
for example:
if(lamguage==english)
your_imageView.setImageResource(R.drawable.your_english_image);
else
your_imageView.setImageResource(R.drawable.your_arabic_image);

How to create Tabhost layout that works for android>=froyo?

I want to create layout with TabHost and I want to create activity for each tab to do some calculation. In addition i want this to work on all android >=Froyo. I searched for the solution everywhere which was not clear and conclusive. So if anybody could help me with this it could be a great help.
Nice question really helpful for others,
Use below code to work with Tabs and to call activities by click on a particular tab:
public class TabSample extends TabActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabmain);
setTabs() ;
}
private void setTabs()
{
addTab("Tab1", R.drawable.tab1,Home.class);
addTab("Tab2", R.drawable.tab2,AboutUs.class);
addTab("Tab3", R.drawable.tab3,Services.class);
addTab("Tab4", R.drawable.tab4,Contact.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
Here is the whole implementation of TabHost which works in all versions of android. see my answer in given link
tabs and images with some devices

How to Change Label of TabHost Programmatically/Dynamically

I have a tabhost created by
this.tabHost = getTabHost();
// Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch the first Activity for the tab (to be reused)
intent = new Intent().setClass(this, FirstGroup.class);
// Initialize a TabSpec for the first tab and add it to the TabHost
spec1 = tabHost.newTabSpec("FirstGroup").setIndicator("Regionlar",
getResources().getDrawable(R.drawable.region2)) // Replace null with R.drawable.your_icon to set tab icon
.setContent(intent);
tabHost.addTab(spec1);
And I want to change label of tabhost programmatically: "Regionlar" to "newMenuTabbar". I couldn't find any example. thanks for attention.
Edit:
I want to change second tabitem's label from "Mənzərələr"=> "secondTabitem"
intent = new Intent().setClass(this, FirstGroup.class);
// Initialize a TabSpec for the first tab and add it to the TabHost
spec1 = tabHost.newTabSpec("FirstGroup").setIndicator("Regionlar",
getResources().getDrawable(R.drawable.region2)) // Replace null with R.drawable.your_icon to set tab icon
.setContent(intent);
tabHost.addTab(spec1);
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, SecondActivityGroup.class);
spec2 = tabHost.newTabSpec("SecondActivityGroup").setIndicator("Mənzərələr",
getResources().getDrawable(R.drawable.img_gallery_icon)) // Replace null with R.drawable.your_icon to set tab icon
.setContent(intent);
tabHost.addTab(spec2);
Try this:
final TextView label = (TextView) tabHost.getTabWidget().findViewById(android.R.id.title);
label .setText(YOUR NEW LABEL);
Hope it will help.
b.i 's code can only change the first tab's title.
I think this is a more straight way to get the thing done:
((TextView) mTabHost.getTabWidget().getChildTabViewAt(position)
.findViewById(android.R.id.title))
.setText(yourTitle);
where position is the position of the tab and yourTitle is the title you wish to set for the tab. If you want to change the text for the current tab, then instead of replacing position by getCurrentTab, you can just replace getTabWidget().getChildTabViewAt(position)
by getCurrentTabView()
Whoever wrote this should have defined a setTabText(int position, String text) method, otherwise who would know they have a text view id'ed android.R.id.title? Or if they already had, please enlighten me.
Try this code
public class SlideMainActivity extends TabActivity {
public static RelativeLayout headerLayout;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main_silde_tab);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.hd_header);
setTabs();
}
private void setTabs() {
addTab("FirstGroup", R.drawable.tab_home, FirstGroup.class);
addTab("Regionlar", R.drawable.tab_search, Regionlar.class);
}
private void addTab(String labelId, int drawableId, Class<?> c) {
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(
R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}

android tabhost tab and view not matching

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);
}

How to change title of a Tab in TabActivity

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.

Categories

Resources