Day07 Spinner AutoCompleteTextVIew

mail@pastecode.io avatar
unknown
java
a month ago
2.2 kB
18
Indexable
Never
package sumago.androidipt.day07b1spinnerautocompletetextview;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    Spinner spinner;
    ArrayList<String> citiesList;

    AutoCompleteTextView etAutoComplete;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner=findViewById(R.id.spinner);
        etAutoComplete=findViewById(R.id.etAutoComplete);
        citiesList=new ArrayList<>();
        citiesList.add("Nashik");
        citiesList.add("Mumbai");
        citiesList.add("PUNE");
        citiesList.add("Nagpur");
        citiesList.add("Nanded");
        String[] names=new String[]{"Sachin","Sachin","Sachin","Sachin"};
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,citiesList);
        spinner.setAdapter(arrayAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(MainActivity.this, ""+citiesList.get(position), Toast.LENGTH_SHORT).show();
                //Toast.makeText(MainActivity.this, ""+names[position], Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

                Log.d("mytag","onNothingSelected");
                Toast.makeText(MainActivity.this, "onNothingSelected", Toast.LENGTH_SHORT).show();

            }
        });
        etAutoComplete.setAdapter(arrayAdapter);
    }
}
Leave a Comment