I am currently creating a popup window by this code:
View popupView = inflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setAnimationStyle(R.style.Animation);
LinearLayout linearLayout = popupView.findViewById(R.id.linearLayout1);
linearLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
popu = false;
}
});
ImageView imageView = popupView.findViewById(R.id.imageView1);
imageView.setImageResource(drawables[position]);
popupWindow.showAtLocation(imageView, Gravity.CENTER, 0, 0);
popu = true;
However, I want the popup to have multiple pages, like here:
How can I implement a multi-paged popup into my app? can I add a ViewPager into my PopupWindow or I need to switch to a Dialog instead?
Use DialogFragment instead.
https://developer.android.com/reference/android/app/DialogFragment.html
Inside of it you will need a ViewPager with your multiple pages
Add page indicators to make it look similar
You can use a library or TabLayout for the indicators:
How do you create an Android View Pager with a dots indicator?
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);
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));
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.
Here is my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
AlertDialog alertChoice = new AlertDialog.Builder(context).create();
alertChoice.setTitle("Title");
alertChoice.setView(ViewDialogScreen("Test"));
alertChoice.show();
}
private View ViewDialogScreen(String strText) {
LinearLayout llay = new LinearLayout(context);
llay.setLayoutParams(new LayoutParams(320, 400));
TextView tv = new TextView(context);
tv.setText(strText);
llay.addView(tv);
return llay;
}
I got the output like the above.
I need to show the AlertDialog in full screen / 95% of the Screen size.
I need to handle more fields in the Dialog.
How do I enable the HorizontalScrollview in the AlertDialog?
to get the scrolling behaviour, add a surrounding ScrollView to your layout, which would make your ViewDialogScreen method to this:
private View ViewDialogScreen(String strText) {
ScrollView scroll = new ScrollView(context);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
LinearLayout llay = new LinearLayout(context);
llay.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TextView tv = new TextView(context);
tv.setText(strText);
scroll.addView(llay);
llay.addView(tv);
return scroll;
}
Now try adding some more fields and it should scroll.
I am not sure this but you can set your own layout for dialog. Refer my answer Android: Dialog dismisses without calling dismiss
If you want to customize an AlertDialog you need to create a Custom Dialog.