kamilos207
kamilos207

Reputation: 61

Setting a ListView row height

I've got a problem in my application.

I use a ListView to show my products.
I can't get the effect, where every item shows all over the screen.
I mean, so that each row inherits height.

Here is my ListView layout:

<?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"
    android:weightSum="4">
    <!-- Main ListView
         Always give id value as list(@android:id/list)
    -->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="3"
        android:weightSum="2"
        android:background="@color/pod_logo">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/iVlogo_obiekty"
            android:src="@drawable/logo_akme"
            android:layout_weight="1" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="NAZWA OBIEKTU"
            android:id="@+id/tV_nazwa_obiektu"
            android:gravity="center"
            android:textColor="#fff"
            android:textSize="20dp"
            android:background="@color/niebieski"
            android:layout_weight="1" />
    </LinearLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#000" />

</LinearLayout>

Here is my row layout:

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

    <!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
    <TextView
        android:id="@+id/pid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <!-- Name Label -->
    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:textStyle="bold"
        android:gravity="center"
        android:height="40dp"
        android:textColor="#fff"
        android:background="#123456"

        android:text="TEXT"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="0dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="0dp" />

</LinearLayout>

I changed the concept and i now want something like this on picture:

EXAMPLE

Someone want to help me ? Pls. I dont know what i must change:/

Upvotes: 1

Views: 356

Answers (1)

PPartisan
PPartisan

Reputation: 8231

You could try to set the row height programmatically. Note that the code below is untested/from memory, so it may require some tweaks, but essentially it grabs the height of the screen in your Adapter's constructor (or somewhere else) and stores it to screenHeight. You can then use it to set the height of each row in getView():

private int screenHeight;
//...
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics metrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(metrics);
    screenHeight = metrics.heightPixels;
//...

@Override
public View getView (int position, View convertView, ViewGroup parent) {
    if( convertView == null ){
        convertView = inflater.inflate(R.layout.row, parent, false);
    }
    LayoutParams params = convertView.getLayoutParams();
    if (params.height != screenHeight) params.height = screenHeight;

    convertView.setLayoutParams(params);

    return convertView;
}

Edit: Note that the above code assumes you pass a Context argument in your Adapter's constructor, or you could just pass the screenHeight directly, etc.

Upvotes: 1

Related Questions