user-44651
user-44651

Reputation: 4124

Custom Row for List View

I am trying to build this custom row for my ListView. Through the magic of Photoshop here is what I am trying to accomplish:

enter image description here

But when I run my list view, it is coming out like this:

enter image description here

Can anyone help me figure out what I am doing wrong with the view?

Here is my row.xml

<?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:orientation="horizontal"
    android:padding="5dp">

    <!--  ListRow Left sied Thumbnail image -->
    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_alignParentRight="true"
            android:background="@color/COLOR_GREY" />
    </LinearLayout>

    <!-- Item Name -->
    <TextView
        android:id="@+id/txtItemName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Item Name"
        android:textColor="#040404"
        android:textSize="15dip"
        android:textStyle="bold"
        android:typeface="sans" />

    <!-- progress count -->
    <TextView
        android:id="@+id/txtProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/thumbnail"
        android:layout_alignLeft="@+id/thumbnail"
        android:text="Item Name"
        android:textColor="#040404"
        android:textSize="15dip"
        android:textStyle="bold"
        android:typeface="sans" />

    <!-- Retry button -->
    <Button
        android:id="@+id/btnRetry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/txtItemName"
        android:layout_toRightOf="@id/txtItemName"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:text="Retry" />

    <!-- Delete button -->
    <Button
        android:id="@+id/btnDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/btnRetry"
        android:layout_toRightOf="@id/btnRetry"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:text="Delete" />

    <!-- ProgressBar -->
    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_below="@id/txtItemName"
        android:layout_alignLeft="@id/txtItemName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBarItem" />

</RelativeLayout>

Upvotes: 1

Views: 51

Answers (2)

aleksandrbel
aleksandrbel

Reputation: 1490

This is the layout mostly done as on your picture, just need to change with the buttons in order to make them looks better.

In RelativeLayout you do not need to use orientation and for text use sp instead of dip

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/row"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:baselineAligned="false"
     android:gravity="center_vertical">

<!--  ListRow Left sied Thumbnail image -->

<ImageView
    android:id="@+id/list_image"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    app:srcCompat="@mipmap/ic_launcher" />

<!-- Item Name -->
<TextView
    android:id="@+id/txtItemName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/list_image"
    android:layout_toEndOf="@+id/list_image"
    android:text="Item Name"
    android:textColor="#040404"
    android:textSize="15dip"
    android:textStyle="bold"
    android:typeface="sans" />

<!-- progress count -->
<TextView
    android:id="@+id/txtProgress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:text="100"
    android:textColor="#040404"
    android:textSize="15sp"
    android:textStyle="bold"
    android:typeface="sans" />

<!-- Retry button -->
<Button

    android:id="@+id/btnRetry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/btnDelete"
    android:layout_toStartOf="@+id/btnDelete"
    android:text="Retry" />

<!-- Delete button -->
<Button
    android:id="@+id/btnDelete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:text="Delete" />

<!--&lt;!&ndash; ProgressBar &ndash;&gt;-->
<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_below="@id/txtItemName"
    android:layout_toLeftOf="@+id/txtProgress"
    android:layout_toStartOf="@+id/txtProgress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/list_image"
    android:layout_toEndOf="@+id/list_image"
    android:progress="100"
    android:id="@+id/progressBarItem" />

You can find other tips how to work with layouts by doing small parts of the layout and searching them in the stackoverflow and then you can learn how to do bigger layouts. Because before you really needed to wrap some components into linear layouts, but now with Android Studio 2.2 you can use ConstraintLayout

Upvotes: 1

Rahim Rahimov
Rahim Rahimov

Reputation: 1417

Why you using ImageView inside LinelarLayout?

Check this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp">

    <!--  ListRow Left sied Thumbnail image -->

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_alignParentLeft="true"
        android:background="@color/COLOR_GREY" />

    <!-- Item Name -->
    <TextView
        android:id="@+id/txtItemName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/list_image"
        android:layout_toRightOf="@id/list_image"
        android:text="Item Name"
        android:textColor="#040404"
        android:layout_marginLeft="10dp"
        android:textSize="15sp"
        android:textStyle="bold"
        android:typeface="sans" />

    <!-- progress count -->
    <TextView
        android:id="@+id/txtProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dp"
        android:layout_alignBottom="@id/list_image"
        android:text="Item Name"
        android:textColor="#040404"
        android:textSize="15sp"
        android:textStyle="bold"
        android:typeface="sans" />


    <!-- Delete button -->
    <Button
        android:id="@+id/btnDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/btnRetry"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:text="Delete" />

    <!-- Retry button -->
    <Button
        android:id="@+id/btnRetry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/txtItemName"
        android:layout_toLeftOf="@id/btnDelete"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:text="Retry" />

    <!-- ProgressBar -->
    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_alignBottom="@id/list_image"
        android:layout_toRightOf="@id/list_image"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="5dp"
        android:layout_toLeftOf="@id/txtProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBarItem" />

</RelativeLayout>

Also, don't forget that textSize should be in sp

If you want to change button sizes - just replace:

  android:layout_width="wrap_content"
  android:layout_height="wrap_content"

and put your custom width and height in dp like:

  android:layout_width="25dp"
  android:layout_height="25dp"

Upvotes: 0

Related Questions