How to create a fully unclosable ProgressDialog? - java

How to show a ProgressDialog from a background service that is unclosable in any way, displayed above whole screen, above any app, including keys? I mean a dialog exactly like the "Power off.. shutting down" one that is displayed when phone is shutting down, when you can neither do anything to make it disappear nor click anything below it.
PS. If the app needs to be a system app to create such dialog, that's not a problem for me.

Use this
ProgressDialog.setCancelable(false);

Create a full screen activity Using full screen Activity prevent back navigation by overriding onBackPressed()
Disable back button in android

Related

How to close the app when back pressed from main menu

I build an app. There are so many activities. When clicked on the back button of the phone it supposed to exit from the app when in the `MainActivity` but when clicked on back button it switches between the activities visited recently. I added Intents to switch to the activity I need to switch for every activity except `MainActivity`. Now they are working perfectly but I need my app to exit when clicked on the back button of the phone when in the `MainActivity` and also one thing, I have an **Splash Activity** in my app. I made some apps earlier also and there were one `WebView` app and there are only one Activity. In that app also there were a **Splash Activity** and when clicked on the back button it goes to the splash activity and not exiting the app. Also I need to exit the app when clicked on the button.
Please help me!
God Bless!
This can be achieved using finishAffinity()

Android Popup Window vs Android Dialog

I am trying to implement a simple logic in my application where the user is shown a popup (after sometime of application launch). The popup simply shows a TextView with some info message. This message is refreshed every time the application is launched and a new message is shown.
The UI of the popup matches my application UI - here maybe just popup background images is needed. Also one close button (X) is shown at the top right corner of the popup - to close this popup.
Logic of Showing Message: I have a String array having some 100 strings stored in it. I randomly pick one string from this array and populate the popup TextView showing the message. Please suggest if there is any better approach than what I am doing already here. Also is it possible to logic that if one message is picked then the same message is not picked until the other messages are shown at least once?
Logic of Showing Popup: This is what I am not able to implement. I do not want to anchor the popup with any user Event or Button click. I simply wants to show the message after some time - say
Thread.sleep(3000);
Now I have tried to use PopupWindow for this using the below code.
PopupWindow infoPopup;
LinearLayout infoLayout;
TextView infoTextView;
Button infoButton;
infoTextView = new TextView(this);
infoTextView.setText("Testing Popup Text");
infoTextView.setPadding(10,10,10,10);
infoButton = new Button(this);
infoButton.setText("Close");
infoLayout = new LinearLayout(this);
infoLayout.setOrientation(LinearLayout.VERTICAL);
infoLayout.addView(infoTextView);
infoLayout.addView(infoButton);
infoPopup = new PopupWindow(infoLayout,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
infoPopup.setContentView(infoLayout);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
infoPopup.showAtLocation((CoordinatorLayout)findViewById(R.id.main_content),Gravity.CENTER,100,100);
But this popup is showing error at the last line giving null pointer on my
(CoordinatorLayout)findViewById(R.id.main_content)
parameter.
The issue that I am getting are:
First of all, I am not sure if this is the right approach of showing a custom UI popup. I am aware of AlertDialog but not sure which is the best option to go in this case - Please suggest.
Why the CoordinatorLayout is showing null pointer?
How to implement the top right (X) button logic in this Popup ?
1. Yes there are so many options for showing a custom UI popup in Android. You might select one from PopupWindow, AlertDialog or Dialog Activity. You need to decide which suits you best.
If you need to customize your UI a lot and have to show a list or some complex GUI then I would suggest you launch an Activity with theme.Dialog. Just set the theme of the Activity to something like this android:theme="#android:style/Theme.Holo.Light.Dialog". There's a plenty of tutorials for implementing a dialog Activity.
PopupWindow is another tool to customize your custom pop up anywhere in the screen. If you're showing this popup always in the middle of the screen, then I would like to suggest not to use this. The AlertDialog should work fine.
AlertDialog has many variants and as far as I can assume your problem, this one suits you best. You can have a cross button too in the top-right corner of the dialog (You can set the icons anywhere, as you can provide a custom layout to an AlertDialog). Personally I use this library to provide a custom layout to my AlertDialog. You can have a look at this too.
2. The NullPointerException is simple. Your layout doesn't have any id named main_content. Post your logcat if this doesn't solve your problem. Post the layout too.
3. As I've told you earlier, I use the library to provide a custom layout to an AlertDialog and you can have a look at it too. So after implementing this library you can easily design your own layout with a cross button and implement the onClick functionalities easily.
Hope this helps.
Activity with theme Dialog.
This is not a good idea. It looks like a pop-up, but you can't click outside the pop-up to close it.
PopupWindow
It will stop the application. When the user finish clicking the pop-up, the application can work again.
AlertDialog
The best one, it will not stop the application and can be closed by clicking outside the dialog.

how to show a "window/subactivity/dialog" on top of an Activity but keeping the focus in the Activity

My activity A is a game and it does some background operations. When I press a button in the contextual menu, I want to pop up a "small window/dialog/subactivity" (lets call it B) that appears on top of activity A and displays some data about those background operations. But I need to keep the focus on the activity A in order to continue interacting with it (playing the game).
In essence, I want to be able to see the data display by B while playing the game.
I'm not really sure how to implement this. After reading the documentation I have the next conclusions:
I know that I can't use Dialogs because the have the focus. Is it possible to avoid this?
Using a subactivity with a Dialog theme it's another option that looks tempting...but I believe that the subactivity has the focus. Ditto.
My last option is to try to add a LinearLayout with my data to the main Layout, "sharing/splitting" the screen. It's not pretty, but at least I know that this is possible. What I don't like about this approach is that I use the width and height of the screen.
Any suggestions? Solutions?
PS: I found some this thread here that are very related to my question:
Android ==> Sub Activity?
Create an Activity with style Theme.Dialog. This is a normal activity which looks like a dialog, while being modeless and accepting events.
Additional catch is in setting WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL and resetting WindowManager.LayoutParams.FLAG_DIM_BEHIND.
See this answer for complete example: timed modeless dialog
Why not use a FrameLayout that is apart of your Activity? Just ensure that this View has a higher z index (make sure you declare it last in your XML layout or create it at runtime). That way you never leave your Activity.

to launch a Dialog from a Service, should I use Activity or View?

Say I'm running a service and need to pop out a dialog. As we know, it's impossible to launch a dialog directly from a service, therefor we need to launch and activity ( or view ) and then have it launch our dialog.
The dialog, as well as the activity launching it, should NOT obstruct whatever is below it, i.e. what's on screen should not turn gray and any buttons, that are outside of the dialog, should still be clickable.
Can this be achieved with using an activity or would the activity block the view under it anyway?
If so, guess I would have to use a view... since I haven't worked with views before, what would be the right way to initialize it, so that it won't obstruct whatever is under it?
Thanks!
You could have it launch as an activity using the Dialog theme:
http://developer.android.com/guide/topics/ui/themes.html (see heading: Apply a theme to an Activity or application)
Although, no matter what you will probably obstruct the user in some way ;-). This method should only show a minimal dialog box instead of taking up the whole screen though
Can this be achieved with using an activity
No.
would the activity block the view under it anyway?
Yes.
If so, guess I would have to use a view
A view normally is hosted by an activity. A service cannot just create some random view and put it on the screen.
You can attempt to use a Toast with a custom View for a modeless "dialog", but I am uncertain if a service-constructed View will work with that.

