2

My alarm clock design layout listView looked as image below.

enter image description here

custom_row_view

<?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:orientation="vertical">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="10dp">

            <TextView
                android:id="@+id/time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:text="Time"
                android:paddingLeft="40dp"
                android:paddingTop="15dp"
                android:textColor="@color/black"
                android:textStyle="bold"
                />

            <ToggleButton
                android:id="@+id/switchAlarm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textOff=""
                android:textOn=""
                android:layout_marginLeft="200dp"
                android:drawableTop="@mipmap/switch_alarm"
                />



        </RelativeLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Text"
            android:id="@+id/textView" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Text"
            android:layout_marginRight="@+id/textView"
            android:id="@+id/textView1" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Text"
                android:layout_marginRight="@+id/textView1"
                android:id="@+id/textView2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Text"
                android:layout_marginRight="@+id/textView2"
                android:id="@+id/textView3" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Text"
                android:layout_marginRight="@+id/textView3"
                android:id="@+id/textView4" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Text"
                android:layout_marginRight="@+id/textView4"
                android:id="@+id/textView5" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Text"
                android:layout_marginRight="@+id/textView5"
                android:id="@+id/textView6" />

        </LinearLayout>

    </LinearLayout>

My app crashed when this layout get called.

4-14 23:49:10.491  31175-31175/com.example.seng.clock E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12
            at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:471)
            at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:5906)
            at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1854)
            at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1766)
            at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:58)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:748)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at Adapter.MyCustomBaseAdapter.getView(MyCustomBaseAdapter.java:68)
            at android.widget.AbsListView.obtainView(AbsListView.java:2232)
Tony
  • 2,515
  • 14
  • 38
  • 71

2 Answers2

17

Remove android:layout_marginRight="@+id/textView" from the following code.

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="5dp"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Text"
    android:layout_marginRight="@+id/textView"
    android:id="@+id/textView1" />

You cannot give a view as margin.

K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
0

From the documentation : ViewGroup.MarginLayoutParams

The parameters ( I provide the common attributes that may be used. However there are more) , just accept positive values and are not supposed to be used as a reference to another view, element or child on a view.

  • layout_marginStart : Specifies extra space on the top side of this view.
  • layout_marginRight : Specifies extra space on the top side of this view.
  • layout_marginLeft : Specifies extra space on the top side of this view.
  • layout_marginVertical : Specifies extra space on the top and bottom sides of this view.
  • layout_marginHorizontal: Specifies extra space on the left and right sides of this view.

Instead of a view reference, according to the documentation :

android:layout_marginStart : Specifies extra space on the start side of this view. This space is outside this view's bounds. Margin values should be positive. May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

So the line must be changed to something like :

android:layout_marginStart = "10dp"

android:layout_marginLeft="10dp"
eduardo92
  • 1,085
  • 6
  • 9