7

I am displaying a textview(I intend this to fill the entire screen excluding the button below it) and a button(small one at the bottom) in an activity. I want textview to be placed aove the button. I don't want to hardcode any height/width.

Hence For button I have kept height ad width as word_wrap For textview I have kept width as fill parent.

What I want to know is that, is there anyway by whcih I can specify textview height to be screenheight-button height. I want to this in xml file but not dnamically inside my code.

user1938357
  • 1,466
  • 3
  • 20
  • 33

3 Answers3

5

To achieve this you have to create something like this :

<?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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/button1"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

</RelativeLayout>

and you have a textview which will stay above your button and will fit the entire screen.

hardartcore
  • 16,886
  • 12
  • 75
  • 101
  • thanks a lot. I had two buttons(both wrap_text) at the bottom, hence your suggestion worked the best compared to the other two answers I had received. The other two would have also worked with a bit tweaking I guess. – user1938357 Jan 05 '13 at 23:09
  • any idea how can now I get the height of the textview in onCreate method? textview.getHeight() does not work as the resources are not ready before onCreate execution which is usedto set the content of the activity. – user1938357 Jan 05 '13 at 23:15
  • this can give you the right answer i guess : http://stackoverflow.com/questions/4142090/how-do-you-to-retrieve-dimensions-of-a-view-getheight-and-getwidth-always-r/4406090#4406090 – hardartcore Jan 06 '13 at 14:09
0

Use RelativeLayout, set textview's dimensions as fill_parent, then place button below it, set button's android:layout_below="@+id/textview" and button's dimensions as wrap_content.

Check the result in visual designer or in device, it should work

Axarydax
  • 16,353
  • 21
  • 92
  • 151
0

You can use layout_weight attribute

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:text="TextView" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>
Vladimir Mironov
  • 30,514
  • 3
  • 65
  • 62