Search code examples
androidandroid-linearlayoutandroid-relativelayout

Issue with UI in android


I am trying to achieve the following screen:

enter image description here

Hence I tried the following code in XML:

<RelativeLayout
        android:id="@+id/Numberlayout"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:layout_marginLeft="5dp"
        android:visibility="visible"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:baselineAligned="false"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="@color/whiteText"
                android:orientation="vertical" >

                <RelativeLayout
                    android:id="@+id/balancelayout"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight=".50"
                    android:background="@color/layoutcolor_bal_net"
                    android:gravity="center" >

                    <ImageView
                        android:id="@+id/balancerupee"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="3dp"
                        android:background="@drawable/rupees_symbol"
                        android:paddingBottom="5dp"
                        android:paddingTop="5dp"
                        android:visibility="visible" />

                    <TextView
                        android:id="@+id/balanceamount"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_toRightOf="@id/balancerupee"
                        android:paddingLeft="2dp"
                        android:text="@string/numbers"
                        android:textColor="@color/numbertext"
                        android:textSize="14sp"
                        tools:ignore="RtlSymmetry,RtlHardcoded" />

                    <ImageView
                        android:id="@+id/leftarrow"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/leftarrow"
                        android:layout_alignParentLeft="true"
                        android:visibility="visible" />


                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="@color/whiteText"
                android:orientation="vertical" >

                <RelativeLayout
                    android:id="@+id/netlayout"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight=".50"
                    android:background="@color/layoutcolor_bal_net"
                    android:gravity="center" >

                    <ImageView
                        android:id="@+id/netrupee"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="3dp"
                        android:background="@drawable/rupees_symbol"
                        android:paddingBottom="5dp"
                        android:paddingTop="5dp"
                        android:visibility="visible" />

                    <TextView
                        android:id="@+id/netamount"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_toRightOf="@id/netrupee"
                        android:paddingLeft="2dp"
                        android:text="@string/numbers"
                        android:textColor="@color/numbertext"
                        android:textSize="14sp"
                        tools:ignore="RtlSymmetry" />

                    <TextView
                        android:id="@+id/nettext"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/netamount"
                        android:paddingLeft="12dp"
                        android:text="@string/networth"
                        android:textSize="14sp"
                        tools:ignore="RtlSymmetry" />

                    <ImageView
                        android:id="@+id/rightarrow"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/rightarrow"
                        android:layout_alignParentRight="true"
                        android:visibility="visible" />

                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>
        </RelativeLayout>

enter image description here

The arrows are not sitting properly to right and left and also the numbers moved. I am struggling to solve this issue.

Can somebody help me fix this?

Thanks!


Solution

  • You could start with something like this:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
    
      <ImageView
        android:id="@+id/left_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/leftarrow" />
    
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:drawableLeft="@drawable/rupees"
        android:layout_toRightOf="@id/left_arrow"
        tools:text="123456" />
    
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:drawableLeft="@drawable/rupees"
        android:layout_toLeftOf="@+id/right_arrow"
        tools:text="123456" />
    
      <ImageView
        android:id="@id/right_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/rightarrow"
        android:layout_alignParentRight="true"/>
    
    </RelativeLayout>
    

    In general, avoid nested layouts. Performance implications aside, it's really hard to read and re-factor.