Autocomplete Text view is used to get suggestion on screen.Like google provide suggestion for searching anything.
< ?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">
< AutoCompleteTextView
android:id="@+id/autodata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search Name"
android:padding="20dp" />
< /LinearLayout>
package example.com.uidemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
public class AutoCompleteDemo extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_complete_demo);
String names[]=getResources().getStringArray(R.array.names);
AutoCompleteTextView at=findViewById(R.id.autodata);
at.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,names));
at.setThreshold(1);
at.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int i, long l) {
Toast.makeText(AutoCompleteDemo.this, adapterView.getItemAtPosition(i).toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
< resources>
< string name="app_name">UiDemo
< string-array name="names">
< item>Select Version
< item>Alpha
< item>Beta
< item>Cupcake
< item>Donut
< item>Eclair
< item>Froyo
< item>Gingerbread
< item>Honeycomb
< item>Ice Cream Sandwich
< item>Jelly Bean
< item>KitKat
< item>Lollipop
< item>Marshmallow
< item>Nougat
< item>Oreo
< item>Pie
< /string-array>
< /resources>
No comments:
Post a Comment