Reputation: 141
I am displaying custom dialog in my xamarin android app.
it display but it display very small.
I want to set height and width for dialog dynamically according to the device size..
I found the solution but solution is for android..
here is solution from android
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog2.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialog2.getWindow().setAttributes(lp);
I want to set same functionality but i want to it in xamarin.
How could i do this in xamarin
Upvotes: 0
Views: 319
Reputation: 11
Theres a lot of content i don't know if you need it all but its there :)
On my side, I wanted to dynamically set the height of my content based on its size. If the content is smaller, I want to use "wrap content" to adjust the height accordingly. If the content is too large, I want to limit it to a maximum of 90%.
Dialog alert = new Dialog(this);
View view = LayoutInflater.Inflate(Resource.Layout.my_content, drawer, false);
alert.SetContentView(view);
//Get display pixel width
int displayWidth = (int)(Resources.DisplayMetrics.WidthPixels * 0.9);
int displayHeight = (int)(Resources.DisplayMetrics.HeightPixels * 0.9);
alert.Window.SetLayout(displayWidth, displayHeight);
this is the part to recalculate it
alert.Window.DecorView.Post(() =>
{
//put whatever you want in this
int content1Height = 100;
int content2Height = 200;
int content3Height = 300;
int totalViewHeight = content1Height + content2Height + content3Height;
int heightUsed = System.Math.Min(displayHeight, totalViewHeight);
alert.Window.SetLayout(displayWidth, heightUsed);
});
Upvotes: 0
Reputation: 9084
Usage in Xamarin like this :
WindowManagerLayoutParams lp = new WindowManagerLayoutParams();
lp.CopyFrom(dialog2.Window.Attributes);
lp.Width = WindowManagerLayoutParams.MatchParent;
lp.Height = WindowManagerLayoutParams.WrapContent;
lp.Gravity = GravityFlags.Center;
dialog2.Window.Attributes = lp;
Upvotes: 1