HOW TO ADD DIFFERENT XML(VIEWS) TO EACH COMPONENT OF TABHOST

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

HOW TO ADD DIFFERENT XML(VIEWS) TO EACH COMPONENT OF TABHOST

Postby yu398616602 » Tue Mar 30, 2010 10:50 pm

Hi, I am working on this issue for 2 days now, and i cant find any answer to my problem, hope anyone can help me.

this is my main.java:


public class MainActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TabHost tabs = (TabHost)this.findViewById(R.id.th_set_menu_tabhost);
tabs.setup();

TabHost.TabSpec ts1 = tabs.newTabSpec("Page 1");
ts1.setIndicator("", this.getResources().getDrawable(R.drawable.icon1));
ts1.setContent(R.id.content1);
tabs.addTab(ts1);

TabHost.TabSpec ts2 = tabs.newTabSpec("Page 2");
ts2.setIndicator("",this.getResources().getDrawable(R.drawable.icon2));
ts2.setContent(R.id.content2);
tabs.addTab(ts2);

TabHost.TabSpec ts3 = tabs.newTabSpec("Page 3");
ts3.setIndicator("",this.getResources().getDrawable(R.drawable.icon3));

ts3.setContent(new Intent(this, People.class));

//ts3.setContent(R.id.content3);

tabs.addTab(ts3);

}

}

and this is my main.xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/th_set_menu_tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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="200dp"
android:paddingTop="30dp">
<LinearLayout
android:id="@+id/content1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/content2"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/content3"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
</TabHost>

and i was trying to add a different view/layout than the default one in main.xml to one of the tabs (tab3).
the class people.java is a simple file:


public class People extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tabcontent1);
}
}
which calls the tabcontent1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content4"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="plain.xml" />
</LinearLayout>

but it got me error on emulator when i am clicking on "tab3" which intents the people.class.


anyone can help me would be really appreciated.

Tinne

[fade]
yu398616602
Freshman
Freshman
 
Posts: 2
Joined: Tue Mar 30, 2010 1:41 pm

Top

Re: HOW TO ADD DIFFERENT XML(VIEWS) TO EACH COMPONENT OF TAB

Postby songotho » Wed Mar 31, 2010 7:43 pm

yu398616602 wrote:Hi, I am working on this issue for 2 days now, and i cant find any answer to my problem, hope anyone can help me.

this is my main.java:


public class MainActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TabHost tabs = (TabHost)this.findViewById(R.id.th_set_menu_tabhost);
tabs.setup();

TabHost.TabSpec ts1 = tabs.newTabSpec("Page 1");
ts1.setIndicator("", this.getResources().getDrawable(R.drawable.icon1));
ts1.setContent(R.id.content1);
tabs.addTab(ts1);

TabHost.TabSpec ts2 = tabs.newTabSpec("Page 2");
ts2.setIndicator("",this.getResources().getDrawable(R.drawable.icon2));
ts2.setContent(R.id.content2);
tabs.addTab(ts2);

TabHost.TabSpec ts3 = tabs.newTabSpec("Page 3");
ts3.setIndicator("",this.getResources().getDrawable(R.drawable.icon3));

ts3.setContent(new Intent(this, People.class));

//ts3.setContent(R.id.content3);

tabs.addTab(ts3);

}

}

and this is my main.xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/th_set_menu_tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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="200dp"
android:paddingTop="30dp">
<LinearLayout
android:id="@+id/content1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/content2"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/content3"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
</TabHost>

and i was trying to add a different view/layout than the default one in main.xml to one of the tabs (tab3).
the class people.java is a simple file:


public class People extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tabcontent1);
}
}
which calls the tabcontent1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content4"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="plain.xml" />
</LinearLayout>

but it got me error on emulator when i am clicking on "tab3" which intents the people.class.


anyone can help me would be really appreciated.

Tinne

[fade]


U can find some example in Google API Demos
http://developer.android.com/resources/ ... index.html

Hope help u.
Alex
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Re: HOW TO ADD DIFFERENT XML(VIEWS) TO EACH COMPONENT OF TAB

Postby pemko » Fri May 28, 2010 12:28 pm

i don't find any good examples...
pemko
Freshman
Freshman
 
Posts: 9
Joined: Fri May 28, 2010 12:17 pm
Location: Switzerland

Re: HOW TO ADD DIFFERENT XML(VIEWS) TO EACH COMPONENT OF TAB

Postby songotho » Fri May 28, 2010 4:54 pm

pemko wrote:i don't find any good examples...


Hi,

How do you think that these are good examples ? ^^ if possible you can see some examples of tabs and improve by yourself.

Best regard,
Alex.
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 10 guests