Android Inflate Framelayout Inside Tabcontent
I need yours help to resolve simple issue . How dynamically 'connect' or inflate layout into Tab content. I want project with TabsHost . For each tab I like different layouts. Ve
Solution 1:
// try this way,hope this will help you...
tab1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/txtTab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab1"
android:textSize="25sp"/>
</LinearLayout>
tab2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/txtTab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab2"
android:textSize="25sp"/>
</LinearLayout>
tab3.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/txtTab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab3"
android:textSize="25sp"/>
</LinearLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TabHost
android:id="@+id/mytabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include
android:id="@+id/tab1"
layout="@layout/tab1" >
</include>
<include
android:id="@+id/tab2"
layout="@layout/tab2" >
</include>
<include
android:id="@+id/tab3"
layout="@layout/tab3" >
</include>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
private TabHost mytabhost;
private TextView txtTab1;
private TextView txtTab2;
private TextView txtTab3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mytabhost =(TabHost) findViewById(R.id.mytabhost);
txtTab1 =(TextView) findViewById(R.id.txtTab1);
txtTab2 =(TextView) findViewById(R.id.txtTab2);
txtTab3 =(TextView) findViewById(R.id.txtTab3);
mytabhost.setup();
mytabhost.addTab(mytabhost.newTabSpec("Tab1").setIndicator("MyTab1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1));
mytabhost.addTab(mytabhost.newTabSpec("Tab2").setIndicator("MyTab2",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab2));
mytabhost.addTab(mytabhost.newTabSpec("Tab3").setIndicator("MyTab3",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab3));
txtTab1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"My Tab 1",Toast.LENGTH_SHORT).show();
}
});
txtTab2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"My Tab 2",Toast.LENGTH_SHORT).show();
}
});
txtTab3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"My Tab 3",Toast.LENGTH_SHORT).show();
}
});
}
}
Post a Comment for "Android Inflate Framelayout Inside Tabcontent"