Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.