How to prevent overlapping in relative layout programmatically?

How can I prevent views from overlapping in relative layout?

  • Right now I have two textViews one on aligned to the left of the relative layout and one aligned to the right. The text on the left is much longer than that on the right. On some occasions, the text on the left is two lines. When this happens this text overlaps the text that is aligned to the right. Is there anyway to prevent this? Or is there a way to say if the text on the left is two lines put the text on the right on the second line? I'm having trouble with name and time down here: <RelativeLayout android:layout_width="wrap_content" android:id="@+id/relativeLayout" android:layout_height="wrap_content"> <TextView android:text="TextView" android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10sp" android:textStyle="bold" android:textSize="16sp"></TextView> <TextView android:layout_width="wrap_content" android:id="@+id/address" android:text="address" android:layout_height="wrap_content" android:layout_below="@+id/name" android:layout_alignLeft="@+id/name" android:layout_marginLeft="30sp"></TextView> <TextView android:layout_width="wrap_content" android:layout_toRightOf="@+id/address" android:text="" android:layout_height="wrap_content" android:layout_alignTop="@+id/address" android:layout_alignBottom="@+id/address" android:id="@+id/crossStreet"></TextView> <TextView android:layout_width="wrap_content" android:id="@+id/time" android:text="Time" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="10sp"></TextView> </RelativeLayout>

  • Answer:

    Could you instead contain those two TextViews within a horizontal LinearLayout (which itself would still be a child of your RelativeLayout). Then, assign appropriate weight attributes to each of the two TextViews within that LinearLayout.

angrymonkey at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Please check below code more helpful to you. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="fill_parent" android:weightSum="1.0" android:id="@+id/relativeLayout" android:layout_height="wrap_content"> <TextView android:text="TextView1 hhhhh hhhhhhhh hhhhhhhh hhhhhhh hhhhhh" android:id="@+id/name" android:layout_weight="0.5" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_marginLeft="10sp" android:textStyle="bold" android:textSize="16sp"></TextView> <TextView android:layout_width="0dip" android:layout_weight="0.5" android:id="@+id/time" android:text="Textview4" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="10sp"></TextView> </LinearLayout> </LinearLayout>

Nik....

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.