AutoCompleteTextView in Android
In this blog i want tell you how we can create AutoCompleteTextView in android
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="city name "
android:textSize="20sp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
/>
<AutoCompleteTextView
android:id="@+id/my_autocomplte_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:hint="search you city name here "
/>
</LinearLayout>
MainActivity.java
package com.example.myfirstapp;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private AutoCompleteTextView autoCompleteTextView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.auto_complete_textview);
autoCompleteTextView=findViewById(R.id.my_autocomplte_textview);
String[] cityName=getResources().getStringArray(R.array.city_name);
ArrayAdapter<String> adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,cityName);
autoCompleteTextView.setAdapter(adapter);
autoCompleteTextView.setThreshold(1);
}
}
Output
Post a Comment