Calling different tabs from normal activity in android - android

in my app after the SplashScreen i am calling a Tabactivity.
In tha tab activity, from the first tab i am switched over to a another activity called as Float, which is not related to the TabActivity. From this activity when a condition becomes True i want to show the third tab in the TabBar. How to open the third tab from the tab activity.
Following is the code of my Tabactivity class
public class MainTabBar extends TabActivity
{
TabHost tabHost;
Intent intent;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.maintab);
addTab1(Display.class);
addTab2(History.class);
addTab3(Capture .class);
addTab4(AboutUs.class);
}
private void addTab1( Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("Tab1");
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.hometab, getTabWidget(), false);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
private void addTab2( Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("Tab2");
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.macstab, getTabWidget(), false);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
private void addTab3( Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("Tab3");
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.abouttab, getTabWidget(), false);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
private void addTab4( Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("Tab4");
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.contacttab, getTabWidget(), false);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
Is it to the above thing using a flag or any other easy way, pls suggest me friends, i am very new to android

You can use the tab index to show the third tab
tabHost.setCurrentTab(2);

Related

Cannot resolve constructor Intent

Im trying to add a tabhost to my application but i cant figure why this error 'cannot resolve constructor intent' is showing, here's my activity code:
public class SixthFragment extends Fragment {
public static final String TAG = "sixth";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sixthfragment, container, false);
TabHost tabHost = (TabHost) view.findViewById(R.id.tabHost);
TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,MainActivity.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this,Tab2Activity.class));
tab3.setIndicator("Tab3");
tab3.setContent(new Intent(this,Tab3Activity.class));
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
some help would be apreciated, thanks for your time.
The first parameter to the Intent constructor that you are trying to use takes a Context. Fragment is not a Context. Use getActivity() instead of this.

How to add Run Time Tabs into TabHost in Android?

Hello I am developing an application
Which requires to add Run Time tabs in the android And all the tabs should add dynamically.
My Requirement is:
I want to add Tabhost with 5 tabs Which Should display the Screen.
But some Time it require only 3 tabs in the screen then design should not change.
Same if I want to add more then 5 tabs then tab should scroll Run time The main Thing is Design should not change.
Can any one tell me How can i do that?
Currently I am using Following Code:
public class Story_List extends ActivityGroup
{
ListView list_stories;
TabHost tab_stories;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.story_list);
tab_stories=(TabHost)findViewById(R.id.tabhoststories);
tab_stories.setup(this.getLocalActivityManager());
setupTab1(new TextView(this), "Album 1");
setupTab2(new TextView(this), "Album 2");
setupTab3(new TextView(this), "Album 3");
}
private void setupTab1(final View view, final String tag)
{
View tabview = createTabView(tab_stories.getContext(), tag);
Intent intent = new Intent().setClass(this, StoryAlbum1.class);
TabSpec tab = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent);
tab_stories.addTab(tab);
}
private void setupTab2(final View view, final String tag)
{
View tabview = createTabView(tab_stories.getContext(), tag);
Intent intent = new Intent().setClass(this, StoryAlbum2.class);
TabSpec tab = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent);
tab_stories.addTab(tab);
}
private void setupTab3(final View view, final String tag)
{
View tabview = createTabView(tab_stories.getContext(), tag);
Intent intent = new Intent().setClass(this, StoryAlbum3.class);
TabSpec tab = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent);
tab_stories.addTab(tab);
}
private static View createTabView(final Context context, final String text)
{
View view = LayoutInflater.from(context).inflate(R.layout.tabs_text, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
try this :
TabHost.TabSpec tabSpec = tabHost.newTabSpec("Tab1");
tabSpec.setContent(R.id.btnTab);
tabSpec.setIndicator("My Tab 1");
tabHost.addTab(tabSpec);
btnAddTab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TabHost.TabSpec spec = tabHost.newTabSpec("Tab"+i);
spec.setContent(new TabHost.TabContentFactory() {
#Override
public View createTabContent(String tag) {
return new AnalogClock(MainActivity.this);
}
});
spec.setIndicator("Clock");
tabHost.addTab(spec);
}
});

FragmentTabHost and custom tabs

