Android CustomDialog minWidth - android

I have a CustomDialog (extending Dialog) which is currently set to wrap_content for both width and heigh. Problem is if I set the main text as something really short (say "hello") the dialog gets very small in width. I would like the dialog to always take all the space available, but still have the default margin from the edges of the screen.
I already tried various methods:
setting:
android:layout_width="match_parent"
on the dialog layout, doesn't have any effect.
doing:
int width = (int)(getContext().getResources().getDisplayMetrics().widthPixels*0.90);
getWindow().setLayout(width, LinearLayout.LayoutParams.WRAP_CONTENT);
causes the dialog to go at the start of the screen and doesn't change anything regarding the width
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.horizontalMargin = 10;
params.width = LinearLayout.LayoutParams.MATCH_PARENT;
window.setAttributes(params);
makes the dialog take the whole screen but without any horizonthal margin
putting
<item name="windowMinWidthMajor">100%</item>
<item name="windowMinWidthMinor">65%</item>
in the dialog style has no effect.
how do I accomplish that? Mind that the dialog is used all around the app, so putting any code outside of the dialog class itself is not an option.

Related

Android 11 dialog in fullscreen doesn't work

When I set a dialog in full screen it isn't really fullscreen.
see here
How can I fix that?
Window window = getWindow();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
This code is in a custom dialog class that extends Dialog.
That's why I am able to write only getWindow().
Actually I don't need it to be fullscreen but I need atleast the pixel difference between the screen and the dialog box.
you can add the following theme to dialog's second parameter android.R.style.Theme_Black_NoTitleBar_Fullscreen
Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen)

Is there a SoftInputMode workaround to show whole dialog

I have a DialogFragment with two EditText fields and another field with an ImageView to increment its value underneath these, they all live in a ScrollView.
The problem is neither adjust mode for the soft keyboard shows my entire DialogFragment at once, despite there being space.
adjustResize causes the ScrollView to resize and hide the bottom row. adjustpan keeps the ScrollView size intact but the soft keyboard overlaps the bottom row.
Removing the ScrollView means either option causes the keyboard to overlap.
What I would like is for the DialogFragment to move up the screen without resizing. Can I make that happen? Ideally I'd like to keep the ScrollView in my Layout to better support very small screens.
The only solution I found was to change the window options for the dialog fragment itself. Obviously on a smaller screen this will still be an issue so I am still looking for a better answer.
#Override
#NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
//I was using android.R.style.Theme_Translucent_NoTitleBar for another issue
//but I don't think the theme makes any difference to how the window is laid out
//that is relevant to the below code
Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Translucent_NoTitleBar);
... //Do your usual stuff here
dialog.getWindow().setContentView(...);
final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.TOP; //this is the important part
dialog.setCanceledOnTouchOutside(false);
return dialog;
}

Align Android activity

