-1

I am trying to build a custom view for the ListView rows. Below is what my row looks like

Img  |  Text 1           
     |------------
Text |  Text 2           

Below is my layout that I am using to generate row view

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

<RelativeLayout
    android:id="@+id/icon_status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_margin="10dp"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:contentDescription="@string/app_name"/>

    <TextView
        android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/icon"
        android:layout_centerHorizontal="true"
        android:text="STATUS" />
</RelativeLayout>

<View
    android:id="@+id/ver_divider"
    android:layout_width="2dp"
    android:layout_height="fill_parent"
    android:layout_alignBottom="@id/icon_status"
    android:layout_alignTop="@id/icon_status"
    android:layout_toRightOf="@id/icon_status"
    android:background="@android:color/black" />

<View
    android:id="@+id/hor_divider"
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:layout_centerVertical="true"
    android:layout_marginRight="10dp"
    android:layout_toRightOf="@id/ver_divider"
    android:background="@android:color/black" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignBottom="@id/icon_status"
    android:layout_alignTop="@id/icon_status"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_toRightOf="@id/ver_divider"
    android:background="#99FF0000"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Test ME" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Test ME" />
</LinearLayout></RelativeLayout>

List view is pretty simple no customisation.

Text 1 and Text 2 comes at equal distance from the horizontal separator. While I tried to set Text2 below the separator with layout_below it doesn't work. In the above layout I tried to create a linearlayot with full hight as view and then setting two textviews of same height using weight this also doesn't work.

While the same code works fine on android 5.0 it fails on android 4.4.4 or below.

Any help is highly appreciated.. Not sure what should I do to make it work on the android 4.x.x

Manish
  • 1,215
  • 10
  • 29

1 Answers1

0

Setting layout_below on Text 2 wont work, because the parent is a LinearLayout. I would have put the hor_divider inside the LinearLayout, something like this:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignBottom="@id/icon_status"
    android:layout_alignTop="@id/icon_status"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_toRightOf="@id/ver_divider"
    android:background="#99FF0000"
    android:orientation="vertical" 
    android:gravity="center_vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Test ME" />

    <View
        android:id="@+id/hor_divider"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@android:color/black" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Test ME" />
</LinearLayout>

Then you can add some padding for Text 1 and Text 2 to create some more space.

Carnal
  • 21,744
  • 6
  • 60
  • 75
  • That won't work as icon height will vary and whole solution won't remain dynamic. – Manish Dec 04 '14 at 15:14
  • @Manish: You have set it to 60dp. But maybe, just a quick suggestion at the right direction. Either way you could also specify the behaviour in 4.x, makes it easier to help you. – Carnal Dec 04 '14 at 15:20
  • @Manish: To start with, I wouldn't have a RelativeLayout as root. I would in your case made it a LinearLayout with orientation = horizontal, with 3 childs – Carnal Dec 04 '14 at 15:22
  • thanks for suggestion but still problem remain same, how to put the two views above and below horizontal divider with vertical center position. I am just wondering how it works on android 5 and not below as it seems like normal layout. – Manish Dec 04 '14 at 17:59