Search code examples
androidandroid-linearlayout

Placing button over two different LinearLayouts


So, I would like to add a button with the text "Already Registered?" on the middle bottom screen but my code contains a two different LinearLayouts for the half left and the half right side. I want the button to be half on the left Linearlayout and half on the right. In addition, they are clickable so as far as I concern I must have them in my current layout and not include them.

what I currently have:

enter image description here

What I want to get:

enter image description here

my code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    tools:context=".MainActivity"
    android:weightSum="2"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/customerLinearLayout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_weight="1"
        android:background="@color/lightblueMainActivity"
        android:onClick="customerSignUp">

        <TextView
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:textColor="@color/orangeMainActivity"
            android:text="@string/customerMainActivity"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/electricianLinearLayout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_weight="1"
        android:background="@color/orangeMainActivity"
        android:onClick="electricianSignUp">

        <TextView
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:textSize="24sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/lightblueMainActivity"
            android:text="@string/electricianMainActivity"/>
    </LinearLayout>
</LinearLayout>

THANKS IN ADVANCE!


Solution

  • There are other types of ViewGroup besides LinearLayout which allow you to achieve a "split screen" effect, but let's keep it simple and use a weighted LinearLayout to divide the screen.

    The child Views however can be TextViews (no intermediate ViewGroup required) because you can let them have a background color and also control the text alignment.

    Since you want the Button to overlap both parts of the screen, you can put it and the LinearLayout into a FrameLayout (I used a TextView but the attributes are basically the same):

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="2">
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:text="Customer"
                android:textColor="#aaaaaa"
                android:textSize="20sp"
                android:gravity="center"
                android:background="#0000ff"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:text="Electrician"
                android:textColor="#666666"
                android:textSize="20sp"
                android:gravity="center"
                android:background="#ffab00"/>
        </LinearLayout>
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|bottom"
            android:layout_marginBottom="24dp"
            android:textColor="#ffffff"
            android:textSize="20sp"
            android:text="Already Registered?"/>
    </FrameLayout>
    

    enter image description here