Adam Lee
Adam Lee

Reputation: 586

Android: How to set AlertDialog to Material Design when launched from an Activity with a Translucent theme?

I am developing an app which triggers the following steps.

  1. Uses AlarmManager to trigger a BroadcastRecevier 5 seconds after launch.
  2. The BroadcastReceiver then sets off an Activity through an Intent.
  3. Within that Activity, an Alert Dialog is launched.

When I launch the application on my Motorola Moto E6 (Android 9), the Alert Dialog that is triggered looks like the following:enter image description here

However, I want the AlertDialog to look like the following dialog (how would I go about making it look like that):

enter image description here

Because the Activity cannot have a UI (it should just show a dialog over the screen), I believe it needs to have the following definition in AndroidManifest.xml, in which the theme is specified as translucent:

<activity
    android:name=".AlarmDialog"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    android:screenOrientation="fullSensor"
    android:showOnLockScreen="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.alertdialog">
        <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">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <activity
            android:name=".AlarmDialog"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:screenOrientation="fullSensor"
            android:showOnLockScreen="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver
            android:name=".AlarmReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

MainActivity.kt:

package com.example.alertdialog

import android.app.Activity
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private fun generateAlarmPendingIntent(context: Activity): PendingIntent? {
        val intent = Intent(context, AlarmReceiver::class.java)
        val alarmId: Int = (Math.random() * (100 - 1 + 1) + 1).toInt();

        return PendingIntent.getBroadcast(context, alarmId, intent, 0)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Trigger an alarm 5 seconds after the app launches
        val alarmManager = applicationContext.getSystemService(Context.ALARM_SERVICE)
                                       as AlarmManager

        alarmManager.setExactAndAllowWhileIdle(
            AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + 5000,
            generateAlarmPendingIntent(this)
        )
    }
}

AlarmReceiver.java:

package com.example.alertdialog;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context k1, Intent k2) {
        // Trigger the Activity which will launch the Alarm's dialog
        Intent i = new Intent(k1, AlarmDialog.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        k1.startActivity(i);
    }
}

AlarmDialog.java:

package com.example.alertdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.WindowManager;

public class AlarmDialog extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON   |
                             WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                             WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                             WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

        // Construct and display the alarm's alert dialog
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Title");
        alertDialog.setMessage("Message");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        alertDialog.show();
    }
}

Upvotes: 0

Views: 62

Answers (2)

thebadassdev
thebadassdev

Reputation: 166

If you want to modify the alert dialog as per your wish. You can also use

builder.setView(inflatedView)

This will help you to make your own custom alert dialog where you can design the alert dialog as per your design and add as many buttons as required. Code will look like below. But you need to inflate your view before you pass it.

AlertDialog alertDialog = new AlertDialog.Builder(this).setView(inflatedView).create()

And for setting the background of the dialog you can use this.

alertDialog.window.setBackgroundDrawableResource(R.layout.background)

Upvotes: 0

Furkan Yurdakul
Furkan Yurdakul

Reputation: 3167

Change this line

AlertDialog alertDialog = new AlertDialog.Builder(this).create();

to this:

AlertDialog alertDialog = new AlertDialog.Builder(this, R.style.Theme_MaterialComponents_Light_Dialog_Alert).create();

By passing a 2nd argument you allow the theme to be specified. If it's a root theme (a.k.a not derived from application themes) it will directly use its references.

Upvotes: 1

Related Questions