Tuesday 29 October 2013

Tab Example

Tabs in the action bar make it easy to explore and switch between different views or functional aspects of your app, or to browse categorized data sets.

Example:





Code:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <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" >

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="84dp"
                    android:orientation="vertical" >

                    <Button
                        android:id="@+id/button1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Button" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Large Text"
                        android:textAppearance="?android:attr/textAppearanceLarge" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <RatingBar
                        android:id="@+id/ratingBar1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.tabexample;

import android.os.Bundle;
import android.app.TabActivity;
import android.content.res.Resources;
import android.widget.TabHost;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
TabHost th;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
th = getTabHost();
Resources rs = getResources();
th.addTab(th.newTabSpec("First").setIndicator("Button",rs.getDrawable(R.drawable.ic_launcher)).setContent(R.id.button1));
th.addTab(th.newTabSpec("second").setIndicator("Text View",rs.getDrawable(R.drawable.ic_launcher)).setContent(R.id.textView1));
th.addTab(th.newTabSpec("third").setIndicator("Rating Bar",rs.getDrawable(R.drawable.ic_launcher)).setContent(R.id.ratingBar1));
}

}


Download full project from here




Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

No comments:

Post a Comment