How programmatically make a custom dialog be 100% of screen height? - android

I have a relative layout that occupies 100% of the screen. I call many custom dialogs and always works well. I get positioning and correct height and width with no problems.
However, now I need a dialog that is 100% of screen height and 100% of screen width. I get 100% width but I don't get 100% height. The maximum is around 95 of height.
I've reduced my code to a minimum for better clarity:
var lay = RelativeLayout(this)
var dial = Dialog(cx)
val win = dial.window
val wlp = win.attributes
win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
wlp.apply {
gravity = Gravity.TOP or Gravity.LEFT
x = 0
y = 0
}
win.attributes = wlp
// Here are my widgets linked to lay with lay.addView(widget).
dial.setContentView( // Connection between layout and dialog
lay, ViewGroup.LayoutParams(
screen_width, // variable with screen width in pixels
screen_height )) // variable with screen heigth in pixels
I've read many questions in Stackoverflow. What I have tried:
a) I've used MATCH_PARENT as height.
b) I've tried some windows flags:
win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
win.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN)
c) I've tried windows attribute property:
wlp.verticalMargin = 0F
No success.
PS: I also have a strange shade border at right and bottom side of dialog that I cannot get rid of it.
Update:
Rahul Khurana solution has killed the problem.
It just use
val dial = Dialog(cx,android.R.style.Theme_Black_NoTitleBar_Fullscreen)
It also eliminates the shade in other no fullscreen dialogs
val dial = Dialog(cx,
android.R.style.Theme_DeviceDefault_Dialog_NoActionBar)

Change the constructor like this:
val dialog=Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);

Initialize and show your dialog as below in onStart
override fun onStart() {
super.onStart()
val dial = Dialog(this);
var width = ViewGroup.LayoutParams.MATCH_PARENT;
var height = ViewGroup.LayoutParams.MATCH_PARENT;
dial.getWindow().setLayout(width, height);
dial.show()
}

Use this snippet of code, it will work.
Activity activity = ...;
AlertDialog dialog = ...;
// retrieve display dimension
Rect displayRectangle = new Rect();
Window window = activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
// inflate layout
LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.your_layout, null);
layout.setMinimumWidth((int)(displayRectangle.width() * 0.95f));
layout.setMinimumHeight((int)(displayRectangle.height() * 0.95f));
dialog.setView(layout);

dialog?.window?.apply {
requestFeature(Window.FEATURE_NO_TITLE); setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT)
}

Related

Android: How to slide a PopupWindow between 2 points?

I want to slide a PopupWindow from the bottom of the screen to some point above it, say center.
I have the following logic so far
val inflater = getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
val popupView: View = inflater.inflate(R.layout.popup_window, null)
val width = LinearLayout.LayoutParams.WRAP_CONTENT
val height = LinearLayout.LayoutParams.WRAP_CONTENT
val focusable = true // lets taps outside the popup also dismiss it
val popupWindow = PopupWindow(popupView, width, height, focusable)
val slide = Slide()
slide.apply {
duration = 300
slideEdge = Gravity.BOTTOM
}
view?.doOnAttach {
popupWindow.isFocusable = true
popupWindow.enterTransition = slide
popupWindow.exitTransition = slide
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0)
}
I am able to slide the window between its width. So what this does is that the sliding happens from -w/2 where w is the width of the window and ends at w/2. Which maybe expected since the position of the window is set as 0, 0. However I want the sliding to happen between 2 specific points. Like the sliding should start from the bottom of the screen and it should continue to slide till it reaches the coordinate 0,0.
I found a workaround by expanding the width of the window to the portion below 0,0 but that cause the views below it to not be clickable. So this doesn't work in my case. Is there a way to do this sliding? Thanks!

Android: Set popup window width same as achor view width

I am using constraint layout in which I have arranged views using flow. These views are added dynamically.
On clicking any of view a popup window should open like shown in the illustration below:
I have managed to show the popup window but it is not taking complete width of anchor view. This is how it is looking right now:
This is the snippet from my code which shows the popup window
anchorView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
val width = anchorView.measuredWidth
val height = ViewGroup.LayoutParams.WRAP_CONTENT
val focusable = true
val popupWindow = PopupWindow(popupCardBinding.root, width, height, focusable)
popupWindow.showAsDropDown(anchorView)
How can I make it's width same as anchorView's width?
I'm not sure where you're calling measure for the anchor view, but try measuring after the view has been drawn - using UNSPECIFIED doesn't always return the correct values, if you use measure too early:
anchorView.post {
anchorView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
val width = anchorView.measuredWidth
val height = ViewGroup.LayoutParams.WRAP_CONTENT
val focusable = true
val popupWindow = PopupWindow(popupCardBinding.root, width, height, focusable)
popupWindow.showAsDropDown(anchorView)
}
If it doesn't work, try showing more code about how and when anchorView is created
I had the exact same problem and couldn't find a good answer after searching for a while.
I ended up passing the Anchor View to the PopupWindow's constructor.
public TestPopupWindow(Context context, View anchorView) {
super(context);
this.context = context;
this.anchorView = anchorView;
createView();
}
And setting the width during the view inflation.
private void createView() {
View view = View.inflate(context, R.layout.popup_window_layout, null);
this.setWidth(anchorView.getWidth());
setContentView(view);
}
Simple and works for me

