Welcome to the Treehouse Community
Looking to learn something new?
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?
Abdalla Ali2,972 Points
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.