Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.9 kB
39
Indexable
Never
package com.vrushabhhirap.it9_spinneranautocomplete;

import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.SimpleAdapter;
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;
    AutoCompleteTextView autoCompleteTextView;
    ArrayList <String>cites;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        spinner = findViewById(R.id.spinner);
        autoCompleteTextView = findViewById(R.id.autocomplete);

        cites = new ArrayList<String>();


        cites.add("Cities");

        cites.add("jalna");
        cites.add("mumbai");

        ArrayAdapter<String> citiesAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, cites);
        spinner.setAdapter(citiesAdapter);

        autoCompleteTextView.setAdapter(citiesAdapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, cites.get(position), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(MainActivity.this, "Nothing Selected!", Toast.LENGTH_SHORT).show();

            }
        });

    }
}
Leave a Comment