Opening OptionsMenu on the touch of a tab of the TabActivity - android

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

Related

Switch Taps from Activitys by buttons, etc

I have following TapControl in the StartActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for A Tap
TabSpec atapspec = tabHost.newTabSpec("ATap");
// setting Title and Icon for the Tab
atapespec.setIndicator("ATap", getResources().getDrawable(R.drawable.state_atap));
Intent atapIntent = new Intent(this, ATapActivity.class);
atapspec.setContent(atapIntent);
// Tab for B Tap
TabSpec btapspec = tabHost.newTabSpec("BTap");
btapspec.setIndicator("BTap", getResources().getDrawable(R.drawable.state_btap));
Intent btapIntent = new Intent(this, BtapActivity.class);
btapspec.setContent(btapIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(atapspec); // Adding a tab
tabHost.addTab(btapspec); // Adding b tab
}
Each Tap has its own Activity.
Now my problem, how can I switch with a button to the next Tap-Activity?
I tried to start just the Activity but then the Tap-control was missing.
startActivity(new Intent(this, BTapActivity.class));
I found that it should be sometion like that:
setCurrentTabByTag("BTab");
but i don't know how.
EDIT:
When I put "tabHost.setCurrentTabByTag("BTap");" on the end of the onCreate Method, BTap is selected.Therefore it is the correct command. But from another Activity I'm not able to access the tabHost.
When I make the the tabHost to a global Object, The App crashes:
TabHost tabHost = getTabHost();
I tried it like this from the Tab-Activity:
startActivity StartAct = new startActivity ();
StartAct.setTap("BTap");
This Method is in the StartActivity:
public void setTap(String tap) {
tabHost.setCurrentTabByTag(tap);
}
What can I do? Sorry, but I'm a beginner ...
You can use TabHost's setCurrentTab(int index) function to open tabs programmatically.

How to display tabbar in whole app in android?

Hi I am creating an application. In this I need to display a TabBar in all Activities. I set the TabBar bottom using 4 tabs; home, contact, about and call us.
Inside home tab I have some buttons. When I click inside home any button that time I need to move some other Activity. Using intent I moved to another Activity but here the TabBar was not displayed. However, I need to display the same TabBar in all Activities. If any one knows how to do this, please suggest a solution to me.
DefenceLaywer.java:
public class DefenceLaywer extends TabActivity {
TabHost tabhost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabhost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec firstTabSpec = tabhost.newTabSpec("Home");
TabSpec secondTabSpec = tabhost.newTabSpec("Claimonline");
TabSpec thirdTabSpec = tabhost.newTabSpec("CallUs");
TabSpec fourthTabSpec = tabhost.newTabSpec("AboutUs");
// TabSpec thirdTabSpec = tabhost.newTabSpec("Interactive");
firstTabSpec.setIndicator("Home", getResources().getDrawable(R.drawable.home));
secondTabSpec.setIndicator("ContactUs", getResources().getDrawable(R.drawable.contactus));
thirdTabSpec.setIndicator("CallUs", getResources().getDrawable(R.drawable.callus));
fourthTabSpec.setIndicator("AboutUs", getResources().getDrawable(R.drawable.aboutus));
firstTabSpec.setContent(new Intent(this,HomeTab.class));
secondTabSpec.setContent(new Intent(this,ContactUs.class));
thirdTabSpec.setContent(new Intent(this,CallUs.class));
fourthTabSpec.setContent(new Intent(this,AboutUs.class));
tabhost.addTab(firstTabSpec);
tabhost.addTab(secondTabSpec);
tabhost.addTab(thirdTabSpec);
tabhost.addTab(fourthTabSpec);
}
}
use fragments in activity and set that activity on tabspecs.
To learn FrAgment use following lin:
http://developer.android.com/guide/topics/fundamentals/fragments.html
and for how to use fragments to implement tab, follow followig tutorial:
http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/

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 tabhost add view to the tab from another tab

I need little bit help related to android tabhost. I have 3 tabs and 3 activities. MainTab activity is "extends from TabActivity" and other 2 activities are "extends from Activity".
In MainTab activity there are two buttons Radio and CheckBox, when user click radio button then i want to display radio button view in tab2. when user click check box i want to display checkbox view. Can some gives me an idea how I can achieve this?? Below is my code.
public class MainTabHost extends TabActivity implements OnTabChangeListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.homeui);
TabHost tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Main.class);
spec = tabHost.newTabSpec("Main Tab").
setIndicator("Main Tab")
.setContent(intent);
spec.setIndicator("", this.getResources().getDrawable(R.drawable.maintabicon) );
tabHost.addTab(spec);
intent = new Intent().setClass(this, TabA.class);
spec = tabHost.newTabSpec("Tab A").
setIndicator("Tab A")
.setContent(intent);
spec.setIndicator("", this.getResources().getDrawable(R.drawable.tabaicon) );
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
#Override
public void onTabChanged(String tabId) {
}
}
You can use SharedPreference for this. So that when you choose radio button you can change your shared value to radio and when you use Check box change value to check box and use the view in your second tab according to the shared value.

Categories

Resources