Попробуйте этот код:
MainActivity. java
import android.arch.lifecycle.AndroidViewModel;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// By ID get the AutoCompleteTextView
// which id is assign in xml file
AutoCompleteTextView
autoCompleteTextView
= (AutoCompleteTextView)
findViewById(
R.id.autocompleteTextView);
// Create the string array
// and store the values.
String[] colors
= { "Red", "Green", "Black",
"Orange", "Blue", "Pink",
"Blush", "Brown", "Yellow" };
// Create the object of ArrayAdapter with String
// which hold the data as the list item.
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(
this,
android.R.layout.select_dialog_item,
colors);
// Give the suggestion after 1 words.
autoCompleteTextView.setThreshold(1);
// Set the adapter for data as a list
autoCompleteTextView.setAdapter(adapter);
autoCompleteTextView.setTextColor(Color.BLACK);
}
}
activity_main. xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="80dp"
android:text="Write the color name !"
android:textSize="20dp"
android:textStyle="bold" />
<AutoCompleteTextView
android:id="@+id/autocompleteTextView"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:layout_marginLeft="90dp"
/>
</RelativeLayout>
Подробнее см. Здесь: https://www.geeksforgeeks.org/android-auto-complete-textbox-and-how-to-create-it/
Если вы хотите получить данные, которые сохранены в общих настройках, используйте этот код:
val j=adapter.count
var i=0
while (i<j){
Log.d("abc",adapter.getItem(i))
i++}