java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.App0"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
java
package com.koala.app0;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private static Context context;
private Button button;
private View imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplication().getApplicationContext();
button = findViewById(R.id.btn1);
imageView = LayoutInflater.from(context).inflate(R.layout.png,null);
button.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener(){
@Override
public void onClick(View v) {
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
// layoutParams.format = PixelFormat.RGBA_8888;
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// layoutParams.width = 200;
// layoutParams.height = 200;
// layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
// layoutParams.gravity = Gravity.BOTTOM | Gravity.LEFT;
layoutParams.x = 30;
layoutParams.y = 40;
WindowManager windowManager = getWindowManager();
windowManager.addView(imageView,layoutParams);
}
};
public static Context getContext(){
return context;
}
}