Search code examples
androidandroid-layoutandroid-relativelayout

How do I place my textview under another?


I am trying to place my textview4 below, my sourceNameTextView, but i can't see the text from textView4? I am new to using relative layout can someone tell me why this is happening and how to fix this? I already tried reading some post on stackoverflow but can't find anything that fixes this issue.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"

    android:layout_height="80dp"
    android:layout_marginLeft="18dp"
    android:layout_marginRight="18dp"
    android:layout_marginBottom="18dp"
    android:background="@drawable/gradient"
    android:clickable="true"
    android:focusable="true"
    android:padding="6dip">

    <ImageView
        android:id="@+id/newsThumbnail"

        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="6dip"
        android:contentDescription="TODO"
        android:src="@mipmap/ic_launcher" />


    <TextView
        android:id="@+id/newsBody"

        android:layout_width="match_parent"


        android:layout_height="wrap_content"


        android:layout_alignWithParentIfMissing="true"
        android:layout_above="@id/sourceName"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/newsThumbnail"
        android:gravity="center_vertical"
        android:textColor="#FFFFFF"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/sourceName"


        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:layout_toEndOf="@id/newsThumbnail"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="Description"
        android:textColor="#9A9CA5"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_toEndOf="@id/newsThumbnail"
        android:layout_below="@+id/sourceName"

        android:layout_height="wrap_content"
        android:text="TextViwew.." />


</RelativeLayout>

Solution

  • The reason you don't see textView4 because you added android:layout_alignParentBottom="true" to the sourceName TextView,

    This means that that you want to align a view which is textView4 to be beneath of a View that is exactly at the bottom of the parent.

    So, the textView4 will be off the screen or correctly speaking will be off the RelativeLayout (under the bottom of it)

    To Solve this You need to remove android:layout_alignParentBottom="true" from the sourceName TextView

    Also when you do this change you will notice that newsBody now is gone, that is because you added android:layout_above="@id/sourceName" to it and didn't constraint the sourceName to be at the bottom of it.

    You can use now:

    <?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="80dp"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="18dp"
        android:layout_marginBottom="18dp"
        android:background="@drawable/gradient"
        android:clickable="true"
        android:focusable="true"
        android:padding="6dip">
    
        <ImageView
            android:id="@+id/newsThumbnail"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="6dip"
            android:contentDescription="TODO"
            android:src="@mipmap/ic_launcher" />
    
    
        <TextView
            android:id="@+id/newsBody"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignWithParentIfMissing="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_toEndOf="@id/newsThumbnail"
            android:layout_toRightOf="@id/newsThumbnail"
            android:gravity="center_vertical"
            android:textColor="#FFFFFF"
            android:textSize="16sp" />
    
        <TextView
            android:id="@+id/sourceName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_below="@+id/newsBody"
            android:layout_marginBottom="10dp"
            android:layout_toEndOf="@id/newsThumbnail"
            android:ellipsize="marquee"
            android:singleLine="true"
            android:text="Description"
            android:textColor="#9A9CA5"
            android:textSize="12sp" />
    
        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sourceName"
            android:layout_toEndOf="@id/newsThumbnail"
            android:layout_toRightOf="@id/newsThumbnail"
            android:text="TextViwew.." />
    
    </RelativeLayout>