Android - Activity behaviour?

I have a small Android application with a TabScreen as my main screen. I have a small problem where my application currently loses focus on the last Activity the user was on.
This is the scenario:
User launches application
Application shows login screen
User enters login details and goes to tab screen
User leaves application via home key
User presses application icon to return to app and the login screen displays again
I want the application to return to the last known displayed Activity in this case.
What I do at the minute is launch the login screen as the Main/Launcher Actvitiy, then when correct credentials are entered launch the tab screen activity and finish the Login activity.
Is there a launch mode or something I should be using to achieve this?
EDIT: More info
The Tab screen is launched simply like this:
Intent intentTabActivity = new Intent(getApplicationContext(), TabScreenActivity.class);
startActivity(intentTabActivity);
Leaving the application through the home button.
I intend to persist the login state and bypass the login but on smaller applications I have created the application returns to the last displayed activity automatically and does not return to the initial Launcher screen every time and I was wondering why this is not the same behavior in this application.
Also as per my other question HERE the behavior seems to be different for debug and signed releases.
This has always been tested on real devices.
This is the correct behavior. Essentially what happens is as soon as the activity goes in the background it is on the mercy of Android DVM. If DVM feels it needs space it will essentially go ahead and kill your application. So once you try to start the application from the icon it actually restarts it from scratch.
However to solve your problem, you should have a checkbox like "Automatically login" or "Remember password" on the login screen and when the user checks it everytime the app opens it should automatically log you in and take to the next screen. This behavior needs to be implemented by you using some sort of persistent storage.
Might be because you are using your onPause() so that your tabs does some action when the tab is passed.
So eventually when the home key is pressed onPause() will be called which might lead you to this problem. Maybe you will have to check your onPause() for this case.
on home button only onStop is called, are you doing finish of activity on onStop? If not it preserves what was the activity which is top of the stack.

Categories

Resources