TabHost not showing contents in the beginning - android

I have a TabActivity with two tabs. the layout of the activity is as follows:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="5dp">
<ListView android:id="#+id/list"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</FrameLayout>
</LinearLayout>
</TabHost>
The layout consists of a ListView which is populated accordingly in setOnTabChangedListener(). no problem with filling the list and showing it.
My problem is that the list view is not shown when the activity starts, although I find it by ID and populate it; it is only populated after I change the tabs.
and the code in the onCreate(..) looks like this:
l = (ListView) findViewById(R.id.list);
l.setAdapter(new CommentsAdapter(this, (JSONArray) obj));
TabSpec spec;
spec = tabHost.newTabSpec("0").setIndicator("0",
res.getDrawable(R.drawable.tab_recent)).setContent(
R.id.list);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("1").setIndicator("1",
res.getDrawable(R.drawable.tab_pop)).setContent(
R.id.list);
tabHost.addTab(spec);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
int tab = Integer.valueOf(tabId);
showDialog(WAIT_DIALOG);
switch (tab) {
// recent
case 0:
//refill the list
break;
// popular
case 1:
//refill the list
break;
}
}
});
tabHost.setCurrentTab(0);
any hints?

I had a similar issue. What I noticed is that if you do
tabHost.setCurrentTab(0);
then onTabChanged is not triggered. BUT, if you do
tabHost.setCurrentTab(1);
then it is, the list shows up and all it's fine. As per why this happens, it remains a bit of a mistery to me.
Use this cheat to start your app on the first tab:
in onCreate(), after you've created the tabs call first:
tabHost.setCurrentTab(1);
Then call:
tabHost.setCurrentTab(0);

You can try use different layout, say
spec = tabHost.newTabSpec("0").setIndicator("0",
res.getDrawable(R.drawable.tab_recent)).setContent(
R.id.list1);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("1").setIndicator("1",
res.getDrawable(R.drawable.tab_pop)).setContent(
R.id.list2);
tabHost.addTab(spec);

you just do another activity in which list view is present & put the below one
Intent i1 = new Intent(tabs.this,listDisplay.class);
spec=tabHost.newTabSpec("0").setIndicator("0",res.getDrawable(R.drawable. tab_recent)).setContent(i1);
Also put the tabHost.setCurrentTab(0);
before ontabChangeListener Hope this might help..
hope this might solve the problem..

Related

Open child activity in Tab host android

My Tab_Bar.class define define Tabs
How I can open child activity
I am using only one single Tab_bar.class for Tab Host
public class Tab_Bar extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
setTabs() ;
}
void setTabs()
{
addTab("My Profile", R.drawable.home_normal, MyProfile.class);
addTab("Search", R.drawable.search_normal, JobSearch.class);
addTab("Saved Jobs", R.drawable.starred, Saved_jobs.class);
addTab("Job Alert", R.drawable.job_match, JobAlert.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
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);
tabHost.setCurrentTab(1);
}
}
I am using these xml file
TabIndicator.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="55dip"
android:layout_weight="1"
android:orientation="vertical"
android:background="#drawable/tab_indicator"
android:padding="5dp">
<ImageView android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/icon"
/>
<TextView android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/tabWidgetStyle"
android:textSize="13sp"
/>
and Tab.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="0dip"
android:layout_weight="1" />
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="0" />
</LinearLayout>
</TabHost>
I am using Tab_bar.class and these xml file for Tab host but i can't have any idea about open
tab host child activity .
I am new in android.
Please Help me, How i can open child activity
Any Help is Appreciated
And I am really sorry about my bad English
This is not eaxctly what you need, but might help. I used this setup to create dynamic tabs and then do different things with them.
protected void onCreate(Bundle savedInstanceState) {
...
final TabHost Tabs = (TabHost) findViewById(android.R.id.tabhost);
Tabs.setup();
int count;
for (count =0;count < 2;count++){
...
final int passedTabId = count;
NewTab.setContent(new TabHost.TabContentFactory()
{
public View createTabContent(String tag)
{
...
RelativeLayout layout = new RelativeLayout(getApplicationContext);
android.widget.RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(params);
layout.setId(some ID);
layout.setBackgroundResource(R.drawable.room_background);
TextView dynText = new TextView(getApplicationContext);
...
layout.addView(dynText);
return layout;
// You can set onClickListeners, etc here and then assign them some functions you need
// You can also create different layouts for every tab according to the passedTabId
}
});
Tabs.addTab(NewTab);
}
}
Thing is, you cant just simply set another activity to run in each tab. You need to set some functions you need to the objects you create in that tab, but the main activity remains the same. Good luck :)
Firstly try using Fragment for creating tabs.
Feature which you are using is deprecated by now.
I have posted complete post over Dynamically changing the fragments inside a fragment tab host?
Please check that out and let me know if you have any concern.
Simply fallow each of defined steps one by one.
CONCEPT
I am putting simple example for clarifying you that exactly how this fragment works.
Take simple scenario of your bed. Your bed consist of several things such as pillow, bed sheet etc.
Now consider a case that your bed sheet is dirty. So what you will do by now. You will simply replace that sheet with a new one and then you will have a tight sleep :)
Similarly when you need to change your UI... then this fragment is replaced with a new one.
That's it. Now you are good to go with my post.
Thanks!

