Android: Java - implement Screenshot code to floating button

39 Views Asked by At

I have two programs which are working. But I need to implement screenshot function into floating button (second) code.

In the first there is made screenshot after button click:
MainActivity.java

package com.example.screenshotapp;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_CODE_PERMISSION = 101;

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

        Button screenshotButton = findViewById(R.id.screenshotButton);
        screenshotButton.setOnClickListener(v -> {
            if (checkPermission()) {
                takeScreenshot();
            } else {
                requestPermission();
            }
        });
    }

    private boolean checkPermission() {
        int permission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        return permission == PackageManager.PERMISSION_GRANTED;
    }

    private void requestPermission() {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_PERMISSION);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                takeScreenshot();
            } else {
                Toast.makeText(this, "Permission denied. Unable to take screenshot.", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void takeScreenshot() {
        try {
            View rootView = getWindow().getDecorView().getRootView();
            rootView.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(rootView.getDrawingCache());
            rootView.setDrawingCacheEnabled(false);

            String fileName = "screenshot_" + new Date().getTime() + ".png";
            File screenshotFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName);

            FileOutputStream outputStream = new FileOutputStream(screenshotFile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
            outputStream.flush();
            outputStream.close();

            Toast.makeText(this, "Screenshot saved: " + screenshotFile.getAbsolutePath(), Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "Error taking screenshot.", Toast.LENGTH_SHORT).show();
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.screenshotapp">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true">
        <activity android:name="com.example.screenshotapp.MainActivity"
            android:exported="true"
            android:theme="@style/Theme.AppCompat">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/screenshotButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Udělat screenshot"
        android:layout_centerInParent="true" />

</RelativeLayout>

And there is second code - floating button widget which is visible also through other apps: MainAcivity.java

package com.example.mywidget2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;



public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        startFloatingWidgetService();
    }
    private void startFloatingWidgetService() {
        Intent intent = new Intent(this, FloatingWidgetService.class);
        startService(intent);
    }
}

FloatingWidgetService.java

package com.example.mywidget2;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import com.example.mywidget2.R;

public class FloatingWidgetService extends Service {


    private WindowManager mWindowManager;
    private View mFloatingWidgetView;
    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    // Method to request the SYSTEM_ALERT_WINDOW permission
    private void requestOverlayPermission() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
            intent.setData(Uri.parse("package:" + getPackageName()));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }

    // Method to create the floating widget
    private void createFloatingWidget() {

        // Inflate the layout for the floating widget
        mFloatingWidgetView = LayoutInflater.from(this).inflate(R.layout.floating_widget_layout,
                null);

        // Initialize the button on the floating widget
        Button button = mFloatingWidgetView.findViewById(R.id.floating_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Handle the button click event
                showTextDialog();
                // You can perform other actions here as needed
            }
        });

        // Initialize the WindowManager
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        // Set up WindowManager.LayoutParams for the floating widget
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT
        );

        // Set the gravity of the floating widget (position on the screen)
        params.gravity = Gravity.BOTTOM | Gravity.END;

        // Add the floating widget to the WindowManager
        mWindowManager.addView(mFloatingWidgetView, params);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        // Check if the permission is granted (only for API level 23 and higher)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
            // You don't have permission to draw overlays, so request it
            requestOverlayPermission();
        } else {
            // Permission is granted, proceed with creating the floating widget
            createFloatingWidget();
        }



    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mFloatingWidgetView != null && mWindowManager != null) {
            mWindowManager.removeView(mFloatingWidgetView);
        }
    }

    private void showTextDialog() {

    }
}

AndroidManifest.xml

<?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.WRITE_EXTERNAL_STORAGE" />

    <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.Mywidget2"
        tools:targetApi="31">

        <service
            android:name=".FloatingWidgetService"
            android:enabled="true"
            android:exported="true" />

        <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>

floating_widget_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/floating_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:background="@android:color/holo_red_light"
        android:text="asdasda"
        android:textColor="#212121" />

</LinearLayout>
0

There are 0 best solutions below