PopupWindow does not show as expected - android

I am trying to create and display a PopupWindow:
private void setupPopupWindow() {
TextView popTextView = new TextView(this);
popTextView .setText("xxxxxxxxxxxxx");
mPopupWindow = new PopupWindow(popTextView );
mPopupWindow.setTouchable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.showAtLocation(anotherViewInMyActivity, Gravity.CENTER, 120, 120);
}
But the popupwindow does not show.
Did I miss anything?

Try to set size for your TextView.
setWidth(120);
setHeight(120);
If popup window will ignore outside touches, just set background
setBackgroundDrawable(new ColorDrawable(Color.WHITE));

Related

PopupWindow doesn't show

I have a PopupWindow in my app.
That is how I initialize it:
final FrameLayout frameLayout = new FrameLayout(context);
frameLayout.setLayoutParams(new FrameLayout.LayoutParams(200, 200));
hashTagsWindow = new PopupWindow(frameLayout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
hashTagsWindow.setBackgroundDrawable(new ColorDrawable(Color.BLACK));
And that is how I try to show it:
hashTagsWindow.showAsDropDown(binding.hash, 20, 20);
But my PopupWindow doesn't display itself.
What I already tried to solve a problem?:
Wrap .showAsDropDown(...)to the .post(Runnable) and .runOnUiThread(Runnable)
All the methods of PopupWindow which should show it.
PopupWindowCompat.showAsDropdown(...) method.
All this things didn't help me.
Meanwhile a PopupMenu class correctly displays in the same context. But I need PopupWindow.
What should I do to show PopupWindow?
Follow this method, May help you.
private void showPopupWindow(View view){
LayoutInflater mLayoutInflater=LayoutInflater.from(this);
View mView=mLayoutInflater.inflate(R.layout.pop_up_layout, null);
mPopupWindow = new PopupWindow(mView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
// mPopupWindow=new PopupWindow(mView,,LayoutParams.WRAP_CONTENT);
mPopupWindow.setContentView(mView);
Button mBtnCancel=(Button) mView.findViewById(R.id.btn_cancel);
mBtnCancel.setOnClickListener(this);
mPopupWindow.showAsDropDown(view, 0, 0, Gravity.LEFT);
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
Drawable d = new ColorDrawable(Color.WHITE);
getWindow().setBackgroundDrawable(d);
}
});
}

Unwanted padding around buttons in dialog

The Problem:
You can clearly see the padding around the button
The Code:
public void startGameDialog(){
Context context = GameBoardActivity.this;
ContextThemeWrapper ctw = new ContextThemeWrapper(context, R.style.AppBaseTheme);
AlertDialog.Builder startGameDialog = new AlertDialog.Builder(ctw);
startGameDialog.setTitle(getResources().getString(R.string.whats_your_name));
LinearLayout dialogLayout = new LinearLayout(context);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
dialogLayout.setOrientation(LinearLayout.VERTICAL);
final EditText newName = new EditText(context);
newName.setLayoutParams(params);
newName.setText("");
final Button submit = new Button(context);
submit.setText(getString(R.string.done));
submit.setLayoutParams(params);
dialogLayout.addView(newName);
dialogLayout.addView(submit);
startGameDialog.setView(dialogLayout);
final Dialog dialog = startGameDialog.create();
OnClickListener onClick = new OnClickListener() {
#Override
public void onClick(View v) {
GameBoardActivity.NAME = newName.getText().toString();
dialog.dismiss();
setUpPlayers();
}
};
submit.setOnClickListener(onClick);
dialog.show();
dialog.setCancelable(false);
}
Attempted solutions (Both failed):
Using the builder .create() method to build to AlertDialog and setting .setView(dialogLayout, 0, 0, 0, 0).
Removing parent's padding by trying ViewGroup parent = (ViewGroup) dialogLayout.getParent() then trying parent.setPadding(0, 0, 0, 0); (This returned a NullPointerException error because you can't set padding for Dialog nor AlertDialog.
Any other ideas!?
Thanks in advance!
JRad the Bad
Default Button in android has a natural padding. You can remove it by either changing the background or the style on the layout XML.
Set minimum Hight & minimum width to "0" then padding will remove for ex
submit.setMinHeight(0);
So I figured another solution:
Keep in mind, this could break things if the 9 Patch images in the SDK get changed in the future, but otherwise, it works seemlessly to remove the 9Patch images' padding and works with all API's.
Here's the code I changed:
//set to compensate for 9 patch padding on button
dialogLayout.setPadding(-5, 5, -5, -8);
//set to compensate for dialogLayout padding affecting the EditText view
FrameLayout editTextWrapper = new FrameLayout(context);
editTextWrapper.addView(newName);
editTextWrapper.setPadding(5, 0, 5, 0);
dialogLayout.addView(editTextWrapper);
dialogLayout.addView(submit);

