I have prepared one application,my app contains one tab layout.Tab layout contain 6 tab.I want assign animation to each tab.For this i have used this line of code,
overridePendingTransition(R.anim.in, R.anim.out);
my tab layout code is,
TabSpec tabSpec = tabHost.newTabSpec(""+count);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.custom_tabs, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.tv_Count);
title.setText(""+count);
tabSpec.setIndicator(tabIndicator);
Intent intent = new Intent(this, Utils.formsOfAPlan.get(count));
intent.putExtra("FORM_ID", count);
tabSpec.setContent(intent);
Utils.tabColors.put(count, Constants.NOT_SUBMITTED);
tabHost.addTab(tabSpec);
please guide me to over come this.
Related
I want to add background images for Tabhost which i already added in selector xml. But not sure how to add resource while initiating Tab. Below is tab :
Code to add :
getResources().getDrawable(R.drawable.tabicon)
/* Tabs */
Bundle bundle = getIntent().getExtras();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// First Activity
intent = new Intent().setClass(this, InfoListView.class);
spec = tabHost.newTabSpec("some_things").setIndicator("Info").setContent(intent);
tabHost.addTab(spec);
// Second Activity
intent = new Intent().setClass(this, LogListView.class);
spec = tabHost.newTabSpec("top_things").setIndicator("Sync Log").setContent(intent);
tabHost.addTab(spec);
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 95;
tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 95;
/* Tabs ends */
Create a TextView , set text to that textView, set background drawable to it, and set this textview as indicator
Somewhat like this:
TextView mTv = new TextView(mContext);
mTv.setText(/*Your-text*/);
mTv.setBackgroundDrawable(mContext.getResources.getDrawable(/*id-of-your-image*/));
spec = tabHost.newTabSpec("top_things").setIndicator(mTv).setContent(intent);
tabHost.addTab(spec);
After your tabhost is intilize with indicator and other things add background to it like this
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabicon); //fro first tab
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabicon); //for second tab
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);
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);
}
}
I want to have a TabHost which consists of two tabs: one created from View (R.id.something) and the second one from Activity.
So I do that like this:
mTab = (TabHost) findViewById(R.id.tabhost);
mTab.setup();
TabHost.TabSpec spec = mTab.newTabSpec("All");
spec.setContent(R.id.all_tab); // Created from View
spec.setIndicator("All", getResources().getDrawable(R.drawable.emo_im_cool));
mTab.addTab(spec);
Intent intent = new Intent().setClass(this, TasksDone.class);
spec = mTab.newTabSpec("Done");
spec.setIndicator("Done", getResources().getDrawable(R.drawable.emo_im_happy));
spec.setContent(intent); // Created from Intent
mTab.addTab(spec);
After that the content on the first tab isn't visible, but it's there because I see reaction on my clicks.
But it appears if I set the setContent of a second tab as a View instead of intent.
Do you guys know why the content on the first page is invisible?
Mh, have you tried spec.setContent(R.layout.all)? I assume there should be a layout id, not the id of a view-object.
The root of the problem was a mistake in xml.
I am trying to create and remove tabs dynamically. Usually an activity should be set for each tab created in TabSpec. But how to do it when the tabs are created dynamically? Here I am using a frame layout to display tab content. If I try to use the same activity by setting the tab content, the text is getting overlapped. Here I have to read the text from the EditText view and set it as the tab content and that content should be shown whenever I navigate to that tab.
Try This
protected TabHost tabs;
// ...
/**
* Init tabs.
*/
private void initTabs() {
tabs = (TabHost) findViewById(R.id.tabhost);
tabs.setup();
tabs.setBackgroundResource(R.drawable.bg_midgray);
TabHost.TabSpec spec;
// Location info
txtTabInfo = new TextView(this);
txtTabInfo.setText("INFO");
txtTabInfo.setPadding(0, 0, 0, 0);
txtTabInfo.setTextSize(14);
txtTabInfo.setBackgroundResource(R.drawable.bg_tab_left_inactive_right_inactive);
txtTabInfo.setTextColor(Color.DKGRAY);
txtTabInfo.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
txtTabInfo.setHeight(39);
spec = tabs.newTabSpec("tabInfo");
spec.setContent(R.id.tabInfo);
spec.setIndicator(txtTabInfo);
tabs.addTab(spec);
// Maps
txtTabMap = new TextView(this);
txtTabMap.setText("MAP");
txtTabMap.setTextSize(14);
txtTabMap.setPadding(0, 0, 0, 0);
txtTabMap.setBackgroundResource(R.drawable.bg_tab_middle_inactive_right_active);
txtTabMap.setTextColor(Color.DKGRAY);
txtTabMap.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
txtTabMap.setHeight(39);
spec = tabs.newTabSpec("tabMap");
spec.setContent(R.id.tabMap);
spec.setIndicator(txtTabMap);
tabs.addTab(spec);
tabs.setCurrentTab(0);
tabs.setOnTabChangedListener(this);
}
// ...