Search code examples
androidbackgroundonclicklistenertransparent

Click listeners on a transparent layout with background items click-able


I have created an activity which is transparent. The background is click-able, anything can be performed on mobile with activity opened. Now I want to set click listeners on the textview that is inside the transparent activity. I have searched but didn't find anything.

Here is the code to my layout :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    android:background="@android:color/transparent">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

Here is the Java code that i am using :

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.tv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
            }
        });


    }

Sample

I want the hello world to be click-able as well as other apps for example, Chrome, messaging, menu etc.


Solution

  • What you are trying to achieve is not possible with an activity. When you use activity it covers all of the screen and any touch done on screen will only accessible by that activity(If it transparent or not, this does not matter).

    My suggestion is, try to use a widget instead of activity. It may fulfill your requirement. I suggest you to use any floating widget or permanently place that widget into center of the screen.

    Below link may helpful for you.

    Chat Widget

    Floating widget