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.
Related
I'm trying to create an overlay that is triggered when a button is pressed. This overlay is supposed to allow the user to add their contact and I was wondering how can I use fragments to get this effect like you can see in this mockup.
I am in a dilemna over using fragments is the right choice. My reasoning being that I only need to have it do one task that is adding contacts, and thus I do not need a whole activity.
This is what I have on the main activity right now
I would really appreciate any help on understanding how to make this work.
You can use a DialogFragment.
It behaves like a normal Fragment for the most part. Here is a guide for a basic implementation https://guides.codepath.com/android/using-dialogfragment
They appear automatically in the center of the screen. To get it a bit lower like in your mockup you can change where it is in the window. Here is an answer showing such Position of DialogFragment in Android
I'm new to android. Ok so what I want to do is dim everything except the lottieAnimationView whenever I make the lottieAnimationView visible and also make everything back to normal when I turn lottieAnimationView gone. I tried a piece of code but it doesn't work
Edit: Maybe its possible through making the lottieanimationview visible inside an alert dialog box so can someone provide the code for that.
You need to create Custom Dialog which contains LottieAnimationView through which you can achieve desire output.
You can get an idea that how to create Custom Dialog using below link.
How to create a Custom Dialog box in android?
Dialog with transparent background in Android
I have an android dialog
I saw this tutorial for swiping a dialog in and out.
how can I filp a dialog when the user clicks on the dialog.
I want to actually show the user two dialog with different content and trasit with flip between them
Try using the code from google or combine it's xml if it works, saw also good example here
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.
This is my first question at stackoverflow :-). However, I've started to Android programming some weeks ago and now I have get into a problem that I will describe below.
Problem:
I have a background-image for the whole screen. Inside this LinearLayout I have another LinearLayout with Buttons inside. How can I (without to start a new activity) load another buttons and make the first buttons disappear. The Buttons have a drawable background-image assigned to the specific button.
Like: I have a menu; play, options and quit. When I for example click on Options, the Options should load, and the play-button, options-button and quit-button should disappear.
All I want to become is to remain the background-image without to create a new activity and always can load a bunch of new buttons depending on the button that was clicked on.
For now I have created a new activity for the new buttons that should appear for example: Exit -> Do you want to Exit? Yes or no., then I also have done so the new acitivty loads another xml-layout-resource file, with the same background as before. This is what I want to change to a smoother resolution.
Make two buttons with your second layout. use button1.setVisibility(View.GONE) method to set the visibility of the button. check this link