How to Avoid Overlap View in Relative Layout in Android

How to avoid overlap view in relative layout in android?

If you are using this kind of design than you should use linear Layout.
and use Table Rows in it to display this kind of view.

and also use weight so that your view doesn't et overlap on other views. try like this:

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:weightSum="10">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:textSize="30sp"
android:text="test"
android:layout_weight="5" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:textSize="20sp"
android:text="test"
android:layout_weight="5"/>
</TableRow>

</LinearLayout>
</ScrollView>

Hope it Helps!!

How to avoid view overlapping in RelativeLayout

I have changed some of your code and try to make a layout as you want.

 <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- CONTENT NO 1 -->
<LinearLayout
android:id="@+id/linerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginBottom="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top aligned text1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top aligned text2"/>
</LinearLayout>
<!-- /CONTENT NO 1 -->
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_below="@+id/linerlayout"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bottom aligned text1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bottom aligned text2"/>
<!-- CONTENT NO 2 -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="bottom"
android:layout_height="match_parent"
android:layout_weight="0">
</LinearLayout>
<!-- /CONTENT NO 2 -->
</LinearLayout>
</RelativeLayout>
</ScrollView>
<Button
android:layout_alignBottom="@id/scrollview"
android:text="bottom button"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</RelativeLayout>

you can also add your CONTENT NO 1 and CONTENT NO 2 UI as per your requirement

How can I prevent views from overlapping in relative layout?

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.

Prevent views from overlapping in RelativeLayout

use layout_toLeftOf in TextView layout

like this:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/caption"
android:layout_below="@+id/caption" >

<ImageButton
android:id="@+id/button_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="@drawable/content_edit"
style="?android:attr/borderlessButtonStyle" />

<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/num_placeholder"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_toLeftOf="@+id/button_edit" />

How to center a view in a layout without overlapping other view

In order to make the text look centered the margins from both the ends of the parent layout have to be equal. So without adding fake button you can add a margin_right to text equal to width of the button.

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">

<RelativeLayout
android:id="@+id/dialog_header"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageButton
android:id="@+id/dialog_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:padding="20dp"
android:src="@drawable/btn_close" />

<TextView
android:id="@+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/dialog_close"
android:layout_marginRight = "//value equal to source width"
android:text="Some very long text which will overlap my button and I dont want that."
android:textSize="17sp"
android:gravity="center"/>

</RelativeLayout>
</FrameLayout>

Hope this helps.

How to partially overlap views within a relative layout

Views are drawn in the order they're listed in your layout, with the first one listed being the farthest away on the z-axis, the last being the closest. If you want the button on top, list it last in the layout.

<include layout="@layout/view"
android:id="@id/view"
android:layout_height="100dp"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"/>

<include layout="@layout/button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_above="@+id/view"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:layout_marginBottom="-50dp"/>

Text view overlapping when orientation with relative layout

Add another RelativeLayout as a parent for your TextView above btn1 with android:layout_height="match_parent"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/btn1">
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="TEST"
android:textAlignment="center"
android:textSize="36sp" />
</RelativeLayout>
<Button
android:id="@id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn2"
android:text="button1"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn3"
android:text="button 2"/>
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="button 3"/>
</RelativeLayout>

Another solution

Add android:layout_height="match_parent" and android:layout_above="@+id/btn1" to your TextView, and center the text in the TextView using android:gravity="center"

<RelativeLayout android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/btn1"
android:gravity="center"
android:text="TEST"
android:textAlignment="center"
android:textSize="36sp"/>

<Button
android:id="@id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn2"
android:text="button1"/>

<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn3"
android:text="button 2"/>

<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="button 3"/>
</RelativeLayout>

Either solution should work fine for both portrait and landscape orientations

How to not overlapping with RelativeLayout

Use

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
android:id="@+id/imageViewTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />

<ImageView
android:id="@+id/imageViewBottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="@drawable/ic_launcher" />

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/imageViewBottom"
android:layout_below="@+id/imageViewTop"
android:background="#006600" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>

</RelativeLayout>


Related Topics



Leave a reply



Submit