Membuat Menu Tab Android Studio

Hari sabtu, untuk mengisi hari libur kerja saya akan sharing cara membuat Tab menu di android studio, Ok Langsung saja buka android studio dan buat Project baru dengan TabMenu untuk Activity-nya Pilih yang Empty Activity.
Selanjutnya pada activity_main.xml ganti file xml dengan xml berikut :

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"    
android:id="@android:id/tabhost"    
android:layout_width="fill_parent"   
android:layout_height="fill_parent" >

    <LinearLayout        
        android:layout_width="fill_parent"        
        android:layout_height="fill_parent"        
        android:orientation="vertical" >

        <TabWidget            
            android:id="@android:id/tabs"            
            android:layout_width="fill_parent"            
            android:layout_height="wrap_content"           
            android:layout_gravity="bottom" />

        <FrameLayout
            android:id="@android:id/tabcontent"           
            android:layout_width="fill_parent"            
            android:layout_height="fill_parent"            
            android:layout_gravity="bottom" />

    </LinearLayout>
</TabHost>


Terus tambahkan Empty Activity Baru dengan nama BerandaActivity kemudian pada activity_beranda.xml nya ganti dengan xml dibawah ini :


<?xml version="1.0" encoding="utf-8"?>
<DigitalClock xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="fill_parent"    
android:layout_height="fill_parent"  
android:gravity="center_vertical|center" 
android:textSize="50sp" >
</DigitalClock>

Terus tambahkan Empty Activity Baru dengan nama BeritaActivity dan ganti xmlnya dengan xml dibawah ini :

<?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"    >
<ListView  
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"        />
</LinearLayout> 
Terus tambahkan Empty Activity Baru dengan nama TemanActivity dan ganti xmlnya dengan xml dibawah ini :

<?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"    >  
<ListView        
   android:id="@android:id/list"        
   android:layout_width="fill_parent"        
   android:layout_height="fill_parent"        />
</LinearLayout>

Terus tambahkan Empty Activity Baru dengan nama ChatActivity dan ganti xmlnya dengan xml dibawah ini :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent"    
    android:orientation="vertical"    >
    <RelativeLayout        
        android:layout_width="match_parent"        
        android:layout_height="400dp"        
        android:layout_alignParentTop="true"        
        android:layout_alignParentStart="true">
        <ListView           
        android:layout_width="wrap_content"        
        android:layout_height="wrap_content"          
        android:id="@+id/listView"            
        android:layout_centerVertical="true"            
        android:layout_centerHorizontal="true" />
    </RelativeLayout>

    <RelativeLayout        
       android:layout_width="match_parent"        
       android:layout_height="100dp"        
       android:layout_alignParentBottom="true"        
       android:layout_centerHorizontal="true">
        
    <Button            
       style="?android:attr/buttonStyleSmall"            
       android:layout_width="wrap_content"            
       android:layout_height="wrap_content"            
       android:text="Send"            
       android:id="@+id/button"            
       android:layout_centerVertical="true"            
       android:layout_alignParentEnd="true" />
    <EditText            
       android:layout_width="wrap_content"            
       android:layout_height="wrap_content"            
       android:id="@+id/editText"            
       android:layout_alignBottom="@+id/button"            
       android:layout_alignParentStart="true"            
       android:layout_toStartOf="@+id/button" />
    </RelativeLayout>
</RelativeLayout>

Kemudian bukan MainActivity.java ganti source code menjadi seperti dibawah ini :
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabHost tabhost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        intent = new Intent().setClass(this, BerandaActivity.class);//content pada tab yang akan kita buat        
        spec = tabhost.newTabSpec("beranda").setIndicator("Beranda",null).setContent(intent);       
        tabhost.addTab(spec);//untuk membuat tabbaru disini bisa diatur sesuai keinginan anda
        intent = new Intent().setClass(this, BeritaActivity.class);
        spec = tabhost.newTabSpec("berita").setIndicator("Berita",null).setContent(intent);
        tabhost.addTab(spec);

        intent = new Intent().setClass(this, TemanActivity.class);
        spec = tabhost.newTabSpec("teman").setIndicator("Teman",null).setContent(intent);
        tabhost.addTab(spec);

        intent = new Intent().setClass(this, ChatActivity.class);
        spec = tabhost.newTabSpec("chat").setIndicator("Chat",null).setContent(intent);
        tabhost.addTab(spec);
    }
}

Ganti source code BerandaActivity.java dengan source code dibawah ini :

public class BerandaActivity extends Activity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_beranda);
    }
}
untuk source code BeritaActivity.java ganti menjadi :
public class BeritaActivity extends ListActivity {

    String [] berita ={"Jadwal Piala Dunia 2014", "Capres Indonesia", "Debat Capres", "Debat Cawapres"};

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_berita);

        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, berita));
    }
}
untuk source code TemanActivity.java ganti menjadi :
public class TemanActivity extends ListActivity {

    String [] teman ={"Andra", "Dina", "Edo", "Julia"};

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teman);

        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, teman));

    }
}
untuk source code ChatActivity.java ganti menjadi :

public class ChatActivity extends Activity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
    }
}

Jika sudah semua seperti diatas, jalankan programnya dan lihat hasilnya.... :D
Semoga tulisan ini bermanfaat..

Comments

Popular posts from this blog

IT Asset Management Dengan PHP MySQL

PHP MySql CRUD Dengan Konsep MVC

Cara Sederhana Multi Insert Data Dengan PHP - MySQL