TabHost: One tab of Activity and second of a View - android

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.

Related

android tab host not refreshing data

I am using a Tab host two show two list on two tabs but I want that when click on second tab then it should reload the first tab data.How is it possible?
My code is like this
TabHost tabs = (TabHost)findViewById(R.id.tabhost_spices);
tabs.setup();`enter code here`
TabHost.TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setContent(R.id.list_spices_fav);
tab1.setIndicator("Favorite",getContext().getResources().getDrawable(R.drawable.tab_fav_icon));
tabs.addTab(tab1);
// create tab 2
TabHost.TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setContent(R.id.list_spices_categories);
tab2.setIndicator("View List",getContext().getResources().getDrawable(R.drawable.tab_cat_icon));
tabs.addTab(tab2);
Here is a tutorial,This Help me- link
Extends your activity with TabActivity
TabHost tabHost;
tabHost = getTabHost();
Resources res = getResources();
TabSpec tabspec1 = tabHost.newTabSpec("").setIndicator("Tab1",res.getDrawable(R.drawable.ic_launcher)).setContent(new Intent(this, FirstActivity.class));
tabHost.addTab(tabspec1);
TabSpec tabspec2 = tabHost.newTabSpec("").setIndicator("Tab2",res.getDrawable(R.drawable.ic_launcher)).setContent(new Intent(this, SecondActivity.class));
tabHost.addTab(tabspec2);
For refreshing the data of new Tab you have to keep the code for refereshing the data inside onResume() of that Activity because when TabActivity is created its all the tabs make a call to the respective Activities calling its onCreate()'. So, when you click the Tab once its loaded itsonResume()method is called and notonCreate()`.

Android, Inside a tabhost how to add a button and a listview undernetath?

I want to create something like this
|Button|
item 1
item 2
item 3
.
.
.Items on a listview
I already have a tabhost with 3 tabs, so i don't want to change my main.xml because the button will appear on every tab! i want my first tab to show a calendar (this one is done, i'm not sure if its ok but it has be done), the second tab will show something diferrent and the last one the button and the item list underneath.
I donnot paste any code because everything i've done comes from android tutorials, so i don't want to ask someone to give me an already written code, just to guide me through what do i have to read and where to look to achieve that!
Thanks in advance!
Well this is what I've done so far
this is my main class
`public class HourPayActivity 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
// Create an Intent to launch an //setContentView(R.layout.emptab); Activity for the tab (to be reused)
intent = new Intent().setClass(this, MonthsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Months").setIndicator("",
res.getDrawable(R.drawable.ic_tab_months))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, EmployersActivity.class);
spec = tabHost.newTabSpec("Employers").setIndicator("",
res.getDrawable(R.drawable.ic_tab_employers))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, PaymentsActivity.class);
spec = tabHost.newTabSpec("Payments").setIndicator("",
res.getDrawable(R.drawable.ic_tab_payments))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
`
And this is the tab content that i want to show up
public class EmployersActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView employersList = getListView();
String[] employers = getResources().getStringArray(R.array.employers_list);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, employers));
employersList.setAdapter(getListAdapter());
employersList.setTextFilterEnabled(true);
employersList.setOnItemClickListener(new OnItemClickListener() {
//#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_LONG).show();
}
});
setContentView(employersList);
}
What you do is; You follow the android tutorial, which tells you to create an class that extends TabActivity if I remember correctly.
In this activity you load intents via
TabHost.TabSpec spec = tabHost.newTabSpec([title]);
....
Intent content = new Intent(this, activityToLoad.class);
spec.setContent(content);
tabHost.addTab(spec);
What this does is, it loads an activity in a tab.
This in turn means that you can use an activity like normal, you can use custom layouts with setContentView(R.layout.yourlayout); in the onCreate() method. You can call your components, attach a custom list adapter to your list and so on. For the 3 tabs create 3 different activities. Based on personal knowledge its the easiest way to maintain your tabs.
What you put in these layout is upto you. But those activities you put in the tabs are "normal" activities like you are (probably) familiar with.
To explain your view:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btnId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/yourtext"
/>
<ListView
android:id="#android:id/android:list"
android:layout_height="fill_content"
android:layout_width="match_parent"
/>
</LinearLayout>
This will place a button at the top with a listview underneath it.
The id of the listview is android:list because in my activity i extend listActivity, which expects a listview with that id.

Custom Tab Content

