![]() ![]() Code fragment that gets references to the TextView objects and sets the text properties. These are the TextView's are contained within a horizontally oriented LinearLayout (See main.xml, Listing 1). Listing 2 shows how all the text in the header area are set in the code. The column headings are set in the same manner. Android does not require that the resource id to be unique, but it's a good idea to make them unique within the context of the view hierarchy from which the search is made. ![]() The findViewById() method will start from the root element and recursively search the view hierarchy to find the element with the resource id of header_line1 and return the TextView. ![]() TextView description1 = (TextView) findViewById(id.header_line1) For example, to get the reference to the first header line, use: The resource id is defined by the android:id property of the element in the resource file. To do this, first get a reference to the TextView object by calling findViewById() with the resource id of the TextView. ![]() As of API level 8, fill_parent has been deprecated and the use of match_parent is preferred.ĭynamically Setting the Text in the Header Areaĭynamically setting a text property in the code is simply done by passing the text string with the setText() method of the TextView object. Note: Sometimes you will see fill_parent used rather than match_parent. Since the actual values are not known until the activity starts, they will be set in the activity code using the onCreate() method. None of the TextView elements in the resource file use the android:text property to set the text in the header area.More on this later in the Column Alignment section. Another property, android:layout_weight, also used. The value wrap_content is used for the layout_height property of the parent element, but the width is set using android:layout_width="0dp". The two TextView column header elements for the column headings in the third line are contained within a horizontal LinearLayout element.This is done by setting android:orientation to horizontal. The LinearLayout of the third header line contains two TextView elements which flow horizontally.The Android docs talk about the wrap_content value here. The same is true for the LinearLayout element of the third header line. The three header line elements set the values of android:layout_height property to wrap_content. This means that the height of the TextView elements in the first and second header lines will be just large enough to contain the text and padding.The three header line elements (the two TextView's and the LinearLayout) use match_parent as the android:layout_width property so that the total display width is used.This will make this view use all available display space. Root LinearLayout element sets the android:layout_width and android:layout_height properties to match_parent.Things to note in the main.xml resource file, Listing 1: The ListView element that contains the contents of the list.A LinearLayout element that contains the two TextView column headings which are arranged horizontally.Another TextView element for the second header line.A TextView element that contains the first header line.To accomplish the look in Figure 1, I use a layout resource file (main.xml) with a vertical LinearLayout as the root element. The header area is fixed and does not scroll with the list. The column headings align with the columns of data. There is a list header with two lines of description and a third line for the column headings. įigure 1 shows the visual effect that I want to achieve. This is a simple class consisting of two String type member variables, getter and setter methods, and a static method for conveniently generating a list of MyStringPair objects. The data that will be displayed in the list are objects instantiated from a simple class, MyStringPair.java. Finally, I want to show how to populate the ListView with rows of data.Īll code listings are available from my github project. Another objective is to show how to display the data in a multi-column list where the data and the column headings are nicely aligned. This is to cover a situation where the header information is not known until the activity is launched. I also want to dynamically set the text strings in the header area. In particular, I want to show how to display a fixed header with a ListView where the header area is stationary and does not scroll with the contents of the list. This tutorial was written to demonstrate how to work with a list of data using a ListView with Android. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |