'Disable all screen touches when button is pressed
I am using a constrained layout, and I'm trying to disable screen touches when a button is pressed. I've seen methods of making a transparant layout on top and setting it clickable, but I dont know how to implement it and have been unsuccessful. My activity-main.xml is below:
<?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"
android:clickable="false"
tools:context=".MainActivity">
<Button
android:id="@+id/getimagebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginTop="56dp"
android:text="Upload Image"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="207dp"
android:layout_height="200dp"
android:layout_marginTop="152dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Solution 1:[1]
Try this:
Create a
Viewas a cover/mask:<View android:id="@+id/mask_view" android:layout_width="match_parent" android:layout_height="match_parent" android:alpha="0.5" //half transparent android:background="@android:color/white" android:clickable="true" //absorb the focus, no need "focusable" which is for EditText android:visibility="gone" />Control the visibility:
disableButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { maskView.setVisibility(View.VISIBLE); //set to "GONE" when the task is finished } });
Tutorial: https://youtu.be/qpZdDzJt618 Demo: https://youtu.be/nwTkKx5-_jQ
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sam Chen |