Is there any possibility in Android to align a dialog Activity (activity with a dialog theme) at the bottom? By default, it is displayed in the middle of the screen. I haven't found any information on that... Thanks.
This is not exactly what you are doing, but I had a similar problem where I needed to display a dialog from an activity - at the bottom of the screen. The trick was to use WindowManager.LayoutParams. Here's what my onPrepareDialog looks like:
public void onPrepareDialog(int dialog, Dialog dlg) {
Window window = dlg.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
switch (dialog) {
case DIALOGUE_LOADING_PLEASE_WAIT:
wlp.gravity = Gravity.BOTTOM;
window.setAttributes(wlp);
break;
default:
break;
}
}
I didn't tried but searching on google found this...does this help you getWindow().setAttributes() , I hope this will help you.
content of the link (if it doesn't work) :-
You can call getWindow().getAttributes() to retrieve the
WindowManager.LayoutParams for the window. This has the following
fields involving window placement:
http://code.google.com/android/reference/android/view/ViewGroup.LayoutParams.html#width
http://code.google.com/android/reference/android/view/ViewGroup.LayoutParams.html#height
http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#gravity
http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#x
http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#y
After making your desired changes, use getWindow().setAttributes() to
install the new values.
Note that, though you can force a specific size through the width and
height fields, in general the correct way to do this is let the window
do its normal layout and determine the window size automatically that
way. If you have a single view in the dialog that wants a fixed size
such as 300x200, implement View.onMeasure() to call
setMeasuredDimension(300, 200). Then when the view hierarchy layout
happens, the dialog window will be given a size that ensures your view
is that dimension (probably making the actual window larger to take
into account the dialog frame and decoration).
http://code.google.com/android/reference/android/view/View.html#onMeasure(int,%20int)

Custom Alert dialog not centered vertically on Android

I made a custom alert dialog box to be displayed at the end of my game so that the player can enter its name to save it. The problem is when I call show() on the dialog appears but it's not vertically centered! It's a bit lower than it should and no matter what properties I set in the xml or when using setGravity().
I think this is the same problem as the one mentioned here, but no one gave a proper answer.
Thanks for your help.
For more details, here is my code:
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.newrecord,(ViewGroup)findViewById(R.layout.shoot));
builder = new AlertDialog.Builder(this);
builder.setView(layout);
newRecDialog = builder.create();
And here is the code of the first element of the XML layout of newrecord.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:padding="10dp"
android:baselineAligned="true">
Here is the output screenshot:
(source: free.fr)
The bug is described here. The AlertDialog is reserving space for the title/icon panel even where there is neither a title nor an icon.
The fix is, I think, quite simple: it should set the top panel layout to GONE, just as it does for the button panel in the event of there being no buttons. Until that's done, the only workaround is to implement your own Dialog subclass.
If you implement your own dialog the line requestWindowFeature(Window.FEATURE_NO_TITLE) hides the title panel and the dialog is centered on the screen. Maybe it works with a AlertDialog too.
You can use an Activity instead of a custom alert for this. You have to set the theme of activity as dialog in the android manifest file:
android:theme="#android:style/Theme.Dialog"
And you can adjust the activity xml layout as per your need.
android:theme="#android:style/Theme.Dialog"
If you are dealing with any height and width attributes, you must make sure not to alter the height, since it will alter the position, here is a sample.
myProgressDialog.show();
float widthPecent = 0.60f;
//order matters
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int displayWidth = displayMetrics.widthPixels;
//dont do any adjustments to the height. ************************** <<<<<<<<<<
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(myProgressDialog.getWindow().getAttributes());
int dialogWindowWidth = (int) (displayWidth * widthPecent);
layoutParams.width = dialogWindowWidth;
//dont do any changes to the height. ************************** <<<<<<<<<<
myProgressDialog.getWindow().setAttributes(layoutParams);
layoutParams.height = dialogWindowHeight; //comment or remove this line.
set the attribute on your AlertDialog as android:gravity="center" or programmatically as setGravity(Gravity.CENTER). This Gravity is for your layout only not for the display of your mobile. if you use Custom Title its did not look like center vertical.
Not really an answer, but I was having a similar issue. It appears that it is centered, but it is assuming the AlerterDialog has a title set. In my case, I just set a title.
Can you try using AlertDialog.Builder.setCustomTitle(View); instead of setView? We use it because alert dialog looks a bit better than dialog with empty title.
you should use the following line in the xml
firstly remove that padding line from your xml and after that
android:layout_gravity="center"
after this your dialog will appear in center and if you are using the margin from left etc than remove that and also if you are using that
android:layout_width="fill_parent"
than change it with the
android:layout_width="wrap_content"
after that your dialog will be appear in the center.
Try:
newRecDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
You need to disable the window title.
First create a custom style for your dialog (Make sure to use the parent theme that fits your needs):
<style name="CustomDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
Then apply your style to the dialog builder:
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.newrecord,(ViewGroup)findViewById(R.layout.shoot));
builder = new AlertDialog.Builder( new ContextThemeWrapper(this, R.style.CustomDialog));
builder.setView(layout);
newRecDialog = builder.create();

Maximizing an AlertDialog?

I'd like to show a Dialog that occupies as much screen space as possible.
So, here's a sample:
AlertDialog dialog = new AlertDialog.Builder(ctx)......create();
Window w = dialog.getWindow();
WindowManager.LayoutParams lp = w.getAttributes();
lp.width = 320;
lp.height = 480;
w.setAttributes(lp);
Problem is, this doesn't change a thing. Why?
TIA.
I don't know how to do exactly what you're wanting, but isn't this mostly defeating the purpose of a Dialog? Why not just create a custom Activity and use the Dialog theme if you're just wanting it to look dialog-ey.
You need to change the window layout parameters AFTER setContentView. Anything you do to the window before then (size/location-wise) will be tossed out when you call setContentView.
http://devstream.stefanklumpp.com/2010/07/android-display-dialogs-in-fullscreen.html
You need to add minWidth and minHeight attributes in the Dialog's xml layout file. E.g. your screen size is 800 * 480, then set android:minWidth="800dip", android:minHeight="400dip"

Categories

Resources