Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.5 kB
15
Indexable
Never
package com.vrushabhhirap.day10homeworkit;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Spinner stateSpinner, citySpinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        stateSpinner = findViewById(R.id.State_spinner);
        citySpinner = findViewById(R.id.city_spinner);

        // States array
        String[] states = {"Maharashtra", "Karnataka", "Gujarat"};

        // Cities array for each state
        String[][] cities = {
                {"Mumbai", "Pune", "Nagpur", "Nashik", "Aurangabad"},
                {"Bangalore", "Mysore", "Mangalore"},
                {"Ahmedabad", "Surat", "Vadodara"}
        };

        // Set up state spinner
        ArrayAdapter<String> stateAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, states);
        stateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        stateSpinner.setAdapter(stateAdapter);

        // Set up listener for state spinner
        stateSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // Populate the city spinner based on selected state
                ArrayAdapter<String> cityAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item, cities[position]);
                cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                citySpinner.setAdapter(cityAdapter);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Optionally, you can handle this case
                citySpinner.setAdapter(null);
            }
        });
    }
}


//e mart , tripple Shop , people shop , trio mart,

//e mart , tripple Shop , people shop , trio mart,
//        fadding = AnimationUtils.loadAnimation(this,R.anim.fadding);
//        ex = findViewById(R.id.ex);
//        ex.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ex.startAnimation(fadding);
//            }
//        });
Leave a Comment