Setting a Style for a popUpWindow - android

I'm working on an app that requires something along the lines of a pop up window. I have two different designs I'm considering. One where the popUpWindow opens as a normal popUpWindow with out starting a new activity and another design where the pop up opens as a new activity. In the latter design (where a new activity is open) I can make the new window look really nice by putting the following line in the activity tag of the manifest:
android:theme="#android:style/Theme.Dialog"
However it seems that starting a new activity is not going to work for my overall design. My question is how can I apply this "Dialog" style to a regular popUpWindow?
Below is the code where I instantiate and show the popUpWindow:
PopupWindow popUpWindow;
popUpWindow = new PopupWindow(this);
popUpWindow = new PopupWindow(popUpView, 250, 325, true);
popUpWindow.showAtLocation(mainLinearLayout, Gravity.CENTER, 0, 10);

Here's what I ended up doing which achieved what I wanted:
Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
popUpLayout = Globals.layoutInflater.inflate(R.layout.pop_up_layout,
null);
dialog.setContentView(popUpLayout);
dialog.getWindow().setLayout(275, 350);
dialog.show();

Related

Is there a way when displaying a dialog in Android to tell the app to show the pop up?

I have a full screen app that has buttons that want to have a custom drop down when they are clicked. I was thinking of having a custom dialog show up that will have the options. Is there a way to tell the dialog where to show up?
If you want to create a Dialog window you can change the position of the screen in which it will be displayed by doing something like this:
//Dialog reference
Dialog dialog;
//Instantiate here your dialog
dialog = ... ;
//Get window layout parameters reference
Window window = dialog.getWindow();
WindowManager.LayoutParams windowLayoutParams = window.getAttributes();
//Set the parameters you want
windowLayoutParams.gravity = Gravity.CENTER; //e.g. dialog's will be centered
window.setAttributes(windowLayoutParams);
You can instead use Gravity.BOTTOM or Gravity.TOP to display the dialog accordingly. Alternatively, for more specific positioning you can try setting attributes "windowLayoutParams.x" & "windowLayoutParams.y".

PopupWindow.setFocusable() not working on Lollipop

I'm trying to hide a PopupWindow, when it is clicked outside, and it works perfectly when the setFocusable(true) is set for the PopupWindow. Howewer this solutions seems to be not working on Lollipop devices.
I know there is an other method, when we set a background drawable to the PopupWindow like this:
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
This works as expected, but the background hides the elevation of the PopupWindow.
Is there a proper way to accomplish the following behavior on Lollipop: Make the PopupWindow dismissable on outside click, while keeping it's shadow generated by the setElevation() call?
This is the complete code:
PopupWindow popupWindow = new PopupWindow(binding.getRoot(), UiUtil.dpToPx(context, POPUP_WIDTH_IN_DP),
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setElevation(UiUtil.dpToPx(context, POPUP_SHADOW));
PopupWindowCompat.setOverlapAnchor(popupWindow, true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
int[] position = new int[2];
viewGroup.getLocationInWindow(position);
popupWindow.showAtLocation(viewGroup, Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, position[1]);

Display an image modally with PopupWindow without XML

How do I display a Popup Window without XML and show a custom image?
I have not found any working examples.
Tried this:
public void showMenu() {
PopupWindow myWindow=new PopupWindow(this);
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_layout);
myWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.dashboard_instructions));
myWindow.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
Idea in my mind is you can add custom image as a background of popup window, Not able to test right now, below is the idea hope it will get a clue for you.
PopupWindow myWindow=new PopupWindow(this);
myWindow.showAtLocation(ANY_VIEW_FOR_WINDOW_TOKEN, Gravity.CENTER, 0,0);
myWindow.setBackgroundDrawable(getDrawable(R.drawable.abc));
getDrawable() call requires Target API 21, Hope it will works.

Android: setText() for TextView in PopupWindow not working

Basically, I have a TextView in a layout which I use for a PopupWindow. I show this PopupWindow when a user clicks a button; I want to be able to dynamically change the text in the PopupWindow upon button click. However, findViewById(my_textview).setText() does not seem to do anything, and indeed causes the PopupWindow to no longer show when I click the button.
I can set text from the layout xml fine.
Anyone know what's up with this? Thanks-
I solved the problem. For whatever reason you need to call popup.getContentView().findViewById instead of just findViewById (where popup is your PopupWindow object). I wasn't getting a NullPointerException before so I'm not exactly sure why this fixed the issue but it did.
So the code goes something like:
PopupWindow pw = new PopupWindow(your layout and params here);
((TextView)pw.getContentView().findViewById(R.id.my_textview)).setText("hello there");
pw.showAtLocation(your params here);
You will be able to find the views with the "findViewById" only using the view you inflated the popupWindow before
like this
private View viewPopUp;
private PopupWindow windowPopUp;
//...
//form_popup is the template to the popup
viewPopUp = mContext.getLayoutInflater().inflate(R.layout.form_popup, null);
windowPopUp = new PopupWindow(viewPopUp, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
//...
viewPopUp.findViewById(R.id.popupTopTitle);
viewPopUp.findViewById(R.id.popupMiddleMsg);
//...

Android remove PopupWindow background

I use PopupWindow class for creating custom popup window.
But when I add layout_margin (in my example 15dp) on main there is transparent gray background.
How to remove transparent background?
Please see picture
EDIT
here is my code
window = new PopupWindow(customTool.getContext());
window.setWidth(WindowManager.LayoutParams.FILL_PARENT);
window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
window.setTouchable(true);
window.setFocusable(true);
window.setOutsideTouchable(true);
window.setAnimationStyle(R.style.Animations_PopDownMenu_Left);
window.setContentView(customTool);
window.showAtLocation(customTool, Gravity.NO_GRAVITY, 0, 100);
hmm - try setting on your popup dialog try
yourDiag.setBackgroundDrawable(null);

Categories

Resources