Display an image modally with PopupWindow without XML - android

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.

Related

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]);

Is there a way to move the calendar of a datePicker in spinner mode?

I'm looking for something like this. I have plenty of vertical screen real estate, and I like how the calendar fits in with the app, but I want the user to have a spinner for the year as well. I just want to know if there's a way to move the calendar (if that's possible) so that the year spinner would be able to be seen.
Just add the pickerMode to your DatePicker in your XML file:
<DatePicker
...
android:datePickerMode="spinner" />
Or you can add a popup dialog which is more customizable. If you use a popupdialog, then you have to handle more cases compared to spinner.
And in onclick event of the view, attach popupwindow
PopupWindow yourDatePopup = datePopupWindow();
//this line will give a spinner effect
yourDatePopup.showAsDropDown(clickableView, -7, 0);
public PopupWindow datePopupWindow() {
PopupWindow popupWindow = new PopupWindow(this);
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
// set the list view as pop up window content
//Define your dateview
popupWindow.setContentView(yourDateView);
popupWindow.setOutsideTouchable(true);
return popupWindow;
}
Note:- Just to help, not exact code

Spinner with Icon and Text like Phone App on Android (with Bluetooth)

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.

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