0

I am building an app which has a background service that sends a broadcast message when it meets certain location and time criteria, and a broadcast receiver that opens a new activity when that happens. However, the activity then covers the whole screen, and that's not what I really want to do.

Instead I want the broadcast receiver to trigger some kind of pop-up that only occupies a part of the screen and leaves whatever other apps were running (e.g. phone call, video, web browser etc.) still visible on the rest of the screen.

It doesn't matter what the pop-up is. It could be a snackbar, a dialog box or simply an activity layout that only covers part of the screen. The only criteria are that it must be able to display text and must be able to include OK and CANCEL buttons.

I have tried various suggestions from earlier questions, but can't get anything to work. Can anyone suggest a way of doing this?

I successfully reduced the size of the activity layout to be smaller than the screen using the following code:

WindowManager.LayoutParams params = getWindow().getAttributes();
params.x = -100;
params.height = 200;
params.width = 1000;
params.y = -50;
this.getWindow().setAttributes(params);

but the surrounding screen area is just black. I guess what I have done here is reduced the size of the display for everything to be smaller than the screen, and that's not what I want. I want only to reduce the size of the one activity, with whatever app layout is beneath it showing around it at full screen size.

Christopher S
  • 49
  • 1
  • 4
  • Post the code that you tried. – Prerak Sola Jun 15 '17 at 15:47
  • Prerak - thanks for responding.I tried this: – Christopher S Jun 15 '17 at 16:59
  • Oops, sorry, pressed the wrong button!! I'm afarid I don't have the exact code any more and I can't get back to it. I tried suggestions from here https://stackoverflow.com/questions/6421036/how-to-make-activity-not-covering-full-screen and here http://cases.azoft.com/android-tutorial-floating-activity/ but either I couldn't get them to work (I'm fairly new to android) or they didn't do what I wanted. – Christopher S Jun 15 '17 at 17:08

2 Answers2

0

You could create a heads-up notification

            PendingIntent pendingIntent_OK = PendingIntent.getService(this, 0, new Intent(this, NotifHandler.class), PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent pendingIntent_cancel = PendingIntent.getService(this, 0, new Intent(this, OtherHandler.class), PendingIntent.FLAG_UPDATE_CURRENT);
            final Notification.Builder notif = new Notification.Builder(getApplicationContext())
                    .setContentTitle(getString(R.string.title))
                    .setContentText(getString(R.string.text))
                    .setColor(Color.parseColor(getString(R.color.yellow))) //ok
                    .setSmallIcon(R.drawable.ic_small)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
                    .addAction(R.drawable.icon1, "OK", pendingIntent_OK)
                    .addAction(R.drawable.icon2, "Cancel", pendingIntent_cancel)
                    .setDefaults(Notification.DEFAULT_VIBRATE)
                    .setPriority(Notification.PRIORITY_MAX);

            mNotificationManager.notify(NOTIFICATION_ID, notif.build());

The pendingIntents could call a service and will allow you to handle the different inputs. You could also add an extra to the new intents and handle both inputs in the same class.

MacLean Sochor
  • 435
  • 5
  • 14
0

After more research on stackoverflow I answered my own question! I created an activity (TestActivity) that contains a snackbar which provides my pop-up message. I then created a transparent theme in values/styles.xml as follows:

<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
        <item name="android:background">#00000000</item> 
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
    </style>

and added the style to the TestActivity attributes in my manifest, as follows:

<activity
            android:name=".TestActivity"
            android:theme="@style/Theme.AppCompat.Translucent" />

When I call the snackbar activity, it pops up in front of whatever is already on the screen.

Christopher S
  • 49
  • 1
  • 4