Recently we had a requirement where we have to display data in a list in android. Every row in the list has multiple TextView items. It means, we wanted an output that is like this!
Requirement:
- The list items should have space division between them.
- The default color of the listview item must be colored in gray+blue mix.
- On touch/click of the item, it should highlight it as green.
- Every item in the list has 3 elements (Header, content/information and datetime when the content was received)
So, how did I do it?
- To achieve the colors needed for the project, that is default -gray and on hover - green, we did this
Add: Inside res/drawable, create a new resource file with the following name generic_list_row_selector.xml
Add the following code to it.
2nd, Add: gradient_background.xml which has the below code
3rd, Add gradient_background_hover.xml with the below code snippet in it
- Open the main activity file's designer -> in my case it is MainActivity and its related designer calss is acitivity_main
Add the following code to it. This has the ListView that will show the list of items that we bind
- Now we need the Row that has the TextView elements that displays the data for us. Create a new res file named activity_custom_row_for_listview.xml with the following code in it.
The data that will be bind to the controls - TextView will be from an object holder. So, create a java class named DisplayDataClass.java
Now, create a custom Adaptor, that will make sure to bind the controls with the data as the data is fed in and rebinds it with each item (row) of the listview. Create a file named CustomAdaptor.java
Comeback to the parent class now. It should have the below code
The method private ArrayList<DisplayDataClass> GetDisplayResults() inside the class MainActivity is the data object. You can plug in your own data and show it here. It might be from SQLite database or from an API or from GCM or from another Activity inside your android app.
Hope, you have liked my tutorial. Stay tuned! there is a lot to come. - Eshwar
0 comments:
Post a Comment