rafi787 rafi787 - 3 months ago 10
Java Question

Android : code not functioning as intented

Screenshot of Code

Im trying to display information about a business.
However the phone number is being displyed in wrong place ( ie top right)
According to me, It should come right of call image.
What is wrong with code phonenumber_text_view

<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="match_parent"

tools:context="com.example.rafi.ansarenglishschool.MainActivity">
<ImageView
android:id="@+id/ansar_image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@drawable/ansar"
android:layout_alignParentTop="true"
/>
<TextView
android:id="@+id/ansar_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ansar_image_view"
android:text="Ansar English School"
android:textSize="30sp"
android:background="#2962ff"
android:padding="20dp"
android:textColor="@android:color/white" />


<ImageView
android:id="@+id/call_image_view"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="@drawable/call1"
android:layout_below="@id/ansar_text_view"
/>


<TextView
android:id="@+id/phonenumber_text_view"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_toRightOf="@id/call_image_view"
android:layout_alignParentRight="true"
android:text="123456789"
android:textSize="30sp"
android:padding="20dp"
android:textColor="@android:color/black" />*

<ImageView
android:id="@+id/location_image_view"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="@drawable/location1"
android:layout_below="@id/call_image_view"
/>
<TextView
android:id="@+id/disc_text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/location_image_view"
android:text="Ansar English is a private school run by Ansari Charitable trust located in Perumpilavu."
android:textSize="15sp"
android:padding="20dp"
android:textColor="@android:color/black" />



Answer

Just use the android:layout_below attribute to align it properly like following:

<TextView
        android:id="@+id/phonenumber_text_view"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_toRightOf="@id/call_image_view"
        android:layout_below="@id/ansar_text_view"
        android:text="123456789"
        android:textSize="30sp"
        android:padding="20dp"
        android:textColor="@android:color/black" />

PS: There is a * right after above TextView in your xml file