Untitled

mail@pastecode.io avatar
unknown
java
a month ago
2.3 kB
64
Indexable
Never
package sumago.androidipt.day07b2spinnerautocompletetextview;

import android.os.Bundle;
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> cityList;

    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);

        cityList=new ArrayList<>();
        cityList.add("Nashik");
        cityList.add("Pune");
        cityList.add("Mumbai");
        cityList.add("Nanded");
        cityList.add("Nagpur");

        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,cityList);
        spinner.setAdapter(arrayAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(MainActivity.this, ""+cityList.get(position), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        ArrayList<String> countryList=new ArrayList<>();
        countryList.add("India");
        countryList.add("China");
        countryList.add("USA");
        countryList.add("UK");
        countryList.add("Russia");

        ArrayAdapter<String> countryAdapter=new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,countryList);
        etAutoComplete.setAdapter(countryAdapter);
    }
}
Leave a Comment