I really need to access the images of a tab at runtime for scaling.
Therefore I made a custom tab layout with an imageview and a textview.
But if i want to add the custom tab to my tabhost I a "did you forget to call 'public void setup(localactivitymanager activitygroup)'" exception.
Thx in advance for any solutions ;D
Ps. I cannot use
spec = tabHost.newTabSpec("2").setIndicator(res.getString(R.string.tabname2),
res.getDrawable(R.drawable.tabimage2));
tabHost.addTab(spec, FragmentTwo.class, null);
because i need to scale the image before adding.
Here is my class:
public class MyTabActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabmanager_layout);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
{
mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#3b6c8d")); //unselected
}
}
private void addCustomTab(Context context,String labelId, Drawable drawable, Class<?> c, FragmentTabHost fth ) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_customtab, null);
ImageView image = (ImageView) view.findViewById(R.id.icon);
TextView text = (TextView) view.findViewById(R.id.tabtitle);
image.setImageDrawable(drawable);
text.setText(labelId);
TabHost.TabSpec spec = fth.newTabSpec(labelId);
Intent i = new Intent(this,c);
spec.setContent(i);
spec.setIndicator(view);
fth.addTab(spec);
}
You need to use FragmentTabHost's custom addTab() which will also take your Fragment class as parameter.
Replace
TabHost.TabSpec spec = fth.newTabSpec(labelId);
Intent i = new Intent(this,c);
spec.setContent(i);
spec.setIndicator(view);
with
TabHost.TabSpec spec = fth.newTabSpec(labelId);
spec.setIndicator(view);
fth.addTab(spec, c, null);

Android TabHost / back button

I have a TabHost application with 4 tabs.
Tab 1
List A > List B > View
Tab 2
Camera Activity which start camera and flash onCreate
Tab 3
List C > View
Tab 4
View D > View E
So, when I first start the app, I can go to different tabs back and forth the stack without any problem. But once I started Tab 2 (camera tab) and I go to other tabs from there and go down the stack and when i press the back button, the camera starts automatically even though I am not at Tab 2 (i.e.: at List B, press back button and camera starts even I just go back to List A) ... and when I go to Tab 2 again, the app will crash (Sorry, the Android camera encountered a problem...etc.)
Can anyone tell me what the problem is? Below is my TabHost activity
package com.myapp.appname;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.view.Window;
public class MainActivity extends TabActivity {
/** Called when the activity is first created. */
private TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.top_titlebar);
setTabs();
}
private void setTabs() {
tabHost = getTabHost();
listTab(R.string.tab_1, R.drawable.ic_tab_1);
cameraTab(R.string.tab_2, R.drawable.ic_tab_2);
list2Tab(R.string.tab_3, R.drawable.ic_tab_3);
viewTab(R.string.tab_4, R.drawable.ic_tab_4);
}
private void listTab(int labelId, int drawableId) {
Intent intent = new Intent(this,ListActivity.class);
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);
}
private void cameraTab(int labelId, int drawableId) {
Intent intent = new Intent(this,CameraActivity.class);
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);
}
private void list2Tab(int labelId, int drawableId) {
Intent intent = new Intent(this,List2Activity.class);
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);
}
private void viewTab(int labelId, int drawableId) {
Intent intent = new Intent(this,ViewActivity.class);
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 don't think that the problem is with tabhost, but with camera. Try moving the code that you have in onCreate into onResume. Also you should worry about releasing the camera when you go to another tab.

Android Listview onclick to start TabActivitycc

I have a listview withcin a activity and when someone clicks
on an item in the listview I want to start a activity of type
TabActivity.
Any ideas on why it's not working?
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
JobListRowData item = jobListAdapter.getItem(position);
Intent myIntent = new Intent(view.getContext(), EventsTabs.class);
startActivity(myIntent);
} } });
public class EventsTabs extends TabActivity {
private TabHost mTabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.event_main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, EventsTabs.class);
spec = tabHost.newTabSpec("artists").setIndicator("Artists", res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, EventsTabs.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, EventsTabs.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs", res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
tabHost.setup();
}
}
For some reason I get a :
12-06 13:37:47.607: ERROR/AndroidRuntime(346): Caused by: java.lang.IllegalStateException: Activities can't be added until the containing group has been created.
Try adding this to your code..
LocalActivityManager mlam = new LocalActivityManager(this, false);
mlam.dispatchCreate(savedInstanceState);
tabHost.setup(mlam );
EDIT: you should set up the tabhost before adding the tabs to it..
You should call set up first like this...
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
tabHost.setup(); //Move set up here
Intent intent;
intent = new Intent().setClass(this, EventsTabs.class);
spec = tabHost.newTabSpec("artists").setIndicator("Artists", res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, EventsTabs.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, EventsTabs.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs", res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);

Categories

Resources