Mountain King
Mountain King

Reputation: 11

Layouts on top of each other

This is an android development question.
If I first set the content view to my xml layout using setContentView(R.layout.main); and then add another content view using addContentView(layout, params), the content views overlap each other. I want the second content view to position itself directly under the first one. Is it possible or do I need to combine the xml and the programatically created layouts in some way? Both of the layouts are linear.

Thanks in advance

Upvotes: 1

Views: 4894

Answers (3)

Ojonugwa Jude Ochalifu
Ojonugwa Jude Ochalifu

Reputation: 27237

To place a layout on top of each other (one top the other under), this is what I did:

<?xml version="1.0" encoding="utf-8"?>
  <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">


<RelativeLayout
    android:id="@+id/topLayout"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentTop="true"
    android:background="@color/colorAccent">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="126dp"
        android:text="Existing Client"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:textStyle="bold" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@color/colorPrimary">

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="138dp"
        android:text="New Client"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:textStyle="bold" />

</RelativeLayout>

And the result:

enter image description here

My intention was to use the layouts as buttons that fill a certain width of the screen.

Upvotes: 0

mikekehrli
mikekehrli

Reputation: 341

Thanks guys. I appreciate the feedback.

It didn't work to make the orientation vertical on the parent layout (CoordinatorLayout).

What worked though was to put both elements in a LinearLayout with vertical orientation.

One comment above said I should make first layout an appbarlayout: Yes, I changed that back. I had changed it to a LinearLayout as one of my attempts to get the items to stop painting on top of each other.

I'm not sure that was the correct way to solve this, but it worked. I'll move on. Thanks again.

Upvotes: 1

Praveen
Praveen

Reputation: 91175

Put both Layouts inside an LinearLayout with vertical orientation. thats it.

Edit:

what i mean is you have to create a 3 layout xml files.

  1. Main.xml
  2. layout1.xml --> your first layout
  3. layout2.xml --> your second layout

your main layout file shoul be like this:

<LinearLayout android:orientation="vertical">
<include android:id="+id/lay1" android:layout="@layout/layout1"/>
<include android:id="+id/lay2" android:layout="@layout/layout2"/>
</LinearLayout>

Now your the main layout to your setContentView(R.layout.main)

Upvotes: 6

Related Questions