{"id":1401,"date":"2016-02-06T11:20:56","date_gmt":"2016-02-06T11:20:56","guid":{"rendered":"http:\/\/www.mplx.de\/Fremdgesteuert\/?p=1401"},"modified":"2019-09-04T04:43:34","modified_gmt":"2019-09-04T04:43:34","slug":"listlayouts-for-android-apps","status":"publish","type":"post","link":"http:\/\/www.mplx.de\/Fremdgesteuert\/2016\/02\/06\/listlayouts-for-android-apps\/","title":{"rendered":"ListLayouts for Android Apps"},"content":{"rendered":"<div id=\"attachment_1427\" style=\"width: 179px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.mplx.de\/Fremdgesteuert\/2016\/02\/06\/listlayouts-for-android-apps\/listview-icons-images-text\/\" rel=\"attachment wp-att-1427\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1427\" class=\"wp-image-1427 size-medium\" src=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-icons-images-text-169x300.png\" alt=\"ListView containing icons, images and text\" width=\"169\" height=\"300\" srcset=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-icons-images-text-169x300.png 169w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-icons-images-text.png 540w\" sizes=\"auto, (max-width: 169px) 100vw, 169px\" \/><\/a><p id=\"caption-attachment-1427\" class=\"wp-caption-text\">ListView containing icons, images and text<\/p><\/div>\n<p>Since the &#8216;Fitness Manager&#8217; 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:<\/p>\n<ol>\n<li>\n<p align=\"JUSTIFY\">The layout.xml of the activity containing the ListView is very simple.<\/p>\n<p><div id=\"attachment_1431\" style=\"width: 353px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.mplx.de\/Fremdgesteuert\/2016\/02\/06\/listlayouts-for-android-apps\/listview-layout-definition\/\" rel=\"attachment wp-att-1431\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1431\" class=\"wp-image-1431 size-full\" src=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-layout-definition.png\" alt=\"Layout definition of ListView\" width=\"343\" height=\"193\" srcset=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-layout-definition.png 343w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-layout-definition-300x169.png 300w\" sizes=\"auto, (max-width: 343px) 100vw, 343px\" \/><\/a><p id=\"caption-attachment-1431\" class=\"wp-caption-text\">Layout definition of ListView<\/p><\/div><\/li>\n<li>\n<p align=\"JUSTIFY\">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.<\/p>\n<div id=\"attachment_1430\" style=\"width: 558px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.mplx.de\/Fremdgesteuert\/2016\/02\/06\/listlayouts-for-android-apps\/listview-item-layout-definition\/\" rel=\"attachment wp-att-1430\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1430\" class=\"wp-image-1430 size-full\" src=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-item-layout-definition.png\" alt=\"Layout definition of ListView item\" width=\"548\" height=\"719\" srcset=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-item-layout-definition.png 548w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-item-layout-definition-229x300.png 229w\" sizes=\"auto, (max-width: 548px) 100vw, 548px\" \/><\/a><p id=\"caption-attachment-1430\" class=\"wp-caption-text\">Layout definition of ListView item<\/p><\/div>\n<p align=\"JUSTIFY\">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.<\/p>\n<p align=\"JUSTIFY\">In the Android Studio Designer the XML layout definition of the ListView items is finally displayed like this:<\/p>\n<div id=\"attachment_1428\" style=\"width: 167px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.mplx.de\/Fremdgesteuert\/2016\/02\/06\/listlayouts-for-android-apps\/listview-item-android-studio-designer\/\" rel=\"attachment wp-att-1428\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1428\" class=\"wp-image-1428 size-medium\" src=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-item-Android-studio-designer-157x300.png\" alt=\"Layout definition of ListView item displayed in Android Studio designer\" width=\"157\" height=\"300\" srcset=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-item-Android-studio-designer-157x300.png 157w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-item-Android-studio-designer.png 285w\" sizes=\"auto, (max-width: 157px) 100vw, 157px\" \/><\/a><p id=\"caption-attachment-1428\" class=\"wp-caption-text\">Layout definition of ListView item displayed in Android Studio designer<\/p><\/div>\n<p align=\"JUSTIFY\">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.<\/p>\n<p><div id=\"attachment_1429\" style=\"width: 860px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.mplx.de\/Fremdgesteuert\/2016\/02\/06\/listlayouts-for-android-apps\/listview-item-java-class\/\" rel=\"attachment wp-att-1429\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1429\" class=\"wp-image-1429 size-full\" src=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-item-Java-class.png\" alt=\"Java class for ListView item definition\" width=\"850\" height=\"582\" srcset=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-item-Java-class.png 850w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-item-Java-class-300x205.png 300w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-item-Java-class-768x526.png 768w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-item-Java-class-588x403.png 588w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-item-Java-class-600x411.png 600w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/a><p id=\"caption-attachment-1429\" class=\"wp-caption-text\">Java class for ListView item definition<\/p><\/div><\/li>\n<\/ol>\n<ol start=\"3\">\n<li>\n<p align=\"JUSTIFY\">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 <em>onCreate<\/em> 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.<\/p>\n<div id=\"attachment_1426\" style=\"width: 814px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.mplx.de\/Fremdgesteuert\/2016\/02\/06\/listlayouts-for-android-apps\/listview-adapter\/\" rel=\"attachment wp-att-1426\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1426\" class=\"wp-image-1426 size-full\" src=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-adapter.png\" alt=\"Definition of adapter for ListView\" width=\"804\" height=\"783\" srcset=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-adapter.png 804w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-adapter-300x292.png 300w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-adapter-768x748.png 768w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-adapter-588x573.png 588w\" sizes=\"auto, (max-width: 804px) 100vw, 804px\" \/><\/a><p id=\"caption-attachment-1426\" class=\"wp-caption-text\">Definition of adapter for ListView<\/p><\/div>\n<p align=\"JUSTIFY\">The adapter class needs to contain a constructor and the methods <em>getItemId<\/em> and <em>getView<\/em>. In the <em>getView<\/em> 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.<\/p>\n<\/li>\n<\/ol>\n<ol start=\"4\">\n<li>\n<p align=\"JUSTIFY\">At least the ListView and the adapter have to be instantiated and assigned in the <em>onCreate<\/em> function of the activiy.<\/p>\n<div id=\"attachment_1424\" style=\"width: 973px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.mplx.de\/Fremdgesteuert\/2016\/02\/06\/listlayouts-for-android-apps\/listview-activity-oncreate\/\" rel=\"attachment wp-att-1424\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1424\" class=\"wp-image-1424 size-full\" src=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-activity-onCreate.png\" alt=\"Initialization of ListView and corresponding adapter\" width=\"963\" height=\"627\" srcset=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-activity-onCreate.png 963w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-activity-onCreate-300x195.png 300w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-activity-onCreate-768x500.png 768w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-activity-onCreate-588x383.png 588w\" sizes=\"auto, (max-width: 963px) 100vw, 963px\" \/><\/a><p id=\"caption-attachment-1424\" class=\"wp-caption-text\">Initialization of ListView and corresponding adapter<\/p><\/div>\n<p align=\"JUSTIFY\">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 <em>setOnItemClickListener<\/em> of the ListView needs to be handled in the <em>onCreate<\/em> method of the activity as well.<\/p>\n<\/li>\n<\/ol>\n<ol start=\"5\">\n<li>\n<p align=\"JUSTIFY\">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 <em>onResume<\/em> function of the activiy.<\/p>\n<div id=\"attachment_1425\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.mplx.de\/Fremdgesteuert\/2016\/02\/06\/listlayouts-for-android-apps\/listview-activity-onresume\/\" rel=\"attachment wp-att-1425\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1425\" class=\"wp-image-1425 size-medium\" src=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-activity-onResume-300x116.png\" alt=\"Reload of ListView and corresponding adapter\" width=\"300\" height=\"116\" srcset=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-activity-onResume-300x116.png 300w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ListView-activity-onResume.png 370w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1425\" class=\"wp-caption-text\">Reload of ListView and corresponding adapter<\/p><\/div>\n<p align=\"JUSTIFY\">The implementation of the <em>onResume<\/em> method of the activity is similar to the implementation of the <em>onCreate<\/em> 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 <em>notifyDataSetChanged<\/em> at the end notifies the attached observers that the underlying data has been changed and any view reflecting data set should refresh itself.<\/p>\n<\/li>\n<\/ol>\n<p align=\"JUSTIFY\">This kind of ListView designs can of course also be used for other types of ListViews like the ExpandableListView.<\/p>\n<div id=\"attachment_1432\" style=\"width: 179px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.mplx.de\/Fremdgesteuert\/2016\/02\/06\/listlayouts-for-android-apps\/expandablelistview\/\" rel=\"attachment wp-att-1432\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1432\" class=\"wp-image-1432 size-medium\" src=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ExpandableListView-169x300.png\" alt=\"ExpandableListView\" width=\"169\" height=\"300\" srcset=\"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ExpandableListView-169x300.png 169w, http:\/\/www.mplx.de\/Fremdgesteuert\/wp-content\/uploads\/2016\/01\/ExpandableListView.png 540w\" sizes=\"auto, (max-width: 169px) 100vw, 169px\" \/><\/a><p id=\"caption-attachment-1432\" class=\"wp-caption-text\">ExpandableListView<\/p><\/div>\n<p align=\"JUSTIFY\">I hope you did enjoy reading my Blog about the implementation of ListView Layouts for Android devices.<\/p>\n<p align=\"JUSTIFY\">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.<\/p>\n<p align=\"JUSTIFY\">And I would even more appreciate if you like to check out the <a href=\"https:\/\/play.google.com\/store\/apps\/details?id=de.mplx.fitnessmanager&amp;hl=de\">&#8216;Fitness Manager&#8217; App in the Google Play Store<\/a> or the <a href=\"https:\/\/itunes.apple.com\/us\/app\/fitness-manager\/id447600109?l=de&amp;ls=1&amp;mt=8\">&#8216;Fitness Manager&#8217; App in the iTunes Store<\/a>. See you.<\/p>\n<p align=\"JUSTIFY\">Follow us on<br \/>\n<a href=\"https:\/\/www.facebook.com\/Fitness-Manager-106773422758349\">Facebook<\/a><br \/>\n<a href=\"https:\/\/twitter.com\/FitnessManager_\">Twitter<\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/100472904065361948117\">Google Plus<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since the &#8216;Fitness Manager&#8217; 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: The layout.xml of the activity [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[392],"tags":[394,418,415,413,417,416,395,414,404,393,405],"class_list":["post-1401","post","type-post","status-publish","format-standard","hentry","category-android-app-development","tag-android","tag-android-expandablelistview","tag-android-imageview","tag-android-listview","tag-android-listview-icon","tag-android-listview-image","tag-android-studio","tag-android-textview","tag-app","tag-fitness-manager","tag-java"],"_links":{"self":[{"href":"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-json\/wp\/v2\/posts\/1401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-json\/wp\/v2\/comments?post=1401"}],"version-history":[{"count":10,"href":"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-json\/wp\/v2\/posts\/1401\/revisions"}],"predecessor-version":[{"id":1495,"href":"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-json\/wp\/v2\/posts\/1401\/revisions\/1495"}],"wp:attachment":[{"href":"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-json\/wp\/v2\/media?parent=1401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-json\/wp\/v2\/categories?post=1401"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mplx.de\/Fremdgesteuert\/wp-json\/wp\/v2\/tags?post=1401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}