Jumat, 09 Januari 2015

Tab Host



TabHost


Buat layout seperti ini

Desain code activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffffff"
    android:orientation="vertical"
    android:padding="2dp"
    tools:context="com.duwibayupratomo.tabhostdemo.MainActivity"
    tools:ignore="MergeRootFrame" >

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

        <LinearLayout
            android1:layout_width="match_parent"
            android1:layout_height="match_parent"
            android1:orientation="vertical" >

            <TabWidget
                    
                android1:id="@android:id/tabs"
                android1:layout_width="match_parent"
                android1:layout_height="wrap_content" >
            </TabWidget>
                          
                
            <FrameLayout
                android1:id="@android:id/tabcontent"
                android1:layout_width="match_parent"
                android1:layout_height="match_parent" >
                <include layout="@layout/main_tab1" />
                           <include layout="@layout/main_tab2" />

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

</LinearLayout>
Desain pada main_tab1.xml
<?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:id="@+id/tab1" >

    <AnalogClock
        android:id="@+id/tab1Clock"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Desain pada main_tab2.xml
<?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:id="@+id/tab2" >
   
    <TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0000ff"
android:text=" ACME Login Screen"
android:textColor="@android:color/white"
android:textSize="20sp"  />
<EditText
android:id="@+id/tab2TxtPerson"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter person Name"
android:inputType="textCapWords"
android:textSize="18sp" />
<Button android:id="@+id/tab2BtnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Go " />
   

</LinearLayout>

Dan berikut ini pada MainActivity.java
package com.duwibayupratomo.tabhostdemo;

import java.util.Date;


import android.support.v4.app.Fragment;
import android.app.Activity;
import android.app.ActionBar;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.view.ViewGroup;
import android.widget.AnalogClock;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends Activity implements OnClickListener{
      
       TabHost tabhost;

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

              //if (savedInstanceState == null) {
                     //getFragmentManager().beginTransaction()
                     //.add(R.id.container, new PlaceholderFragment()).commit();
              //}
              final AnalogClock clock1 = (AnalogClock) findViewById(R.id.tab1Clock);
              final Button btnGo = (Button) findViewById(R.id.tab2BtnGo);
              final EditText txtPerson = (EditText) findViewById(R.id.tab2TxtPerson);
             
             
              tabhost = (TabHost) findViewById(android.R.id.tabhost);
              tabhost.setup();
              TabHost.TabSpec tabspec;
              tabspec = tabhost.newTabSpec("screen1");
              tabspec.setContent(R.id.tab1);
              tabspec.setIndicator("1-Clock", null);
              tabhost.addTab(tabspec);
              tabspec = tabhost.newTabSpec("screen2");
              tabspec.setContent(R.id.tab2);
              tabspec.setIndicator("2-Login",
              getResources().getDrawable(R.drawable.pic_2));
              tabhost.addTab(tabspec);
              tabhost.setCurrentTab(0);
             
              btnGo.setOnClickListener(new OnClickListener() {
                     public void onClick(View v) {
                     String theUser = txtPerson.getText().toString();
                     txtPerson.setText("Hola " + theUser + " \n" + new Date());
                     hideVirtualKeyboard();
                     }
                     });
       tabhost.setOnTabChangedListener(new OnTabChangeListener() {
              @Override
              public void onTabChanged(String tagId) {
              // do something useful with the selected screen
              String text = "Im currently on: " + tagId + "\nindex: "
              + tabhost.getCurrentTab();
              switch (tabhost.getCurrentTab()) {
              case 0: // do something for layout-0
              hideVirtualKeyboard();
              break;
              case 1: // do something for layout-1
              break;
              }
              Toast.makeText(getApplicationContext(), text, 1).show();
              }
              });
}
       public void hideVirtualKeyboard() {
              // temporarily remove the virtual keyboard
              ((InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE))
              .toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
              }
              public void showVirtualKeyboard() {
              // no used – shown for completeness
              ((InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE))
              .toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
              }

       @Override
       public boolean onCreateOptionsMenu(Menu menu) {

              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }

       @Override
       public boolean onOptionsItemSelected(MenuItem item) {
              // Handle action bar item clicks here. The action bar will
              // automatically handle clicks on the Home/Up button, so long
              // as you specify a parent activity in AndroidManifest.xml.
              int id = item.getItemId();
              if (id == R.id.action_settings) {
                     return true;
              }
              return super.onOptionsItemSelected(item);
       }

       /**
        * A placeholder fragment containing a simple view.
        */
       public static class PlaceholderFragment extends Fragment {

              public PlaceholderFragment() {
              }

              @Override
              public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
                     View rootView = inflater.inflate(R.layout.fragment_main, container,
                                  false);
                     return rootView;
              }
       }

       @Override
       public void onClick(View arg0) {
              // TODO Auto-generated method stub
             
       }
      
      

}



Tidak ada komentar:

Posting Komentar