Problem with button layers (Android studio)

2020-03-27 android

I have a problem with layers of buttons. I want to put "Button" under those other buttons. So if I click on the "+2" button, the "+2" reacts (not that "button").

...I don’t know how I can help more, if anything else needs to be sent, just write.

Can somebody help me with this problem?

Picture of my project: enter image description here

Thanks in advance :)

//  XML

    <?xml version="1.0" encoding="utf-8"?>
    <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"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/T1"
        android:layout_width="77dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="60dp"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="60dp"
        android:layout_marginRight="60dp"
        android:layout_marginBottom="696dp"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/B1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="10dp"
        android:onClick="mod2"
        android:text="+2"
        app:layout_constraintBottom_toTopOf="@+id/B2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/B2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:onClick="mod4"
        android:text="+4"
        app:layout_constraintBottom_toTopOf="@+id/B3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/B1" />

    <Button
        android:id="@+id/B3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:onClick="mod6"
        android:text="+6"
        app:layout_constraintBottom_toTopOf="@+id/B4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/B2" />

    <Button
        android:id="@+id/B4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="480dp"
        android:onClick="mod8"
        android:text="+8"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/B3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="407dp"
        android:layout_height="735dp"
        android:height="3dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Answers

You can just do that manually in the layout editor.

First place "button", then on top of it, place the "+2" with the same height and width.

When, you will click on them, only the top one, "+2" will get clicked and not the "button".

Just reorder your views in the layout. Put "button" first in the hierarchy, then your other buttons.

UPD: replace the "button" with TextView, does it make sense in your case?

Related