<?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>
Buat file java dengan nama "AutoComplete.java"
package com.example.contoh;
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 AutoComplete 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) {
}
}
Buka file "strings.xml" pada folder res>values
tambahkan perintah
<string name="perintah">Masukkan minimal 3 huruf</string>
Terakhir, buka file AndroidManifest.xml
tambahkan
<activity
android:name="com.example.contoh.AutoComplete"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Berikut tampilannya

Tidak ada komentar:
Posting Komentar