tab doesnot show tab name in ginger bread

I am developing an application that uses tabs.
The problen is: in devices with API level 15 or higher it is working fine, but in lower than API 15 (for ex GingerBread) it is not working.
Problem: In tabs, it dosenot show the text that I have written on tabs, it just show blank tabs.
You can see the images
correct image : we can see the tab names "Tracker"," Call Logs", "STD codes" and "ISD Codes"
Problem Image tab name not shown in this image(in gingerbread)
As you can see in this image tab names names are not shown.
My code is below
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:id="#android:id/tabhost"
>
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/tabcontent"
>
</FrameLayout>
</LinearLayout>
</TabHost>
My activity file is
public class MobileNumberTrackerTabbedHomeActivity extends TabActivity {
/** Called when the activity is first created. */
TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost=getTabHost();
//First Tab
TabSpec tab1=tabHost.newTabSpec("Tracker");
tab1=tab1.setIndicator("Tracker");
Intent intentNumberTracker =new Intent(this, NumberTrackerActivity.class);
tab1=tab1.setContent(intentNumberTracker);
TabSpec tab2=tabHost.newTabSpec("STD Codes");
tab2=tab2.setIndicator("STD Codes");
Intent intentSTDCode=new Intent(this,STDCodeFinderActivity.class);
tab2=tab2.setContent(intentSTDCode);
TabSpec tab3=tabHost.newTabSpec("ISD Codes");
tab3=tab3.setIndicator("ISD Codes");
Intent intentISDCode=new Intent(this,ISDCodeFinderActivity.class);
tab3=tab3.setContent(intentISDCode);
TabSpec tab4=tabHost.newTabSpec("Call Logs");
tab4=tab4.setIndicator("Call Logs");
Intent intenCallLogs=new Intent(this,ShowCallLogsActivity.class);
tab4=tab4.setContent(intenCallLogs);
tabHost.addTab(tab1);
tabHost.addTab(tab4);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
}
}
What should I do so that tabs worh fine with all APIs
Please suggest a solution
thanks
It should be like,
//First Tab
TabSpec tab1=tabHost.newTabSpec("Tracker");
tab1.setIndicator("Tracker");
Intent intentNumberTracker =new Intent(this, NumberTrackerActivity.class);
tab1.setContent(intentNumberTracker);
for each Tab, instead of
//First Tab
TabSpec tab1=tabHost.newTabSpec("Tracker");
tab1=tab1.setIndicator("Tracker");
Intent intentNumberTracker =new Intent(this, NumberTrackerActivity.class);
tab1=tab1.setContent(intentNumberTracker);
By assigning everytime tab1 = new content/setting you are over-writing the previous set fields.

How to return result from Tabs to my Activity

I am developing an application using android 2.1. In one Page, upper part have two textviews and lower part have tablayout having two tabs.
My XML code is like this...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include android:id="#+id/content" layout="#layout/content_table"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabHost" android:layout_below="#+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
</RelativeLayout>
And activity class is like...
public class PageActivity extends ActivityGroup {
LogoBarActivity logoBar;
TabHost mTabHost;
TextView text1;
TextView text2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.page);
text1 = findViewById(R.id.textview1);
text2 = findViewById(R.id.textview2);
mTabHost = (TabHost) findViewById(R.id.tabHost);
mTabHost.setup(this.getLocalActivityManager());
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Type1Activity.class);
spec = mTabHost.newTabSpec("type1").setIndicator("Type1").setContent(intent);
mTabHost.addTab(spec);
intent = new Intent().setClass(this, Type2Activity.class);
spec = mTabHost.newTabSpec("type2").setIndicator("Type2").setContent(intent);
mTabHost.addTab(spec);
mTabHost.setCurrentTab(0);
mTabHost.getTabWidget().getChildAt(0).getLayoutParams().height /= 2;
mTabHost.getTabWidget().getChildAt(1).getLayoutParams().height /= 2;
}
}
Explanation: When user comes this page, he can click on any tabs either on Type1 or Type2. Both Tabs have ListView (Type1Activity & Type2Activity are ListView ).
When user click on any tab he will get ListView in Tabs. User can select any item from listview and that item will set in text1 and text2 variable of PageActivity class respectively.
Question: How can I return result from tab activity to my PageActivity when user select any item from list view of any Tab.
You can get the position from the ListView in the tabs, use that position to get corresponding data in the onClickListener.
Set this data in the TextViews. I am assuming ListViews' OnClickListener has reference to the TextViews

