Michiel
Michiel

Reputation: 120

Android Layout beneath Action Bar

I want to make a screen with multiple buttons, however, it keeps coming out something like this with the buttons under the action bar.Wrong Buttons The buttons are added later programmatically to LinearLayout.

The xml file looks like activity_main.xml:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    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:id="@+id/coordinator_layout"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize">

    </FrameLayout>

    <include
        layout="@layout/content_main"
        android:layout_width="331dp"
        android:layout_height="672dp"
        app:layout_anchor="@+id/container"
        app:layout_anchorGravity="center"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

content_main.xml:

<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/mainScreen"
    tools:context=".MainActivity"
    >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="415dp"
        android:layout_height="606dp"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        tools:context=".MainActivity" />

    <Button
        android:id="@+id/button"
        android:layout_width="108dp"
        android:layout_height="86dp"
        android:layout_marginBottom="2dp"
        android:background="@android:drawable/ic_input_add"
        android:onClick="newScheme"
        android:tag="0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.997"
        tools:ignore="OnClick" />


</androidx.constraintlayout.widget.ConstraintLayout>

How can I get the buttons beneath the action bar instead of under it?

Upvotes: 1

Views: 53

Answers (1)

iknow
iknow

Reputation: 9862

I created something like this but I changed some hardcoded sizes (I don't know what You do but I think usually it's not a good practice).

Main XML

<androidx.coordinatorlayout.widget.CoordinatorLayout
    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:id="@+id/coordinator_layout"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

    <include
        layout="@layout/content_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_anchor="@+id/container"
        app:layout_anchorGravity="center" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

content_main.XML

<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/mainScreen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:context=".MainActivity">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button 1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button 2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button 3" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button 4" />

    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="108dp"
        android:layout_height="86dp"
        android:background="@android:drawable/ic_input_add"
        android:onClick="newScheme"
        android:tag="0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="OnClick" />

</androidx.constraintlayout.widget.ConstraintLayout>

Here I added buttons but if You add it programmatically just delete it.

And an effect:

enter image description here

Upvotes: 1

Related Questions