Today I'm gonna talk about one of the beautiful features of Android which gives you the ability to change the default behavior of GUI components.
when designing GUIs, most of the times you want to change the appearance of buttons, input Fields, menus and.... Android Selectors have been provided to solve all these kind of problems, they enable us to decide what to show and how to show based on different states of each components...for example you can tell a button to have black background color with red text color when it is in pressed state or whatever else.
In this post i will show you an example of customizing a ListView which is gonna look like this :
It is nothing but a simple ListView... believe me, and here is the XML which is being used to create it :
the code which I've used to populate the list :
and finally, here is the content of menu_item.xml file :
see? it's a simple, ordinary list, there is no secret here but a simple trick; I've used selectors for both background and text color for our TextView, what do you think "selector" and "color_selector" are?
they actually refer to selector.xml and color_selector.xml files inside drawable directory, you can see the content of them below :
what does the content of color_selector file mean? it says that the text color will be black in "selected" state, red in "pressed" state and white otherwise, and i reckon now you should be able to figure out what the content of selector file means.
here is the content of selector_s and selector_d :
as you might have noticed,I've also used "listSelector" attribute of our ListView to customize the behavior of the list when user is going through options in the list.
list_selector.xml file looks like this :
and here are all the drawable objects i used in this application if you wanna give it a try and see how easy it works ;)