Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6828783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:25:25+00:00 2026-05-26T22:25:25+00:00

I have to develop an android view such that i have 2 spinner controls

  • 0

I have to develop an android view such that i have 2 spinner controls in it, one for state and the second for cities.

My question is, how can I populate the city spinner automatically whenever a state is selected?

What’s the logic behind it?

My string.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="spinner_category"></string>
<string-array name="category_state">
    <item >kerala</item>
    <item >tamil nadu</item>
    <item >Andra Pradesh</item>
    <item >karnataka</item>
</string-array>
</resources>

My main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:text="Select : "/>
        <Spinner android:layout_width="250dip"
            android:layout_height="wrap_content"
            android:id="@+id/spinner_state"/>
    </LinearLayout>

<LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:text="Select : "/>
        <Spinner android:layout_width="250dip"
            android:layout_height="wrap_content"
            android:id="@+id/spinner_state"/>
    </LinearLayout>
</LinearLayout>

And my activity.java file:

package com.converter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class ConverterActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner spinner_s = (Spinner)findViewById(R.id.spinner_state);
        ArrayAdapter<CharSequence> category_adapter = ArrayAdapter.createFromResource(
                this, R.array.category_array, android.R.layout.simple_spinner_item);
        category_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_s.setAdapter(category_adapter);

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:25:25+00:00Added an answer on May 26, 2026 at 10:25 pm

    Here is the correct way dear….

    I had wrote all the needed states and cities in string_arays….

    like

    <string-array name="State_array">
                <item >s1</item>
                <item >s2</item>
                <item >s3</item>
                <item >s4</item>
        </string-array>
    //then cities array for each states, like 
    <string-array name="State1Cities_array">
                <item >c11</item>
                <item >c12</item>
                <item >c15</item>
                <item >c13</item>
        </string-array>
    <string-array name="State2Cities_array">
                <item >c1</item>
                <item >c2</item>
                <item >c5</item>
                <item >c3</item>
        </string-array>
    
    // and so on for all the states
    

    then in main xml added 2 spinners for both. i belive all of u can do i simple, na?

    then i have my main.xml as…

    spinner_states_activity = (Spinner)findViewById(R.id.spinner_states_main);
            spinner_states_activity.setOnItemSelectedListener(this);
            adapter = ArrayAdapter.createFromResource(
                    this, R.array.state_array, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(R.layout.myspinner);
    // my layout for spinners u can use urs or defalut. k?
            spinner_states_activity.setAdapter(adapter);
    
    spinner_cities_activity = (Spinner)findViewById(R.id.spinner_cities_main);
            spinner_cities_activity.setOnItemSelectedListener(this);
    
    //and in function onItemSelected
    
            int pos,pos2;
            pos = spinner_states_activity.getSelectedItemPosition();
            int iden=parent.getId();
            if(iden==R.id.spinner_states_main)
            {
                pos2 = 0;
                switch (pos) 
                {
                    case 0: unit_adapter= ArrayAdapter.createFromResource(
                                this, R.array.States1Cities_array, android.R.layout.simple_spinner_item);
                            break;
                    case 1: unit_adapter= ArrayAdapter.createFromResource(
                                this, R.array.States3Cities_array, android.R.layout.simple_spinner_item);
                            break;
                            // all the StatesxCities entires....
                    default:
                            break;
                }
    
                spinner_cities_activity.setAdapter(unit_adapter);
                unit_adapter.setDropDownViewResource(R.layout.myspinner);
            }
    

    Just check a look and do it by yourself…
    Hope this will helps u a little…

    k dear friends..
    Sujith

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let say that I have two Activity to develop in Android. Upon the end
For my schoolproject I have to develop an Android app that continuously tracks the
Android view is in xml file, and we can drag n drop controls to
I have to develop an Android application in that when the user just enters
Hi i have develop and deploy one multilingual (english uk and swedish) website at
I have to develop an application using C#.net that has to be run once
I have to develop software for a USB scale that, when you press a
I have to develop a tool in C# that retrieves some data from an
I will have to develop some android applications. Witch mobile phone is better for
I occur a problem when develop a application on android. There have two image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.