Custom Tab Content - android

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.

Related

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

How to Create Custom Tab In Android Application

thanks advance..
when i create custom tab in android application so no one tab display in main screen
how to solve this problem please any one tell me?
you mean that you want to create custom tab for your application.ok now you have two class that is homemyclass.java and myclass.java. At first you declare flowing two line in homemyclass.java
private Intent mytab;
private static final String TAB_MYCLASS = "myclass";
then you write flowing line into onCreate() function of homemyclass.java
mytab = new Intent(this, myclass.class);
addTab(TAB_MYCLASS, getString(R.string."label for tab"), R.drawable."icon for tab display when
tab is selected", R.drawable."icon for tab display when tab is not selected", mytab);
just add addTab() into homemyclass.java
private void addTab(String tag, String label, int icon, int ficon, Intent content) {
TabHost tabHost = getTabHost();
TabSpec tabspecDialer = tabHost.newTabSpec(tag).setContent(content);
//boolean fails = true;
tabspecDialer.setIndicator(label, getResources().getDrawable(icon));
tabHost.addTab(tabspecDialer);
}
you also need to modify androidManifest.xml by homemyclass.java and myclass.java

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.

Change tab using onClickItem in ListActivity

Can someone please tell me how to change tab by clicking on element INSIDE the tab? I already tried it with global data. The code looks like this:
public class Tabs extends TabActivity {
int tabNumber = 0;
private TabHost tabHost;
int returnedTabNumber = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
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, Tribocracy.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("map").setIndicator("Map",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Areas.class);
spec = tabHost.newTabSpec("areas").setIndicator("Areas",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Settings.class);
spec = tabHost.newTabSpec("settings").setIndicator("Settings",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(tabNumber);
}
protected void onResume() {
super.onResume();
GlobalData globalData = ((GlobalData)getApplicationContext());
returnedTabNumber = globalData.getTabNumber();
tabHost.setCurrentTab(returnedTabNumber);
}
}
The global adapter looks like this:
public class GlobalData extends Application {
//----------------------------------------------------
private int Point1; //define the vars here
private int Point2; //define the vars here
private int Point3; //define the vars here
private int Point4; //define the vars here
private int Point5; //define the vars here
private int Point6; //define the vars here
private int tabNumber;
public int getTabNumber() //getter of the value
{
return tabNumber;
}
public int setTabNumber(int number) //setter of the value
{
tabNumber = number;
return tabNumber;
}
}
Now when I'm trying to change tab in my ListActivity tab by clicking on one of the items it doesn't do anything and stays on the ListActivity tab. Perhaps I shouldn't use onResume() here. Basically I want to go to first tab when I click on one of the items in the list. Please help!
One way to do this is to use Intents. And there are many ways to use Intents for this. This works especially well for some cases, for example where the activity is displaying some content from a content provider. I'll describe one possible method below:
For the Activity that hosts the tabs, give it an intent filter if it doesn't already have one. For example, if it's an 'item detail'-style Activity, maybe its intent filter can match for ACTION_VIEW on a certain content type like vnd.android.cursor.item/vnd.somecontent.
Have the Activity take an extra such as focus_tab, and in the Activity's onCreate, after setting up the tabs, set the active tab to the one specified by this focus_tab extra.
Override onNewIntent, and in that method, look for that same focus_tab extra and set the active to the one specified by that extra.
Now, in your button code (i.e. a button inside one of your tab content activities), create an Intent that will start your host Activity, and give it a focus_tab extra corresponding to the tab you want to switch to. Add the FLAG_ACTIVITY_SINGLE_TOP Intent flag so you don't create a new instance of the host Activity. And then, call startActivity on that Intent you just created.
Note: I haven't tried this myself, but it should work in theory.

Categories

Resources