GPGVM
GPGVM

Reputation: 5619

Using RelativeLayout to control a ListView row height

I have what seems by my Googling to be a rare question.

I have an XML file that defines what a row should look like in my ListView.

Since I only want to see one row at a time I override the getCount of the Adapter which works great at showing me only one row. The trouble comes in that despite my settings to fill_parent the row still doesn't fill the screen.

I have tried numerous things and the closest I get is to set the RelativeLayout minHeight but then it is specific to a screen size. So either my elements are not correctly setup in the RelativeLayout or I need a way to automatically adjust minHeight based upon device.

I would prefer to do this in XML but could adjust programatically at launch if needed.

This is the code for the parent container:

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:orientation="vertical">

    <ListView 
         android:layout_width="fill_parent" 
    android:layout_height="fill_parent" />

</RelativeLayout>

and for the row xml:

<RelativeLayout
    android:layout_width="fill_parent"
    android:orientation="vertical" 
    android:layout_height="fill_parent">

    <ImageView
        android:layout_width="60dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="5dp" />

    <TextView
        android:layout_centerHorizontal="true"
        android:layout_width="185dp"
        android:layout_height="25dp"
        android:textSize="20sp"
        android:layout_marginTop="15dp"
        android:gravity="center"
        android:inputType="none" />

    <ImageView
        android:layout_width="60dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="60dp"
        android:layout_marginLeft="5dp" />


    <TextView
        android:layout_centerHorizontal="true"
        android:layout_width="185dp"
        android:layout_height="25dp"
        android:textSize="20sp"
        android:layout_marginTop="60dp"
        android:gravity="center"
        android:inputType="none" />

    <TextView
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="35dp"
        android:layout_width="95dp"
        android:layout_height="50dp"
        android:textSize="18dp"
        android:layout_marginTop="110dp"
        android:gravity="center"
        android:inputType="none" />

    <org.gpgvm.widget.NumberPicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/btn_PrevLift"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
         android:layout_marginLeft="35dp"
        android:layout_marginBottom="120dp">
    </org.gpgvm.widget.NumberPicker>

    <TextView
        android:layout_alignParentRight="true"
        android:layout_marginRight="35dp"
        android:layout_width="95dp"
        android:layout_height="50dp"
        android:textSize="18dp"
        android:layout_marginTop="110dp"
        android:gravity="center"
        android:inputType="none" />

    <org.gpgvm.widget.NumberPicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_NextLift"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
         android:layout_marginRight="35dp"
        android:layout_marginBottom="120dp" >
    </org.gpgvm.widget.NumberPicker>

    <Button
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="30dp"
        android:layout_marginBottom="30dp" />

    <Button
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="30dp"
        android:layout_marginBottom="30dp" />   

</RelativeLayout>

Upvotes: 0

Views: 629

Answers (1)

Housefly
Housefly

Reputation: 4422

get the height of the screen of the device using the below code and set the minHeight dynamically

DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        int mHeight = metrics.heightPixels;
        int mWidth = metrics.widthPixels;

 RelativeLayout yourLayout= (RelativeLayout) findViewById(R.id.yourId);
    RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(mWidth, mHeight);

    yourLayout.setLayoutParams(newParams );

Upvotes: 1

Related Questions