0

My app contains ListView and TextView. I want to arrange TextView to below of ListView. I tried with below code, but TextView disappeared.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <ListView android:id="@+id/android:list" 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" /> 

 <TextView  
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:layout_below="@+id/android:list"
  android:text="@string/hello"
 />
</RelativeLayout>

Thanks.

Natali
  • 2,934
  • 4
  • 39
  • 53
Riskhan
  • 4,434
  • 12
  • 50
  • 76

6 Answers6

3

Try replacing your XML with this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

 <ListView android:id="@+id/android:list" 
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_weight="1" /> 

 <TextView  
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:layout_below="@+id/android:list"
  android:text="@string/hello"
 />
</LinearLayout>

You don't want to use the relative layout at all. If you need it specifically, simply replace the LinearLayout with it.

C--
  • 16,393
  • 6
  • 53
  • 60
  • The layout_weight="1" makes the layout to fill into maximum screenspace available". Try putting layout_weigth to the text view too, you will understand the behaviour. – C-- Jan 30 '12 at 11:41
2

Try LinearLayout as below. I think this will produce what you're after. This layout will always put the TextView below the list at the bottom of the view.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

<ListView android:id="@+id/android:list" 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1" /> 

<TextView  
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="@string/hello"
  />
</LinearLayout>
CjS
  • 2,037
  • 2
  • 21
  • 29
  • thanks, your answer is working. May i know about android:layout_weight="1" – Riskhan Jan 30 '12 at 11:16
  • layout_weight provides a weight to each view that needs to get to the right height to fill_parent. Since the other weights are zero, the ListView expands to fill all remaining space. [Read more](http://stackoverflow.com/questions/3995825/what-does-androidlayout-weight-mean) – CjS Jan 30 '12 at 13:23
1

Try this code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
>  
<ListView android:id="@+id/android:list" 
 android:layout_height="match_parent"  
 android:gravity="center"
 android:cacheColorHint="#ffffffff" 
 android:layout_width="match_parent" 
 android:layout_above="@+id/add_workout_all_workout_list"></ListView>

<TextView android:layout_width="wrap_content"      
 android:id="@+id/add_workout_all_workout_list" android:layout_height="wrap_content" 
 android:text="textView" android:layout_alignParentBottom="true"  
 android:layout_alignParentLeft="true"/>

</RelativeLayout>  
Riskhan
  • 4,434
  • 12
  • 50
  • 76
deepak Sharma
  • 1,641
  • 10
  • 23
0

You used fill_parent for both width and height of listview as below. So it will occupy entire screen. So that the Text view is invisible. Please change it to fixed height according to your device resolution.

<ListView android:id="@+id/android:list" 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" /> 

Replace fill_parent with 100px or other in below line and try once

  android:layout_height="fill_parent" /> 
Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67
0

U can place Textview below listview by 2 ways.

  1. Take Relative layout and fix the height of listview and place textview below it.
  2. Take LinearLayout and give weight in horizontal orientation.

Hope u get what i say.

Richa
  • 3,165
  • 1
  • 22
  • 26
0

If you insist on using RelativeLayout, do the following:

  1. In your xml, declare first your TextView before your ListView (interchange declarations) - order is important when using RelativeLayout
  2. In your TextView, put android:layout_alignParentBottom="true"
  3. In your ListView, put android:layout_above="@android/your_text_views_id_here"
  4. Remove all other positioning properties.

Should work.

josephus
  • 8,284
  • 1
  • 37
  • 57