Create Project
Create new project in Android Studio with steps as below:
Step 1: Input Project Name and Select Project Location
Step 2: Select SDK for Android App
Step 3: Select Default Activity for App
Step 4: Finish create project
Add Strings
Open res\values\strings.xml file and add new string as below:
<resources>
<string name="app_name">Learn Android with Real Apps</string>
<string name="search_category">Search Category...</string>
</resources>
Main Activity Layout
Open res\layout\activity_main.xml file and create layout as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextViewCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Main Activity Class
Add code to MainActivity.java file in android.demo.learnandroidwithrealapps package as below:
package android.demo.learnandroidwithrealapps;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private AutoCompleteTextView autoCompleteTextViewCategory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
loadData();
}
private void initView() {
autoCompleteTextViewCategory = findViewById(R.id.autoCompleteTextViewCategory);
}
private void loadData() {
List<String> categories = new ArrayList<String>();
categories.add("Mobile");
categories.add("Computer");
categories.add("Tivi");
categories.add("Laptop");
categories.add("Desktop");
autoCompleteTextViewCategory.setThreshold(1);
autoCompleteTextViewCategory.setHint(getText(R.string.search_category));
ArrayAdapter<String> categoryArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, categories);
autoCompleteTextViewCategory.setAdapter(categoryArrayAdapter);
autoCompleteTextViewCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
autoCompleteTextViewCategory_onItemClick(adapterView, view, i, l);
}
});
}
private void autoCompleteTextViewCategory_onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String category = adapterView.getItemAtPosition(i).toString();
Toast.makeText(getApplicationContext(), category, Toast.LENGTH_SHORT).show();
}
}
Structure of Project
Run App
Load AutoCompleteTextView
Search Category with Keyword
Use ItemClick Event of AutoCompleteTextView