1

I am developing an app which can draw a window on the top of all apps. basically I want to make an app like Night mode. So to that end, I thought to develop an app that can display a TextView on top of all apps(only to test my idea). I am new to android and this is my second app. Also I am self-learning after following a udacity android basic course.

I have a MainActivity java file with a TextView. when the user touches the text view it sends to an OnScreen java file which is extended from service. my application workes fine up to this point. It goes to the OnScreen class and makes the toast also. after this point I have two problems.

  • after making the toast (which means starts the OnSCreen service class) I cannot touch any other apps. I used "FLAG_NOT_FOCUSABLE". which says in the developer guide it can pass touche events through the window. But it doesn't.

  • it does not shows the text view. actually I do not know how to insert it inside the OnScreen class.

my apps minimum SDK version is 17.

Manifest.xml file

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <service
            android:name=".OnScreen"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </service>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


    </application>

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

</manifest> 

MainActivity


import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }



    public void hello(View v){

        Intent intent = new Intent(this, OnScreen.class);
        startService(intent);

    }
}

OnScreen Activity

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import static com.prabathhewa.eclips.R.id.dimmer;

public class OnScreen extends Service {
    public OnScreen() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_LONG).show();

        TextView layout = inflater.inflate(R.layout.on_screen, (ViewGroup) findViewById());

        View myView = new View(this);
        WindowManager.LayoutParams overlay = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,

                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,

                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,

                PixelFormat.TRANSLUCENT);


        overlay.gravity = Gravity.TOP | Gravity.RIGHT;

        WindowManager windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
        windowManager.addView(myView, overlay);





    }


}

XML with text view which I want to show

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dimmer"
    android:background="#7EFFEB3B"
    tools:context=".OnScreen">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="On screen"
        android:textSize="40sp"
        android:layout_gravity="center"
        android:textColor="#FF0505"/>

</LinearLayout>

before asking this i read

Android System Overlay window not working

Draw Overlay in Android (system wide)

Creating a system overlay window (always on top)

but I could not find exact solutions to my problems.

UPDATE:

I managed to solve my first problem by setting layourparams as

WindowManager.LayoutParams overlay = new WindowManager.LayoutParams(-1, -1, 2006, 280, -3);

also, It did me an additional favor which is it covers the notification bar too. Here i used a xml file as my view. Which defined by

LayoutInflater inflator=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View myView= (View) inflator.inflate(R.layout.on_screen, null);

But i don know how to go into the on_screen.xml and get the text view.

Agnth
  • 11
  • 2

1 Answers1

0

I managed to solve my first problem by setting layourparams as

WindowManager.LayoutParams overlay = new WindowManager.LayoutParams(-1, -1, 2006, 280, -3);

also, It did me an additional favor which is it covers the notification bar too. Here i used a xml file as my view. Which defined by

LayoutInflater inflator=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View myView= (View) inflator.inflate(R.layout.on_screen, null);

But i don know how to go into the on_screen.xml and get the text view.

Agnth
  • 11
  • 2