Overlay dialog - align view

I want to implement a help overlay for my app.
It should look like this: How do I create a help overlay like you see in a few Android apps and ICS?
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.coach_mark);
dialog.setCanceledOnTouchOutside(true);
//for dismissing anywhere you touch
View masterView = dialog.findViewById(R.id.coach_mark_master_view);
masterView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
This works very well. The RelativeLayout of coach_mark.xml is displayed over my existing layout which was set via setContentView().
Now I want to align the views of coach_mark.xml near the according views of the existing layout, in order to react dynammically to the resolution and screen size. This is what I have tried:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW,R.id.view_of_main_layout);
view_of_the_overlay.setLayoutParams(lp);
But the view is displayed just in the middle of the screen.
The only thing I could archieve is to align the view to bottom of screen, but that is not what I want to do.
Thanks for your help.
You can use PopupWindow :
PopupWindow popup=new PopupWindow(this);
popup.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
popup.setWindowLayoutMode(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
popup.setContentView(R.layout.coach_mark);
View view=findById(R.id.view_of_main_layout);
int[] viewLocation=new int[2];
view.getLocationInWindow(viewLocation)
popup.showAtLocation(view, Gravity.TOP|Gravity.LEFT, viewLocation[0],viewLocation[1]);
you can also adjust to popup location with the last two variables which are the X and Y offest. Their values can also be negative.

Pop up window to display some stuff in a fragment

I am trying to make something like a pop-up window, that would appear when clicked on a view in a fragment. I want this pop-up window or whatever, to not make the fragment dark, like a Dialog Fragment does.
And I also want the pop up to be positioned where the view is clicked.
Would be good if it has its own activity and layout so I can do some custom changes in it.
Can you plese show me some sample code?
The following should work perfect in accordance with your specification. Call this method from inside onClick(View v) of OnClickListener assigned to the View:
public void showPopup(View anchorView) {
View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);
PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// Example: If you have a TextView inside `popup_layout.xml`
TextView tv = (TextView) popupView.findViewById(R.id.tv);
tv.setText(....);
// Initialize more widgets from `popup_layout.xml`
....
....
// If the PopupWindow should be focusable
popupWindow.setFocusable(true);
// If you need the PopupWindow to dismiss when when touched outside
popupWindow.setBackgroundDrawable(new ColorDrawable());
int location[] = new int[2];
// Get the View's(the one that was clicked in the Fragment) location
anchorView.getLocationOnScreen(location);
// Using location, the PopupWindow will be displayed right under anchorView
popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY,
location[0], location[1] + anchorView.getHeight());
}
The comments should explain this well enough. anchorView is the v from onClick(View v).

Android PopupWindow and WRAP_CONTENT don't work together

