1

I am trying to create a Lock Screen Dialog Notification in android like whatsapp reply

Here is what i have tried

public void sendNotification(String msg, Intent i, String title)
    {
        mNotificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);

        String replyLabel = "Reply";
        RemoteInput remoteInput = new RemoteInput.Builder("KEY_REPLY")
                .setLabel(replyLabel)
                .build();

        PendingIntent contentIntent = PendingIntent.getActivity(context, 100, i,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
                R.drawable.ic_phone, replyLabel, contentIntent)
                .addRemoteInput(remoteInput)
                .setAllowGeneratedReplies(true)
                .build();

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
                        .setContentTitle(title)
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText("Content Hidden"))
                        .setContentText(msg)
                        .addAction(replyAction)
                ;

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.cancel(Constants.PUSH_ID);
        mNotificationManager.notify(Constants.PUSH_ID, mBuilder.build());
}



                Intent myIntent = new Intent(MainActivity.this, NotificationActivity.class);                ApiManager.getInstance().mChatManager.sendNotification("message", myIntent, "Muhammad Umar");

For making it appear transparent activity i have tried

<style name="Theme.CustomTranslucent" parent="android:style/Theme.Translucent">
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:backgroundDimAmount">0.5</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:background">@android:color/transparent</item>
</style>

However i am getting result

enter image description here

The background is dim white and i cant set it to transparent. Is my approach correct? why the activity is not transparent.

Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193

4 Answers4

0
<style name="AppTheme.NoActionBar.Transparent">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@color/transperent_green</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>

<color name="transperent_green">#CC07D1C8</color>


<activity
    android:name=".YourActivityName"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme.NoActionBar.Transparent"></activity>
Pavya
  • 6,015
  • 4
  • 29
  • 42
0

From my understanding, what I think is you need "System_Alert_Window" permission. Facebook, viber uses this permission to open a dialog, activity floating/over another application. Check this SO link . Hope that helps.

Community
  • 1
  • 1
androCoder-BD
  • 498
  • 7
  • 13
0

There is a better approach for this. Make your Activity a Dialog and then make its background transparent.

You can acheive this by making your Dialog's background transparent at runtime.

NotificationDialog dialog = new NotificationDialog(MainActivity.class);
dialog .getWindow().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(MainActivity.this, android.R.color.transparent)));
dialog.show();

UPDATE

Go with Activity, change the theme of your NotificationActivity to @style/Theme.AppCompat.Dialog from Manifest. Now your activity will appear as dialog. You can edit this as per your need. Like

public class NotificationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //If you don't want to show title
        //Use requestWindowFeature(Window.FEATURE_NO_TITLE) if you are extending Activity
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_notification);

        //Play with this if you want to change  the width and height of your `Dialog`
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        int width = displayMetrics.widthPixels;
        int height = displayMetrics.heightPixels;

        getWindow().setLayout(width-20, height-100);

    }
}
Waqas Ahmed Ansari
  • 1,683
  • 15
  • 30
0

simple add this in activity in mainfiest its a dilog box style theme .

        <activity
        android:name="com.and.your activity"
        android:theme="@android:style/Theme.Dialog" >
            </activity>
android_jain
  • 788
  • 8
  • 19