Minggu, 12 April 2015

Materi Pertemuan 9 (Widget2)

Buat Splash Screen

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:background="#ffffff00">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Splash Screen" />
</LinearLayout>
Java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Thread background = new Thread() {
            public void run() {
                try {
                    sleep(3*1000);
                    Intent i=new Intent(getBaseContext(),TabActivity.class);
                    startActivity(i);
                    
                    finish();
                } catch (Exception e) {}
            }
        };
        
        background.start();

    }
   
    protected void onDestroy() {
        super.onDestroy();        
    }   

}

Buat Tab
Xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabHost1">
    <TabWidget
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/tabs">       
    </TabWidget>
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/tabcontent">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tab1"
            android:orientation="vertical"
            android:paddingTop="60px">
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/gambar">               
            </ImageView>           
        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tab2"
            android:orientation="vertical"
            android:paddingTop="60px">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="100px"
                android:text="this is tab 2"
                android:id="@+id/txt2">               
            </TextView>                           
        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tab3"
            android:orientation="vertical"
            android:paddingTop="60px">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="100px"
                android:text="this is tab 3"
                android:id="@+id/txt3">               
            </TextView>                           
        </LinearLayout>       
    </FrameLayout>
</TabHost>   

Java
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TabActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);
        TabHost tabHost = (TabHost) findViewById(R.id.tabHost1);
        tabHost.setup();       
        TabSpec tabSpec1 = tabHost.newTabSpec("Tab 1");
        tabSpec1.setContent(R.id.tab1);
        tabSpec1.setIndicator("Tab 1");
        TabSpec tabSpec2 = tabHost.newTabSpec("Tab 2");
        tabSpec2.setContent(R.id.tab2);
        tabSpec2.setIndicator("Tab 2");
        TabSpec tabSpec3 = tabHost.newTabSpec("Tab 3");
        tabSpec3.setContent(R.id.tab3);
        tabSpec3.setIndicator("Tab 3");
        tabHost.addTab(tabSpec1);
        tabHost.addTab(tabSpec2);
        tabHost.addTab(tabSpec3);
    }
}

Buat Spinner
Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:id="@+id/selection"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <Spinner android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
       
    />
</LinearLayout>



Java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;


public class SpinnerActivity extends Activity implements OnItemSelectedListener {
    TextView selection;   
    String[] items={"Aceh", "Sumatera Utara", "Sumatera Barat", "Riau", "Jambi",
            "Sumatera Selatan", "Bengkulu", "Lampung", "Bangka Belitung", "Kepulauan Riau",
            "Jakarta", "Jawa Barat", "Jawa Tengah", "Yogyakarta", "JawaTimur",
            "Banten", "Bali", "Nusa Tenggara Barat", "Nusa Tenggara Timur", "Kalimantan Barat",
            "Kalimantan Tengah", "Kalimantan Selatan", "Kalimantan Timur", "Sulawesi Utara",
            "Sulawesi Tengah","SulawesiSelatan","Sulawesi Tenggara","Gorontalo","Sulawesi Barat",
            "Maluku","MalukuUtara","PapuaBarat","Papua"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);
        selection = (TextView) findViewById(R.id.selection);
        Spinner spin = (Spinner) findViewById(R.id.spinner);
        spin.setOnItemSelectedListener(this);
        ArrayAdapter<String>aa = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,items);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(aa);
    }

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        selection.setText("Pilihan Anda: "+items[position]);
    }

    public void onNothingSelected(AdapterView<?> parent) {
        selection.setText("Search ...");
    }
}

Buat Autocomplete
Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:text="@string/perintah">       
    </TextView>
    <AutoCompleteTextView android:id="@+id/edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:completionThreshold="3" />
    <TextView android:layout_width="fill_parent"
        android:text="TextView"
        android:layout_height="wrap_content"
        android:id="@+id/hasil">           
    </TextView>
</LinearLayout>


Java
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

public class AutoCompleteActivity extends Activity implements TextWatcher{
    TextView hasil;
    AutoCompleteTextView edit;
    String[] item = { "Merbabu", "Merapi", "Lawu", "Rinjani","Sumbing","Sindoro", "Krakatau", "Selat Sunda", "SelatBali","Selat Malaka","Kalimantan", "Sulawesi", "Jawa" };
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_complete);
        hasil = (TextView) findViewById(R.id.hasil);
        edit = (AutoCompleteTextView) findViewById(R.id.edit);
        edit.addTextChangedListener(this);
        edit.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, item));
        }
   
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        hasil.setText(edit.getText());           
    }
   
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {       
    }
   
    public void afterTextChanged(Editable s) {       
    }
}

Tidak ada komentar:

Posting Komentar