Set Title in Tab childs

How can I change a title from tab child?
I tried a simple setTitle(...) but it won't work.
(from the parent tab activity, it does, however...)
thanks,
Ori
Actually I got it to work this way:
In the TabActvitiy, override:
onChildTitleChanged().
This method is called when a child activity invoking setTitle(). from there you can change the title of the screen.
There is no API to modify the tabs once created -- sorry!
I know that if you're adding tabs dynamically you can use ts.setIndicator("MY TAB!");
...
like so:
myTabHost = (TabHost)this.findViewById(R.id.th_set_menu_tabhost);
myTabHost.setup();
ls1 = new ListView(Main_screen.this);
TabSpec ts1 = myTabHost.newTabSpec("TAB_TAG_1");
ts1.setIndicator("Tab1");
ts1.setContent(new TabHost.TabContentFactory(){
public View createTabContent(String tag)
{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main_screen.this,android.R.layout.simple_list_item_1,new String[]{"item1","item2","item3"});
ls1.setAdapter(adapter);
return ls1;
}
});
myTabHost.addTab(ts1);
here's the xml if you need it
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="64dip" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="64dip">
<ListView
android:id = "#+id/danhsach"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>

Why do I get an error while trying to set the content of a tabspec in android?

I have an android activity in which I'm using tabs.
public class UnitActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unit_view);
TabHost tabHost = getTabHost();
TabSpec spec;
spec = tabHost.newTabSpec("controls");
spec.setIndicator("Control");
spec.setContent(R.layout.unit_control);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("data");
spec.setIndicator("Data");
spec.setContent(R.layout.unit_data);
tabHost.addTab(spec);
}
}
However when I run the program it crashes with the error:
"Could not create tab content because could not find view with id 2130903042". I don't understand what the problem is because R.layout.unit_data refers to a layout file in my resource directory (res/layout/unit_data.xml)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:stretchColumns="*"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner android:id="#+id/unit_num"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/choose_unit"/>
<TableRow android:padding="2dp">
<TextView
android:gravity="right"
android:padding="5dp"
android:text="#string/Power"/>
<TextView android:id="#+id/unit_power"
android:layout_span="3"
android:gravity="center"
android:padding="5dp"
android:background="#android:color/white"
android:textColor="#android:color/black"
android:text="AUTO"/>
</TableRow>
...
</TableLayout>
</ScrollView>
as far as I can tell unit_data.xml is well formed and I've even referenced it successfully in another activity
class UnitData extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unit_data);
Toast.makeText(this, "Hi from UnitData.onCreate", 5);
}
}
which does not give an error and renders the layout just fine.
What's going on? Why can't I reference this layout when creating a tab?
While Activity.setContentView takes an id of a Layout, TabSpec.setContent takes an id of a View. This means you need to pass an id that looks like R.id.something and not R.layout.something.
To solve your particular problem, give the top level view in your layout a view id:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/unit_data"> <!-- NOTE THE CHANGE -->
...
</ScrollView>
and update your source:
public class UnitActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unit_view);
TabHost tabHost = getTabHost();
TabSpec spec;
spec = tabHost.newTabSpec("controls");
spec.setIndicator("Control");
spec.setContent(R.id.unit_control); // NOTE THE CHANGE
tabHost.addTab(spec);
spec = tabHost.newTabSpec("data");
spec.setIndicator("Data");
spec.setContent(R.id.unit_data); // NOTE THE CHANGE
tabHost.addTab(spec);
}
}
For more information, see the tab examples in the ApiDemos:
If you have your tab layout in a different file, you have to inflate the XML.
spec = tabHost.newTabSpec("data");
spec.setIndicator("Data");
// Add the layout to your tab view
getLayoutInflater().inflate(R.layout.unit_data, tabHost.getTabContentView(), true);
spec.setContent(R.id.unit_data);
tabHost.addTab(spec);
You also have to use LayoutInflater after TabHost
LayoutInflater.from(this).inflate(R.layout.unit_data, tabHost.getTabContentView(), true);
I also got stuck with this and finally figure it out.

Categories

Resources