Change focus of activities in android - android

I am using a dialog themed activity in android to show a popup from a application context. The dialog has a transparent theme, but the issue is that I want the underlying activity to have focus and not the popup though the popup must be visble. How do I achieve this in android?

I think you would have to use a Fragment and just make it look like a popup dialog.

You can not switch focus between activities. There's only one activity "on focus" at the time, and is the one that is being displayed at that point. The transparent background doesn't mean you can access the activity below.
If i don't get it wrong, you want to be able to interact with the activity's controls while having a "Dialog" on the screen. Any sort of Dialog class from Android will not help, since they take the focus away. Not really sure about PopupWindow, but i'm guessing will be the same thing as the documentation says "that appears on top of the current activity."
You are going to have to create a custom dialog using a RelativeLayout/FrameLayout within your activity.

Related

Want to show alert dialog across activities

I want to show an Alert Dialog such that it stays on screen even if I go back to previous activity just after showing an alert dialog. Is is possible?
I tried using applicationContext but it does not work.
I don't think so, and I believe this would be a bad user experience. Perhaps you should re-think the design.
You cant do that , because a dialog is attached with a single window context , if window changes the dialog memory will be leaked and it will automatically close the dialog .You can make a same effect by showing the same dialog in both screens .However you will definitely see the transition of dialog with activity .
You can create an activity and give it a dialog theme. That way you dont have to worry about the context. It doesnt make a difference to the user also as it looks just like a dialog.
But I agree with AndyRes, It would be a bad user experience.

Customized alert box or default alert box inside service in Android [duplicate]

I have read really a lot of posts about this topic, however nothing works for me (or doesn't have the effect I wish).
I have a an application, that after logging in starts a background Service (implementation of the Service class). This service syncs itself with a Server and if a new order comes, it creates a notification.
So far, everything works great, I have the notification and the Toast message. However, I would like to have a dialog, that notifies the user about the new order.
As I understood, you can start an activity from within the service, which displays the dialog. This works, but the activity starts on top of the current activity stack and displays the dialog. I have an activity with no view attached and it correctly displays the dialog, however, on a black background.
What I want is to display the dialog on the current activity, causing the actual background(the running activity) to fade and display the dialog.
Is this somehow possible?
We can show dialog from service only if it is a system alert dialog. So, set TYPE_SYSTEM_ALERT window layout parameter to Dialog as follows:
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
But, it needs SYSTEM_ALERT_WINDOW permission. So, don't forget to add this permissin in Manifest file.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Edit:
Better option to show dialog is to start activity as one of the below ways.
Start Activity with Dialog Theme (android:theme="#android:style/Theme.Dialog") -(or)
Start Translucent Activity and show Dialog in it.
Note: You should add Intent.FLAG_ACTIVITY_NEW_TASK to intent
I highly, highly, HIGHLY recommend that you DON'T do this (it goes against Android design and UI guidelines). Notifications are the preferred way to accomplish what you are doing (which it sounds as if you have already accomplished).
That being said, if you must do it, I would recommend just using a Dialog themed activity. That way you don't have to start up a separate dialog. Please see http://developer.android.com/guide/topics/ui/themes.html#ApplyATheme for how to do this.
you can start by learning on how to create an activity that looks like a dialog (no title bar, transparent background, "floating" effect, etc.)
and no, you can't just start a dialog without an activty
No, you can't hijack activity that is not "yours" and command it to show dialog.
Your approach of starting your own activity is the classic one.
You cannot show a dialog. But you can go the alernative way by inflating your customized view so that you can show a dialog on the screen whenver certain conditions are met.

Dialog with outside touchable

In an android application, I want to display a Dialog on screen and in the same time to allow user to click on the application's UI when the the Dialog is open
can this be done?
if no, what can I use instead of Dialog?
I don't think you can do this with Dialog. But you can do it with PopupWindow using PopupWindow.setOutsideTouchable(true);.
Showing up a dialog over your Activity, will make your Activity go into onPause() so you won't be able to actually handle things inside that activity anymore until the dialog is dismissed.
If you want to have some view, overlay your original activities view you'll be looking for into the direction of FrameViews, which can overlap other Views.

How to display a Dialog from a Service?

I have read really a lot of posts about this topic, however nothing works for me (or doesn't have the effect I wish).
I have a an application, that after logging in starts a background Service (implementation of the Service class). This service syncs itself with a Server and if a new order comes, it creates a notification.
So far, everything works great, I have the notification and the Toast message. However, I would like to have a dialog, that notifies the user about the new order.
As I understood, you can start an activity from within the service, which displays the dialog. This works, but the activity starts on top of the current activity stack and displays the dialog. I have an activity with no view attached and it correctly displays the dialog, however, on a black background.
What I want is to display the dialog on the current activity, causing the actual background(the running activity) to fade and display the dialog.
Is this somehow possible?
We can show dialog from service only if it is a system alert dialog. So, set TYPE_SYSTEM_ALERT window layout parameter to Dialog as follows:
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
But, it needs SYSTEM_ALERT_WINDOW permission. So, don't forget to add this permissin in Manifest file.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Edit:
Better option to show dialog is to start activity as one of the below ways.
Start Activity with Dialog Theme (android:theme="#android:style/Theme.Dialog") -(or)
Start Translucent Activity and show Dialog in it.
Note: You should add Intent.FLAG_ACTIVITY_NEW_TASK to intent
I highly, highly, HIGHLY recommend that you DON'T do this (it goes against Android design and UI guidelines). Notifications are the preferred way to accomplish what you are doing (which it sounds as if you have already accomplished).
That being said, if you must do it, I would recommend just using a Dialog themed activity. That way you don't have to start up a separate dialog. Please see http://developer.android.com/guide/topics/ui/themes.html#ApplyATheme for how to do this.
you can start by learning on how to create an activity that looks like a dialog (no title bar, transparent background, "floating" effect, etc.)
and no, you can't just start a dialog without an activty
No, you can't hijack activity that is not "yours" and command it to show dialog.
Your approach of starting your own activity is the classic one.
You cannot show a dialog. But you can go the alernative way by inflating your customized view so that you can show a dialog on the screen whenver certain conditions are met.

Opening Options Menu with Dialog Open

This seems like a pretty simple question, but I'm not sure if it's even possible. Is it possible to allow the MENU button to still bring up the option's menu when a dialog is showing on top? I want to do this because I want to give the user some more options while a dialog is opened.
I don't think that is possible.
Although the docs say that you should be able to do this.
A dialog is always created and displayed as a part of an Activity. You should normally create dialogs from within your Activity's onCreateDialog(int) callback method. When you use this callback, the Android system automatically manages the state of each dialog and hooks them to the Activity, effectively making it the "owner" of each dialog. As such, each dialog inherits certain properties from the Activity. For example, when a dialog is open, the Menu key reveals the options menu defined for the Activity and the volume keys modify the audio stream used by the Activity.
http://developer.android.com/guide/topics/ui/dialogs.html

Categories

Resources