I open a popu window like this:
mInfoPopup = new PopupWindow(layout, 400, 600, true);
mInfoPopup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
The window then gets the exact size specified (400x600) and does NOT adjust its size to its content. What do I need to change so that the popup window will actually wrap around its contents?
Simply changed the way you create it by:
PopupWindow popupMenu = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
PopupWindow popupWin = new PopupWindow(mContext);
// WRAP_CONTENT works well for height, but not for width.
popupWin .setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
// Measure layout here, then we can get measureHeight.
layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popupWin .setWidth(layout.getMeasuredWidth());
popupWin .setContentView(layout);
popupWin .showAsDropDown(anchorView,0 ,0, Gravity.NO_GRAVITY);
I found a solution that works for me (I am working with API 10), you can use it:
popupWindow.setWindowLayoutMode(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(1);
popupWindow.setWidth(1);
If you don't set height/width or set 0 it won't work. Example:
...
private Button button;
protected void onCreate(Bundle savedInstanceState) {
...
attached_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_layout, null);
final PopupWindow popupWindow = new PopupWindow(getApplicationContext());
popupWindow.setContentView(popupView);
popupWindow.setWindowLayoutMode(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(1);
popupWindow.setWidth(1);
popupWindow.setFocusable(true);
Button dismiss = (Button) popupView
.findViewById(R.id.dismiss);
dismiss.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(button);
}
});
I hope to help you.
Following code worked for me.
LayoutInflater layoutInflater = LayoutInflater.from(context);
View layout = layoutInflater.inflate(R.layout.popup_layout, null);
PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(ListPopupWindow.WRAP_CONTENT);
popup.setHeight(ListPopupWindow.WRAP_CONTENT);
Here, used popup.setWidth(ListPopupWindow.WRAP_CONTENT) instead of popup.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT) in my case.
don't use RelativeLayout in layout, replace LinearLayout, maybe it's working.
popview = LayoutInflater.from(context).inflate(R.layout.pop_citypicker, null);
popwindow = new PopupWindow(popview, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Below code is use-full for decreasing layout width and height pop-window
and i used in that custom layout with aligned center
final PopupWindow popupWindow = new PopupWindow(getActivity());
PopupMenu popup = new PopupMenu(getActivity(), v, Gravity.CENTER);
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.popmenu1t1, null);
l1 = (LinearLayout) view.findViewById(R.id.atoz);
l2 = (LinearLayout) view.findViewById(R.id.ztoa);
l3 = (LinearLayout) view.findViewById(R.id.phtol);
l4 = (LinearLayout) view.findViewById(R.id.pltoh);
l5 = (LinearLayout) view.findViewById(R.id.dhtol);
l6 = (LinearLayout) view.findViewById(R.id.dltoh);
l7 = (LinearLayout) view.findViewById(R.id.dotor);
l8 = (LinearLayout) view.findViewById(R.id.drtoo);
int width = 900;
int height = 400;
try {
WindowManager wm = (WindowManager)view.getContext().getSystemService(Context.WINDOW_SERVICE);
width = wm.getDefaultDisplay().getWidth();
height = wm.getDefaultDisplay().getHeight();
} catch (Exception e) {
e.printStackTrace();
}
popupWindow.setWidth(width*3/6);
popupWindow.setHeight(height*3/8);
popupWindow.setFocusable(true);
popupWindow.setContentView(view);
popupWindow.setBackgroundDrawable(null);
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
These two lines not use full for now
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
Happy coding friends..
final PopupWindow newPartWindow = new PopupWindow(newPartIconView, 1, 1, false);
newPartWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
This worked for me, we need the width/height to be initialized at 1 to make the wrap content work.
I had a similar problem but even setting the size in new PopupWindow(...) wasn't working.
I had an image as background of the popup and I solved putting it as background of the layout (I'm using the new ConstraintLayout) with android:background. The image is a 9-Patch image so it resize in a nice way.
For me it depended strangely on what did I pass as a contentView to the PopupWindow constructor:
new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
When it was a LinearLayout without child views and a fixed width and height, passing the width=height=WRAP_CONTENT to the PopupWindow constructor did not work - I got 0x0 size.
When I added a child to that light the width=WRAP_CONTENT started working as expected, but the height=WRAP_CONTENT was working more like MATCH_PARENT, taking the full height of the screen.
After I changed the contentView to a FrameLayout or CardView it started working with width=height=WRAP_CONTENT properly.

Categories

Resources