Skip to content Skip to sidebar Skip to footer

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 addContentVie

Solution 1:

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)


Solution 2:

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.


Solution 3:

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.


Post a Comment for "Layouts On Top Of Each Other"