I made custom tabs using this tutorial: http://joshclemm.com/blog/?p=136
I have them completely customized and looking nice, but now I don't know how to add content to the tabs. I don't even know where to start with the way this code is written, any help? Thanks.
This is the code that I think sets the content:
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {return view;}
});
mTabHost.addTab(setContent);
}
I don't know what this is doing with "TabContentFactory"
I too followed the same blog and was facing problem, while adding the Intent's. So, finally I fixed it myself. What I did is,
1.) public class CustomTabActivity extends Activity here I changed it to extend TabActivity
2.) Just add the content using Intent
Intent intent = new Intent().setClass(this, Hello.class);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
That's it and it worked. Hope this works for you too.
Try following the tutorial given on the android website. Combined with the tutorial you used, it should give you completely what you want.
Specifically, to add content to to each tab you add and activity to each tab like so:
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
Look at this:
private void addActivityTab(int labelResId, int iconResId, Intent intent) {
String tabLabel = getString(labelResId);
View indicator = View.inflate(this, R.layout.simple_tab_spec, null);
ImageView icon = (ImageView) indicator.findViewById(R.id.simple_tab_spec_icon);
icon.setImageResource(iconResId);
TabSpec tabSpec = tabHost.newTabSpec(tabLabel).setIndicator(indicator).setContent(intent);
tabHost.addTab(tabSpec); }
The first argument is the title of your tab item, the second argument is the background image of your tab item.
You should create an intent object to set the parameters and the target activity.

How to create tab's activitys before click on TabWidget bar?

i need to get the Activitys initialize , before anyone click on them . That is cause i have a video in one of them , and i need to have that Activity created to play the video from another tab (or activitys) .
I have this code to initialize the tab content but not the activitys into them :
//Init Tabs
Resources res = this.getResources();
TabHost tabHost = getTabHost();
tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
setupTab(tabHost,MyInitActivity.class,res.getDrawable(R.drawable.ic_tab_icon),res.getString(R.string.tab_init));
setupTab(tabHost,MusicGroupActivity.class,res.getDrawable(R.drawable.ic_tab_icon),res.getString(R.string.tab_playList));
setupTab(tabHost,SearchActivity.class,res.getDrawable(R.drawable.ic_tab_icon),res.getString(R.string.tab_search));
setupTab(tabHost,VideoActivity.class,res.getDrawable(R.drawable.ic_tab_icon),res.getString(R.string.tab_video));
I have seen code like this to try it :
tabHost.setCurrentTab(number);
but that seems to not run when you change immediately cause when i do
Context context = getTabHost().getChildAt(3).getContext();
it throws to me a null exception.
Anyone knows how to do this?
ADDED
private void setupTab( TabHost mTabHost,Class<? extends Activity> activityclass,Drawable image, String tag)
{
View tabview = createTabView(mTabHost.getContext(),image,tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new Intent(this, activityclass));
mTabHost.addTab(setContent);
}
Thanks for all
I do the following in the onCreate() (say i have 4 tabs)
tabHost.setCurrentTab(0);
tabHost.setCurrentTab(1);
tabHost.setCurrentTab(2);
tabHost.setCurrentTab(3);
tabHost.setCurrentTab(Tab_I_Want);

android first tab intent oncreate always called regardless we set tab2 as default tab

Following is the example of tabs with intent data.
While debugging i found that always when first tab we add in tab host in our case following tab
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("list")
.setContent(new Intent(this, List1.class)));
oncreate method of "List1" intent get called regardless it is our current tab or not even if if i define tab2 as a current tab how to fix this ?
public class Tabs3 extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("list")
.setContent(new Intent(this, List1.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("photo list")
.setContent(new Intent(this, List8.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
// This tab sets the intent flag so that it is recreated each time
// the tab is clicked.
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("destroy")
.setContent(new Intent(this, Controls2.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
}
}
setDefaultTab(1);
seems not to be working in TabActivity when separate Activities are used as Tab Content.
Use following instead of this method,
tabHost.setCurrentTab(1);
This will set "photo list" (i.e second tab) as the selected or default tab...
I have found this same behavior as well, and I do not have a specific fix. But I do know of a work-around.
Instead of attaching Activities to each tab, attach a View to each tab. You can then handle the data passing very easily as each view will be in the same Activity. This also eliminates the need to pass information using Intents. Furthermore, you can create (or inflate) your Views as you need them and with more control.
Good luck,
-scott

Categories

Resources