Error inflating view on tab number 4 - android

There is a strange error in my code. I have a TabHost where I am adding a tab dynamically when a link of the WebView. I have tested this code on 4.4.2, it works fine. But on 2.3.3 and 2.3.6 and 2.1, it does not work.
I have customized the theme of the tab with a simple xml file.
All code since the tab number 4 that do not change theme look all white.
private void createTab(String url) {
Log.i(TAG, "createTab");
TabHost tabs = getTabHost();
View view = LayoutInflater.from(tabs.getContext()).inflate(
layouts[layoutTab], null);
layoutTab = (layoutTab + 1) % 4;
TabSpec spec = tabs.newTabSpec(Integer.toString(mTabs.getCounter()));
spec.setContent(new MyTabContentFactory(url));
spec.setIndicator(view);
SingleTab st = new SingleTab(tabs.getCurrentTab(), url, spec);
Log.i(DEBUG, st.toString());
mTabs.getList().add(st);
tabs.addTab(spec);
if (tabs.getTabWidget().getChildCount() < 2)
tabs.getTabWidget().getChildAt(0).setVisibility(View.GONE);
else
tabs.getTabWidget().getChildAt(0).setVisibility(View.VISIBLE);
tabs.setCurrentTab(mTabs.getCounter());
mTabs.incrementCounter();
}

Tab implementation in 2.x is buggy and I had difficulty refresing tab after removing a tab from tabhost. There is a fragment tab host implementation in support library which might suit the need.

Related

Change Tab color depending on which tab is selected

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

Opening OptionsMenu on the touch of a tab of the TabActivity

I have a tabActivity where I am adding tabs on runtime. So I think this is the usual code to do that:
_tabSpec = TabHost.newTabSpec("More");
_tabSpec.setIndicator("", Resources.GetDrawable(Resources.Drawable.myIcon).SetContent(intent);
TabHost.AddTab(_tabSpec);
Now the thing is, I have defined an options menu and I want to pop that up when the user clicks on the 'More' tab. I don't know how to do that. I tried not setting a content on that tab and simply use the OpenOptionsMenu() to pop it, but it doesn't seem to work.
Any clue how to achieve that?
P.S.: This is a C# code written in Xamarin. It might not look like the native java-android code, but its almost the same.
Alright, here's an answer to what I was trying to do.
Motive: Add a 'More' tab to the existing TabActivity. When user clicks it, open some kind of a PopUpWindow or ContextMenu, etc.
Steps to do that:
1) Create a tabSpec and add that tab to the TabHost as shown in the question.
2) Now you need to take in this last added tab as a View type variable. Do this by..
View v = TabWidget.GetChildAt(index)
Remember index of tabs starts from 0
3) Now on the onCreate() method of your main activity (one that holds the TabActivity) add an onTouchListener() (I use C#, so I added v.Click+=myFunction()) and write your code of the PopupWindow or ContextMenu or whatever you want to do there.
private String lastTab = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TabHost tabHost = getTabHost();
TabHost.TabSpec tabSpec;
tabSpec = tabHost.newTabSpec("tab1");
tabSpec.setIndicator("Tab 1");
tabSpec.setContent(new Intent(this, OneActivity.class));
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("tab2");
tabSpec.setIndicator("Tab 2");
tabSpec.setContent(new Intent(this, TwoActivity.class));
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("more");
tabSpec.setIndicator("More");
tabSpec.setContent(new Intent(this, OneActivity.class));
tabHost.addTab(tabSpec);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
if (tabId.equalsIgnoreCase("more")){
openOptionsMenu();
tabHost.setCurrentTabByTag(lastTab);
}
else lastTab = tabId;
}
});
}

How to set up an ActionBar Sherlock Tab?

I'm switching my App's Tab (API 7) to the one's used in the Action Bar Sherlock because of the design, but I don't know how to set this up.
That's how I used to do:
tabH = (TabHost) findViewById(R.id.tabhost);
tabH.setup();
TabSpec espec = tabH.newTabSpec("tabONE");
espec.setContent(R.id.tbhot);
espec.setIndicator("A");
tabH.addTab(espec);
espec = tabH.newTabSpec("tabTWO");
espec.setContent(R.id.tbrecente);
espec.setIndicator("B");
tabH.addTab(espec);
espec = tabH.newTabSpec("tabTHREE");
espec.setContent(R.id.tbcreate);
espec.setIndicator("C");
tabH.addTab(espec);
And now that's what I'm doing:
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 1; i <= 3; i++) {
ActionBar.Tab tab = getSupportActionBar().newTab();
if (i == 1)
tab.setText("A");
else if(i == 2)
tab.setText("B");
else if (i == 3)
tab.setText("C");
tab.setTabListener(this);
getSupportActionBar().addTab(tab);
}
That works, but I don't know how to set the Content, so all tabs have the same thing in it. How do I do it?
Also, my other tab was at the bottom of the layout. Is it possible to set this one at the bottom too? I believe that if I could set the TabHost in the new tab, it will be in the bottom too, so the question is, how to set the tabhost here?
I typically use fragments for the content. You have to implement the ActionBar.TabListener and adjust your content there, but it's pretty easy. You can add all your fragments at the beginning and show/hide them, or replace the current fragment.

Refreshing Android Tabs without Intent

Please how can I refresh the content of Tab 2 (tag 2,Live blog) without adding intent.
TabHost th = (TabHost)findViewById(R.id.tabhost1);
th.setup();
TabHost.TabSpec spec = th.newTabSpec("tag1")
.setIndicator("Testimonies", getResources()
.getDrawable(R.drawable.icon_testimonies_tab))
.setContent(R.id.tab1);
th.addTab(spec);
spec = th.newTabSpec("tag2")
.setIndicator("Live Blog", getResources()
.getDrawable(R.drawable.icon_liveblog_tab))
.setContent(R.id.tab2);
th.addTab(spec);
spec = th.newTabSpec("tag3")
.setIndicator("Streaming Settings", getResources()
.getDrawable(R.drawable.icon_streamingsetting_tab))
.setContent(R.id.tab3);
th.addTab(spec);
th.setCurrentTab(1);
th.getCurrentView().setVisibility(View.VISIBLE);
We "see" each other again, #meduteniopeyemi ! I worked hard trying to solve the issue you are talking about. And the aproach that I used to do it was:
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
//YOUR CODE checking if "tabId" is the tab you want to REFRESH
}
});
It worked so far for me. It just allows you to do any refreshing without problems.

TabHost: One tab of Activity and second of a View

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.

Categories

Resources