Widget Auto Complete merupakan widget pada android yang berfungsi untuk pencarian data dengan mengetikkan inisial, seperti contohnya saat pencarian kontak telepon. Untuk membuatnya dibutuhkan file java serta file xml.
File Java (MainActivity.java) :
****
package wulan.novi.autocomplete;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextWatcher;
public class MainActivity extends Activity implements TextWatcher {
TextView selection;
AutoCompleteTextView edit;
String[] pilihan={ "wulan","wulan2","wulan3","wulannn","wulan5","wulannovi" };
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
selection=(TextView) findViewById(R.id.selection);
edit=(AutoCompleteTextView) findViewById(R.id.edit);
edit.addTextChangedListener(this);
edit.setAdapter(new ArrayAdapter<String>(
this, android.R.layout.simple_dropdown_item_1line,pilihan));
}
public void onTextChanged(CharSequence s, int start, int before, int count){
selection.setText(edit.getText());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void afterTextChanged(Editable s){
}
}
****
File XML :
****
<?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" >
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0033cc"
android:textSize="14pt"
android:textStyle="bold" />
<AutoCompleteTextView
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="3"
android:textColor="#000000" />
</LinearLayout>
****
Contoh hasil running program :
No comments:
Post a Comment