ListLayouts for Android Apps

ListView containing icons, images and text

ListView containing icons, images and text

Since the ‘Fitness Manager’ App is an app for organizing and scheduling the personal workout, I needed to implement powerful ListViews, which provide the user with helpful information and statues. Still the ListViews must be kept clear. And that is the way how I put my requirements into Java code:

  1. The layout.xml of the activity containing the ListView is very simple.

    Layout definition of ListView

    Layout definition of ListView

  2. In order to realize my design concept for the ListView, I had to insert a layout.xml and a Java class file, where I did define the elements of the single list items of the ListView.

    Layout definition of ListView item

    Layout definition of ListView item

    As displayed in the code snippet of the layout.xml I did use a RelativeLayout with an id and a background image. On the left side of the parent layout I did insert an ImageView. To the right side of the ImageView I did define two TextViews with different text styles. On the right side of the parent layout I did add another ImageView. The values of all elements in the ListView items are dynamic except of the last ImageView. The source for this ImageView is already set in the layout.xml and received from the rescources.

    In the Android Studio Designer the XML layout definition of the ListView items is finally displayed like this:

    Layout definition of ListView item displayed in Android Studio designer

    Layout definition of ListView item displayed in Android Studio designer

    The Java class for a ListView item contains variables, constructors, getter and setter functions for the dynamic elements. Whatever is needed for the ListView item.

    Java class for ListView item definition

    Java class for ListView item definition

  1. In Android every ListView needs an adapter. If you just want to display a simple ListView with ListView items only containing one simple String value, you just need to define a simple ArrayAdapter in the onCreate function of your activity. But if you want to use your own design for the items of the ListView, you also have to define a separate adapter in a Java class file extending the class ArrayAdapter.

    Definition of adapter for ListView

    Definition of adapter for ListView

    The adapter class needs to contain a constructor and the methods getItemId and getView. In the getView funtion the elements of the ListView item with dynamic values are set. First an LayoutInflater must be created in order to get the view. The values for the ListView elements are retrieved from the ArrayList and assigned to the ImageView and TextViews by using the corresponding setter functions of the Java class for the ListView elements.

  1. At least the ListView and the adapter have to be instantiated and assigned in the onCreate function of the activiy.

    Initialization of ListView and corresponding adapter

    Initialization of ListView and corresponding adapter

    After initializing the ListView, an ArrayList is defined and filled with the required data and bitmaps in a for loop. Then the adapter has to be defined and assigned to ListView, which then gets registered the context menu. The implementation of the function setOnItemClickListener of the ListView needs to be handled in the onCreate method of the activity as well.

  1. Most often the appearance of the ListView changes while the activity is running. There are several examples like a new element is added to the ListView, an existing element of the ListView is deleted or a text in one of the TextViews can be changed by another foreground activiy. Then the ListView must be reloaded. This is handled in the onResume function of the activiy.

    Reload of ListView and corresponding adapter

    Reload of ListView and corresponding adapter

    The implementation of the onResume method of the activity is similar to the implementation of the onCreate function. The ArrayList is defined and filled with the required data and bitmaps again. The global variables of the ListView and the adapter are not defined again here, instead the adapter is cleared and the elements of the ListView are added one by one. The call of the function notifyDataSetChanged at the end notifies the attached observers that the underlying data has been changed and any view reflecting data set should refresh itself.

This kind of ListView designs can of course also be used for other types of ListViews like the ExpandableListView.

ExpandableListView

ExpandableListView

I hope you did enjoy reading my Blog about the implementation of ListView Layouts for Android devices.

If you have any questions, any improvement suggestions for the implementation (I mentioned that this is my first Android App) or any improvement suggestions for my English, I really would appreciate if you can leave me a comment.

And I would even more appreciate if you like to check out the ‘Fitness Manager’ App in the Google Play Store or the ‘Fitness Manager’ App in the iTunes Store. See you.

Follow us on
Facebook
Twitter
Google Plus

Leave a Reply

Your email address will not be published. Required fields are marked *