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.
Related
I have a PopupWindow on my activity, the popupWindow has Edit text. The thing is when i set popup Window outside touchable to false Edit text does not opening keyboard in pop up window in android and i am dismissing the popup by giving cancel button inside popup. Please help thanks in advance.
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = inflater.inflate(R.layout.repeat, null);
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = false;
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
final LinearLayout until = popupView.findViewById(R.id.Until);
Button pop_done = popupView.findViewById(R.id.pop_done);
Button pop_cancel = popupView.findViewById(R.id.pop_cancel);
dropdown = popupView.findViewById(R.id.spinner);
final EditText repetition = popupView.findViewById(R.id.repeat_times);
Try this
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = inflater.inflate(R.layout.repeat, null);
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
final PopupWindow popupWindow = new PopupWindow(popupView, width, height);
popupWindow.setFocusable(true);
popupWindow.update();
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
popupWindow.setOutsideTouchable(false);
final LinearLayout until = popupView.findViewById(R.id.Until);
Button pop_done = popupView.findViewById(R.id.pop_done);
Button pop_cancel = popupView.findViewById(R.id.pop_cancel);
Spinner dropdown = popupView.findViewById(R.id.spinner);
final EditText repetition = popupView.findViewById(R.id.repeat_times);
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);
}
});
}
in the bleow posted code, i am trying to add a horizontal divider after each added view to the linearlayout as shown in the code.
the problem i have is, at run time the divider is not showing
would please let me know why the divider is not showing and how to make it appears?
code:
private void inflateView(String bez, String ges) {
LinearLayout linLay = (LinearLayout) findViewById(R.id.versicherungsListeActivity2mod_linLay_meineDocList_container);
//divider
View viewDivider = new View(this);
viewDivider.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
viewDivider.setBackgroundColor(Color.parseColor("#000000"));
LayoutInflater inflator = this.getLayoutInflater();
View view = inflator.inflate(R.layout.versicherung_docs_row_model, null);//<<<<<< this is the view i want to add to the map as a key
ImageView imgViewLogo = (ImageView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_imgVie_logo);
TextView texVieBez = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_docBezeichnung);
TextView texVieExtraItem = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_addMoreDocs);
TextView texVieGes = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_docGesellschaft);
Button btnMore = (Button) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_btn_more);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
imgViewLogo.setImageDrawable(this.getResources().getDrawable(R.drawable.insurance_details_doc, this.getTheme()));
} else {
imgViewLogo.setImageDrawable(this.getResources().getDrawable(R.drawable.insurance_details_doc));
}
texVieBez.setText(bez);
texVieGes.setText(bez);
btnMore.setVisibility(View.VISIBLE);
linLay.addView(view);
linLay.addView(viewDivider);
}
viewDivider has height WRAP_CONTENT and as the view is empty, its height is calculated to 0.
You have to set desired height of the divider.
int dividerHeight = getResources().getDisplayMetrics().density * 1; // 1dp to pixels
viewDivider.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dividerHeight));
I want to create a popupwindow for fullscreen
i've used the following :
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutt = inflater.inflate(R.layout.loginto,(ViewGroup) findViewById(R.id.window1));
pwindow = new PopupWindow(layoutt,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);
This covers the action bar but not the full screen..
Also LayuotParams.WRAP_CONTENT is supported by api 11+ . i need the solution to work from api level 8.
For the full screen you have to pass the MATCH_PARENT params instead of WRAP_CONTENT
pwindow = new PopupWindow(layout,LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,true);
JAVA Version
View view=getLayoutInflater().inflate(R.layout.dialog_complete_pause_work,null,false);
PopupWindow popupWindow=new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
popupWindow.showAtLocation(view, Gravity.CENTER,0,0);
Kotlin Version:
val view = layoutInflater.inflate(R.layout.your_layout, null, false)
val popupWindow = PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0)
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);