Why can’t I resize and position my dynamic dialog (with no XML associated)?

I have studied many questions and answers in Stackoverflow, then I made a dummy function, that I run after a click in my app.
Here is the code:
fun openDialog(cx: Context) {
val alertDialog = Dialog(cx)
var linLayout = LinearLayout(cx)
linLayout.setOrientation(LinearLayout.VERTICAL);
// Set width, height and weight
linLayout.layoutParams = LinearLayout.LayoutParams(500,1000,1F)
// top and left position
linLayout.x = 0F
linLayout.y = 0F
val title = TextView(cx); // dummy view 1
title.setText("Custom Dialog 1")
title.setTextColor(Color.BLACK)
title.setTextSize(20F)
linLayout.addView(title) // add in layout
val msg = TextView(cx) // dummy view 2
msg.setText("Custom Dialog Box 2")
msg.setTextColor(Color.RED)
msg.setTextSize(10F)
linLayout.addView(msg) // add in layout
alertDialog.setContentView(linLayout) // Add the layout in Dialog
alertDialog.show(); // Show the dialog with layout
}
What I get? A proper dialog, but in the middle of the screen with width and height defined by the content. I also try to use the windows linked to the custom dialog without success.
val wlp = win.attributes
wlp.apply {
x = 0
y = 0
height = 1000
width = 500
}
win.attributes = wlp
No changes. However wlp.gravity = Gravity.BOTTOM works, but it's not enough for me. A also try to use win.setLayout(1000,500) without success
The Android documentation states:
Set the width and height layout parameters of the window. The default
for both of these is MATCH_PARENT; you can change them to WRAP_CONTENT
or an absolute value to make a window that is not full-screen.
Why can’t I resize and position the layout that I assign to my dialog?
Has somebody a hint?
The cell phone screen:
Update
I've got to change the position of dialog using
wlp.gravity = Gravity.TOP or Gravity.LEFT
wlp.x = 100 // Relative to left
wlp.y = 200 // relative to top
I keep trying to figure out how to change the width and height.
Set the layout params for the view you're inflating when you set the content :
this line :
alertDialog.setContentView(linLayout)
should be :
alertDialog.setContentView(linLayout, LinearLayout.LayoutParams(500,1000,1F))
You can also remove the explicit setting of the params for the LinearLayout

Why dialog position shows a small displacement?

From the click on a button vi, I passed the same coordinates (vi.x and vi.y) for a popup dialog criation (dialog dial), associated a layout lay, and under it has several buttons.
When I've positioned the popup, with the same x, y of the button, it does it correctly, but with a slight right and down shift, that I can't find any explanation.
PS: I positioned exactly the same place as the button, just to make the below image clear.
Here the dummy code. (It's working in a real app, but I've reduced for clarity):
fun openDialog(xPos: Float, yPos:Float) {
...
val dial = Dialog(this) // this is the context
val win = dial.window
val wlp = win.attributes
wlp.apply {
gravity = Gravity.TOP or Gravity.LEFT
x = xPos
y = yPos
}
win.attributes = wlp
var lay = RelativeLayout(cx)
.....
dial.setContentView(
lay, ViewGroup.LayoutParams(150,500))
dial.show()
}
I've tried many things: horizontalMargin and verticalMargin in window attributes (dial.window.attributes), padding, x, y and gravity in layout lay
Nothing works!
PS 1: My main layout is full screen. Even the battery status is hidden. So the screen has the exact size of screen size.
PS 2: All my views and widgets are programatically created without XML.
Here is the scheme:

How to add margins to Left and Right to Custom AlertDialog?

I have created a Custom AlertDialog like this -
remark_builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the
// dialog layout
remark_builder.setCancelable(true);
final View dialogView = inflater.inflate(R.layout.add_remark_dialog, null);
remark_builder.setView(dialogView);
if (remark_dialog == null || !remark_dialog.isShowing()) {
remark_dialog = remark_builder.create();
remark_dialog.setCanceledOnTouchOutside(false);
remark_dialog.show();
}
I want to add margins to the left and right of this alertdialog.
I have tried a lot of code over the internet but it is emphasizing on setting the width and height of the Alert Dialog.
I don't want to set the width and height. I want to add margins to the left and right of the AlertDialog which is MATCH_PARENT.
My current alert dialog looks like :
I want to add margins like 40 or 50 or according to the device density. is this possible?
You can try the below method to achieve this.
Rect displayRectangle = new Rect();
Window window = mContext.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
remark_dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
malertdialog.show();
malertdialog.getWindow().setLayout((int)(displayRectangle.width() *
0.8f), (int)(displayRectangle.height() / 2));
adjust the decimal value for the width and height as per what ratio you need.

Categories

Resources