Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

How to create selector elements in android programming.

I am working on a sign up activity in my android application. I wanna include a selector were a user can select their "gender" and also that were a user can select their "date of birth" and also another selector dialog to select their "country"

How can do that?

1 Answer

Abdalla Ali
Abdalla Ali
2,972 Points

Hi Steve,

You can use Spinner from graphical layout , first you create data in strings.xml file as a type of string-array, and then you create an adapter same like you do for normal listview but instead of using simple_list_item_1 you use simple_spinner_item

Here is a code snippet:

The following code is for string-array which contain the items that will be shown inside the spinner, you can add this to Strings.xml file

<string-array name="country_list">
        <item name="United States">United States</item>
        <item name="England">England</item>
        <item name="Brazil">Brazil</item>
    </string-array>

The following code is for JAVA class

Spinner countryNames = (Spinner) findViewById(R.id.spinner);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(activityName.this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.country_list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countryNames.setAdapter(adapter);

Hope this helps. Abdalla.