Android Listview onclick to start TabActivitycc - android

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

Related

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

ANDROID: Customized tab design crashes app on startup

I have bin trying to customize how my tabs look, and I don't get any errors. But when I try to run the app it crashes. This is what I got:
setContentView(R.layout.activity_main);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
and:
private void setUpTabs() {
// tabHost.setup();
//
// TabSpec spec1 = tabHost.newTabSpec("TAB 1");
// spec1.setContent(R.id.tab_1);
// spec1.setIndicator("", getResources().getDrawable(R.raw.home));
//
// TabSpec spec2 = tabHost.newTabSpec("TAB 2");
// spec2.setContent(R.id.tab_2);
// spec2.setIndicator("", getResources().getDrawable(R.raw.most));
// TabSpec spec3 = tabHost.newTabSpec("TAB 3");
// spec3.setContent(R.id.tab_3);
// spec3.setIndicator("", getResources().getDrawable(R.raw.favorites));
// TabSpec spec4 = tabHost.newTabSpec("TAB 4");
// spec4.setContent(R.id.tab_4);
// spec4.setIndicator("",
// getResources().getDrawable(R.raw.search_icon));
// tabHost.addTab(spec1);
// tabHost.addTab(spec2);
// tabHost.addTab(spec3);
// tabHost.addTab(spec4);
// tabHost.setOnTabChangedListener(this);
setContentView(R.layout.activity_main);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
setupTab(new TextView(this), "Tab 1");
setupTab(new TextView(this), "Tab 2");
setupTab(new TextView(this), "Tab 3");
}
private void setupTab(final View view, final String tag) {
View tabview = createTabView(tabHost.getContext(), tag);
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {
return view;
}
});
tabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context)
.inflate(R.xml.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
The section commented out, is what I did before using the standard tab design.
The .xml parts so far should have no problems, sins that it so far is based 100% on other stuff that I dug up in the net.
private void setupTab(final View view, final String tag) {
View tabview = createTabView(tabHost.getContext(), tag);
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {
final TextView tv = new TextView(this);
tv.setText("Content for tab with tag " + tag);
return view;
}
});
tabHost.addTab(setContent);
}
like this..........
You have to specify the view inside createTabContent{} method........the view returns null thats why its crashing.......

Debug: How to start an activity using custom Tabs

#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
icon = (ImageView) findViewById(R.id.header);
Resources res = getResources();
// construct the tabhost
setContentView(R.layout.tab_layout);
setupTabHost();
// mTabHost.getTabWidget().setDividerDrawable(R.drawable.hotels);
setupTab(new TextView(this), "Hotels");
res.getDrawable(R.drawable.hote);
setupTab(new TextView(this), "MyAccount");
res.getDrawable(R.drawable.ma);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.tab_layout);
View title = getWindow().findViewById(R.id.tabsLayout);
}
#TargetApi(4)
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabSpec set_Content = mTabHost.newTabSpec(tag).setIndicator(tabview)
.setContent(new TabContentFactory() {
public View createTabContent(String tag) {
return view;
}
});
switch (tag) {
case ("Hotels"): {
Intent setClass = new Intent(this, ListSample.class);
set_Content.setContent(setClass);
break;
}
case ("MyAccount"): {
/*
* Intent setClass = new Intent(this, AccountActivity.class);
* set_Content.setContent(setClass);
*/break;
}
}
mTabHost.addTab(set_Content);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context)
.inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
I'm customizing the Tabs and trying to start an activity for each tab. I'm not able to find the error.
try the following code:
final TabHost tabHost = getTabHost();
TabHost.TabSpec ts = tabHost.newTabSpec("ID_1");
ts.setIndicator("1");
ts.setContent(Intent1);// Intent to start activity
tabHost.addTab(ts);
ts = tabHost.newTabSpec("ID_2");
ts.setIndicator("2");
ts.setContent(Intent2);
tabHost.addTab(ts);
ts = tabHost.newTabSpec("ID_3");
ts.setIndicator("3");
ts.setContent(Intent3);
tabHost.addTab(ts);

how to start new activity from list activity within tabview

I am developing new application where I am using tab view as parent layout. I'm using TabHost to display 3 tabs within my application. Each of these tab has separate Activity containing a ListView. This is working fine. When you click on an item within the ListView it currently loads up a brand new Activity full screen leaving the TabHost. I'd like to load up these Activities within the TabHost. I want to retain the tabview after calling another activities from list view.
Thank you both for your response. Here is my code where I need your help.
################HelloTabWidget
//This class displays the tab view with 3 tab - Customer, Company and City.
public class HelloTabWidget extends TabActivity {
//public class HelloTabWidget extends ActivityGroup {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, CustomerTabView.class);
spec = tabHost
.newTabSpec("Customer")
.setIndicator("Customer",
res.getDrawable(R.drawable.ic_tab_Customer))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, CompanyTabView.class);
spec = tabHost
.newTabSpec("Company")
.setIndicator("Company",
res.getDrawable(R.drawable.ic_tab_Company))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, CityTabView.class);
spec = tabHost
.newTabSpec("City")
.setIndicator("City", res.getDrawable(R.drawable.ic_tab_City))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
################CustomerTabView
//This class displays list view of customers names. On click on any item in the list, it should open customer detail page keeping same tabs view.
public class CustomerTabView extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] category = getResources().getStringArray(
R.array.category_array);
setListAdapter(new ArrayAdapter<String>(this, R.drawable.list_items,
category));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Need this logic where I can retain the tab view and call new activity class for customerdetails view.
Intent intent;
intent = new Intent(CustomerTabView.this,
C_DetailActivity.class);
startActivity(intent);
finish();
}
});
}
}
################C_DetailActivity
On click of any item from customertabview, this activity class gets call which shows details of the customer.
public class C_DetailActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the Customer Details view");
setContentView(textview);
}
}
After calling C_DetailActivity class, tab view disappear. I want to retain the main tab view.
So need this logic where I can retain the tab view and call new activity class for customerdetails view
ListView lv = (ListView) findViewById(R.id.myListView);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
LocalActivityManager manager = getLocalActivityManager();
String currentTag = tabHost.getCurrentTabTag();
manager.destroyActivity(currentTag, true);
manager.startActivity(currentTag, new Intent(this, newClass.class));
}
});
Not tested, but something like this should work. If is doesn't I'll help you to fix it.

Calling different tabs from normal activity in 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);

Categories

Resources