How to add seperate layouts under each tab using TabLayout

465 views Asked by At

I have followed this tutorial (http://developer.android.com/resources/tutorials/views/hello-tabwidget.html) and completed the task and it all works fine.

However now I am not sure how can I add diffrent layouts under each tabs? should add the layout to the main.xml and somehow call the layout from the calasses I have created for each tabs?

I would highly appreciate if can someone advice me on that.

For example I want to add a table layout under one of the tabs, how can I do that?

Many thanks

1

There are 1 answers

6
NagarjunaReddy On BEST ANSWER

See This example it is usefull Example of the TabLayout

modified this class Androidactivity.java

public class AndroidActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.androidtab);

add this xml file in Res/layout/androidtab.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"    
android:layout_width="fill_parent"
android:layout_height="fill_parent"       
android:gravity="top"       
android:stretchColumns="1">
<LinearLayout
    android:id="@+id/top_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/text"
        android:textStyle="bold"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20dp"
        android:text="Create Photo" />
</LinearLayout>    
 <TextView            
        android:id="@+id/editUsername"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_weight="4"
        android:text="CREATE NEW RECORD"            
        android:layout_marginLeft="25dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="75dp"   
        android:textStyle="bold"
        android:padding="10dp"/>
<TableLayout  
      android:id="@+id/tableLayout1"    
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"       
      android:gravity="top"       
      android:stretchColumns="1">       
  <TableRow
       android:id="@+id/row2"
       android:layout_marginTop="45dp"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
    <EditText
        android:inputType="text"
        android:id="@+id/editUsername"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_weight="2"
        android:singleLine="true"
        android:hint="input field1"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="2dip"
        android:layout_marginRight="75dp"   
        android:padding="10dp"/>
</TableRow>
<TableRow
    android:id="@+id/row2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <EditText
        android:inputType="text"
        android:id="@+id/editUsername"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_weight="2"
        android:singleLine="true"
        android:hint="input field2"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="2dip"
        android:layout_marginRight="75dp"   
        android:padding="10dp"/>
</TableRow>             
  <TableRow
    android:id="@+id/row2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <EditText
        android:inputType="text"
        android:id="@+id/editUsername"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_weight="2"
        android:singleLine="true"
        android:hint="input field3"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="2dip"
        android:layout_marginRight="75dp"   
        android:padding="10dp"/>
</TableRow>    
  <TableRow
    android:id="@+id/row2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <EditText
        android:inputType="text"
        android:id="@+id/editUsername"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_weight="2"
        android:singleLine="true"
        android:hint="input field4"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="2dip"
        android:layout_marginRight="75dp"   
        android:padding="10dp"/>
</TableRow>
  <RelativeLayout
      android:id="@+id/linearlayout"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:orientation="horizontal" 
      android:gravity="bottom" >
       <Button
        android:id="@+id/takephoto"
        android:layout_width="110dp"
        android:layout_height="35dp"
        android:text="takephoto"                   
        android:textStyle="bold"
        android:layout_marginLeft="102dp"    />
  </RelativeLayout>