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]);
Related
I'm programmatically adding a PopupView which contains an EditText field to my Activity, which is vertically and horizontally centered on the screen. When the keyboard opens, I want the PopupView to move up, so it is still centered on the visible screen/activity part.
My code:
EditText e = new EditText(super.getContext());
PopupWindow popup = new PopupWindow(e, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popup.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
popup.setOutsideTouchable(true);
popup.setFocusable(true);
popup.showAtLocation(this, Gravity.CENTER, 0, 0);
I've tried many things with windowSoftInputMode for the Activity; I've tried to setSoftInputMode(mode) on the popup - but none of my approaches have worked. Neither my layout nor the Popup change their position when the keyboard opens. (I only want my popup but not the layout to change, though, just pointing it out).
Also the code is placed in a LinearLayout class, in case you are wondering why I'm using this as a View.
Easier to get Android to do all the heavy lifting for you.
Just use:
popup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
So after much research, I finally found a way to accomplish that.
The code for creating the PopupWindow and making it being displayed in the vertical and horizontal center stays the same:
PopupWindow popup = new PopupWindow(
popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
/** ... **/
popup.showAtLocation(this, Gravity.CENTER, 0, 0);
Then the only thing you need is a Listener for the Keyboard (or more general: For Window Height changes). This was actually easier than I thought - and it didn't require any special access like an Activity-object or similar. Even in my independent View-class which only knows the Context (which I didn't want to cast), I was able to accomplish that. Everything you need is only one View-object which has already been added to the layout.
// You can call this method on any view that is added to the layout:
final View root = this.getRootView();
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
Rect r = new Rect();
root.getWindowVisibleDisplayFrame(r);
// Calculate the difference between the original height and the new height
int heightDiff = r.height() - root.getHeight();
// Now update the Popup's position
// The first value is the x-axis, which stays the same.
// Second value is the y-axis. We still want it centered, so move it up by 50% of the height
// change
// The third and the fourth values are default values to keep the width/height
popup.update(0, heightDiff / 2, -1, -1);
}
});
For reference:
Listening to window height changes
Only downside:
This solution may not work when you add a PopupView while the Keyboard is already opened. But in my case, this isn't an expectable scenario anyway.
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.
I want to re-create the following:
Specifically, note the Bluetooth icon, clicking on it brings up what looks like a spinner? Or is it a dialog somehow located correctly? I couldn't find the phone app code anywhere, and am at a loss for how to best implement this.
I actually figured out the solution shortly after posting the question.
The correct solution is to use a PopupWindow. Here is pretty much all you need to do:
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.bluetooth_popup, null);
popupView.findViewById(R.id.bluetooth).setOnClickListener(this);
popupView.findViewById(R.id.speakerphone).setOnClickListener(this);
popupView.findViewById(R.id.earpiece).setOnClickListener(this);
popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// Together these two allow for the popupWindow to be dismissed when touch occurs outside
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
and then to show it anchored to a view:
popupWindow.showAsDropDown(speaker, 0, (int) (-160));
Hope this helps you out.
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();
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);