Friday, 22 March 2013

How to create tab layout

Tablayout demo will exaplains about how to set tabs on your application and operations perform on  tabs by  clicking on the tab buttns .when click on the tab button like Home ,cattagirious,aboutus ....., like the way we have the actual code for the tab layout view and i think this should useful to your applicaions and now these days multiple tabs is used for good layout design .
  Below code could be useful:
write this on your main activity:
package com.tab.demo;

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

public class TabLayoutDemoActivity extends TabActivity {
TabHost th;
TabHost.TabSpec spec;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();

// step 3:

th = getTabHost();
// create Tabspec

spec = th.newTabSpec("A1")
.setIndicator("phone", res.getDrawable(R.drawable.ic_launcher))
.setContent(new Intent(this, Phoneclass.class));
// add tab 
th.addTab(spec);
///*************** new tab********************
spec = th.newTabSpec("A2")
.setIndicator("calllogs", res.getDrawable(R.drawable.ic_launcher))
.setContent(new Intent(this, calllogs.class));
// add tab 
th.addTab(spec);
///************* new tab***********************
spec = th.newTabSpec("A3")
.setIndicator("Contacts", res.getDrawable(R.drawable.ic_launcher))
.setContent(new Intent(this, Contactsclass.class));
// add tab 
th.addTab(spec);
}}
// here we created three classes for the tabs which we are used for:
//examples for threee classes are phone,callogs,Contacts
//phone class:

package com.tab.demo; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class Phoneclass extends Activity implements OnClickListener { EditText ed1, ed2; Button b; String name,pass; SharedPreferences sh; SharedPreferences.Editor ed; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.first); ed1 = (EditText) findViewById(R.id.editText1); ed2 = (EditText) findViewById(R.id.editText2); b = (Button) findViewById(R.id.button1); sh=this.getSharedPreferences("Mypreff", MODE_WORLD_WRITEABLE); // create editor ed=sh.edit(); b.setOnClickListener(this); } public void onClick(View v) { // TODO Auto-generated method stub name=ed1.getText().toString(); pass=ed2.getText().toString(); // insert data ed.putString("peru", name); ed.putString("lock", pass); // dont forget to commit ed.commit(); } }
// callogs class:
package com.tab.demo; import java.util.ArrayList; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.ListView; public class calllogs extends Activity{ SharedPreferences sh; ListView lv; String n,p; ArrayList<String> s=new ArrayList<String>(); ArrayAdapter<String> ad; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.third); lv=(ListView)findViewById(R.id.listView1); sh=this.getSharedPreferences("Mypreff", MODE_WORLD_READABLE); n=sh.getString("peru", null); p=sh.getString("lock",null); s.add(n); s.add(p); System.out.println(s); ad=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,s); lv.setAdapter(ad); } }
//Contacts class:
package com.tab.demo; import android.app.Activity; import android.os.Bundle; public class Contactsclass extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.second); } }
//layouts of the three class respectively:
//phone:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#666666" > <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" > <requestFocus /> </EditText> <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
//callogs:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#cc5566">

    

</LinearLayout>

//Contact class:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#d2ff66" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>


//and the layout of the main .xml is :
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/linearLayout1"
            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="match_parent" >
                </LinearLayout>

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

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

</LinearLayout>
//***Register the  three classes in manifest .xml***
the Result will be the shown below:








No comments:

Post a Comment