I'm using alert box, that doesn't fit to my device screen. Especially width, i can increase the height by adding empty view, but i couldn't increase width. It always have some empty space on left and right side.
I don't know exactly whether it is android's default property or re-sizable. I've also tried custom alert box but it doesn't work for me. This alert box will update progress and show until progress complete.
This my code:
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.sync_dialog, null);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(activity);
alertBuilder.setView(view);
alertBuilder.setCancelable(false);
AlertDialog progress = alertBuilder.create();
// Dialogs set their Window's top level layout width and height to WRAP_CONTENT.
// To make the Dialog bigger, you can set those parameters to FILL_PARENT.
progress.setCanceledOnTouchOutside(false);
progress.setCancelable(false);
progress.getWindow().getAttributes().width = LayoutParams.MATCH_PARENT;
progress.getWindow().getAttributes().height = LayoutParams.MATCH_PARENT;
mProgress = (ProgressBar) view.findViewById(R.id.progress);
/*Dialog dialog = new Dialog(activity, android.R.style.Theme_Translucent_NoTitleBar);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.sync_dialog);
dialog.setCanceledOnTouchOutside(false);
mProgress = (ProgressBar) view.findViewById(R.id.progress);*/
mText = (TextView) view.findViewById(R.id.message);
//Rebuilds ProgressBar with saved state when closed
if (!firstTime) {
mText.setText(mCurrentStatus);
mProgress.setProgress(mProgressStatus);
}
mProgress.setMax(mProgressMax);
return progress;
Layout File is:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/body"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical"
android:paddingBottom="10dip"
android:paddingLeft="8dip"
android:paddingRight="8dip"
android:paddingTop="10dip" >
<ImageView
android:id = "#+id/btn_mfa_home"
android:src="#drawable/ic_mfa_icon_home"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
<ImageView
android:id = "#+id/btn_sync_home"
android:src="#drawable/btn_sync_home"
android:paddingTop="50dp"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="12dip"
android:text="#string/dialog_initial_syncing"
android:textColor="#android:color/black" />
<ProgressBar
android:id="#+id/progress"
style="#android:style/Widget.ProgressBar.Horizontal"
android:layout_gravity="center_horizontal"
android:layout_width="240dip"
android:layout_height="wrap_content"
android:max="100" />
</LinearLayout>
Any suggestions would be appreciated!
you are not setting the attrbutes on the dialog
something like this might help
AlertDialog.Builder adb = new AlertDialog.Builder(this);
Dialog d = adb.setView(new View(this)).create();
// (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.)
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);
Note that the attributes are set after the Dialog is shown. The system is finicky about when they are set.
try using LayoutParams
mDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
I help, alone use it after yourAlertDialog.show()
alertDialog.show();
alertDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
Related
I have a custom dialog box (cameraTipDialog ctd) that I need to position at a certain (x,y) position. That position is based on the location of button on the screen. I need to put the dialog box at position:
(int) ((ImageButton) findViewById(R.id.infoButton)).getY() - HEIGHT OF MY DIALOG BOX;'
But I Cannot figure out how to get the height of my custom dialog box. It's height is 'wrap content' so when I do getLayoutParams.getHeight() I get -1.
Following other ideas on StackOverflow I tried using (ctd.findViewById(R.id.dialog_root_layout)).getHeight() but that crashes the app saying that it couldn't find item with id dialog_root_layout.
Show Layout Code:
cameraTipDialog ctd = new cameraTipDialog(submitHome.this);
Window window = ctd.getWindow();
(ctd.findViewById(R.id.dialog_root_layout)).getHeight()
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.TOP | Gravity.LEFT;
wlp.x = (int) ((ImageButton) findViewById(R.id.infoButton)).getX();
wlp.y = (int) ((ImageButton) findViewById(R.id.infoButton)).getY() - DIALOG HEIGHT;
window.setAttributes(wlp);
ctd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
ctd.show();
Dialog box XML code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/dialog_root_layout"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginLeft="16sp"
android:layout_marginRight="16dp"
android:background="#drawable/popup_background">
<TextView
android:id="#+id/content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="32dp"
android:text="#string/tipsForSuccess"
android:textColor="#android:color/white"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/header" />
<TextView
android:id="#+id/header"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="text here."
android:textColor="#android:color/white"
android:textSize="10sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
To get the actual height you can use getRootView().getMeasuredHeight() on your dialog_root_layout. But you can only do that once the view has been measured so you need to do your calculations in an OnShowListener. Modified code looks like this:
cameraTipDialog ctd = new cameraTipDialog(this);
ctd.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
Dialog ctd = (Dialog) dialog;
View dialogRootLayout = ctd.findViewById(R.id.dialog_root_layout);
int dialogHeight = dialogRootLayout.getRootView().getMeasuredHeight();
Window window = ctd.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.TOP | Gravity.LEFT;
wlp.x = (int) ((ImageButton) findViewById(R.id.infoButton)).getX();
wlp.y = (int) ((ImageButton) findViewById(R.id.infoButton)).getY() - dialogHeight;
window.setAttributes(wlp);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
});
ctd.show();
I want to remove title section from dialog box in Android and I want to use my own created xml file as Dialog box.
Inflate your XML. Like this
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
LayoutInflater inflater = context.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.yourlayout, null);
dialogBuilder.setView(dialogView);
AlertDialog b = dialogBuilder.create();
b.show();
You can inflate your own layout and set it to you Dialog.
Dialog dialog = new Dialog(ProfileSettingsActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.uploadphoto); // Your layout here
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT; //Custom width and height
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(lp);
dialog.setCancelable(true);
dialog.show();
Simply add style to dialog and remove title of the custom dialog.
Custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:orientation="vertical">
<TextView
android:id="#+id/txt_warning"`enter code here`
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Warning"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:id="#+id/txt_dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Are you sure you want to Logout?"
android:textColor="#android:color/white"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="15dp"
android:background="#android:color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/btn_no"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight=".5"
android:background="#color/colorPrimary"
android:clickable="true"
android:gravity="center"
android:padding="10dp"
android:text="No"
android:textColor="#android:color/white"
android:textSize="18dp" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/white" />
<Button
android:id="#+id/btn_yes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="#color/colorPrimary"
android:clickable="true"
android:gravity="center"
android:padding="10dp"
android:text="Yes"
android:textColor="#android:color/white"
android:textSize="18dp" />
</LinearLayout>
</LinearLayout>
Set style in Styles.xml:-
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
On button Click listener you have to write this code:-
// custom dialog
final Dialog dialog = new Dialog(mContext, R.style.FullHeightDialog);
dialog.setContentView(R.layout.custom_dialog);
dialog.setCancelable(false);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(lp);
dialog.show();
Button btnYes = (Button) dialog.findViewById(R.id.btn_yes);
Button btnNo = (Button) dialog.findViewById(R.id.btn_no);
// if button is clicked, close the custom dialog
btnYes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
documentImage = documentImagesList.get(getAdapterPosition());
//logout code here
}
});
btnNo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
Here is code for same:
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
final ViewGroup viewGroup = null;
View dialogView = layoutInflater.inflate(R.layout.custom_email_dialog, viewGroup, false);
dialogBuilder.setView(dialogView);
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
Here is "custom_email_dialog" is custom XML that we need to show on dialog.
and you can access dialog element as:
TextView headerTextView = (TextView) dialogView.findViewById(R.id.emailHeader);
I am working on an Android Application , Here I have created a custom dialog box , The dialog box was appearing on complete height of the device , but I wanted to leave margins from top and bottom , so I set margin using below code
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = Utility.convertDPtoPixel(400, screen);
alertDialog.show();
alertDialog.getWindow().setAttributes(lp);
Above code worked perfectly, but the dialog appears with black surroundings.
Here is the screen shot:
Now I want to remove these black surroundings from the dialog box.
Here is the code of dialog box:
public void showDialog() {
AlertDialog.Builder builder;
final AlertDialog alertDialog;
//final Dialog alertDialog;
Context mContext;
mContext = screen;
LayoutInflater inflater = (LayoutInflater) LoginActivity.screen.getSystemService(LoginActivity.screen.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.countrylist, null);
LinearLayout rellayout = (LinearLayout) layout.findViewById(R.id.rellayout);
RelativeLayout closeAcessCodeDialog = (RelativeLayout)layout.findViewById(R.id.closeAcessCodeDialog) ;
ListView listview = (ListView) layout.findViewById(R.id.listview);
listview.setAdapter(new CustomAdapter1(LoginActivity.screen, countryList));
builder = new AlertDialog.Builder(LoginActivity.screen);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = Utility.convertDPtoPixel(400, screen);
alertDialog.show();
alertDialog.getWindow().setAttributes(lp);
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
countryName = idCountry.get(pos);
System.out.println("Country code is: " + countryName);
alertDialog.cancel();
text.setText(countryList.get(pos));
}
});
closeAcessCodeDialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}
countrylist.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#ffe264"
app:cardCornerRadius="10dp"
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rellayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_above="#+id/listview"
android:gravity="center_vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your Country"
android:textColor="#000"
android:textSize="18sp"
android:textStyle="bold"
android:layout_centerInParent="true"
/>
<RelativeLayout
android:id="#+id/closeAcessCodeDialog"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:gravity="center">
<ImageButton
android:id="#+id/closeDialog"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/close_country_dialog" />
</RelativeLayout>
</RelativeLayout>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:divider="#e5e5e5"
android:background="#drawable/list_bacground"
android:dividerHeight="1px" />
</LinearLayout>
I visited many similar links over stackoverflow , but no one was helpfull .
Hope this will help you a lot
I think its default theme color which is Black so you have to check the import of your AlertDialog it should be import android.support.v7.app.AlertDialog; and one more thing you should use custom color for Background like below
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Try this.
Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.yourlayout);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
I am stuck with a margin and padding issue of a title of AlertDialog.Builder, what i am doing is i want the title in center so i am using setCustomTitle i am able to do so but stuck with margin and padding of it. I don't want unwanted padding which is showing and also i want to set some top margin to title, i am using LinearLayout.LayoutParams but it has no effect. please suggest what to do to handle it.thanks
Code :
dialog = new AlertDialog.Builder(context, R.style.DialogTheme);
TextView title = new TextView(context);
title.setTextColor(ContextCompat.getColor(context, R.color.black));
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
title.setTypeface(Typeface.DEFAULT_BOLD);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 20, 0, 0);
title.setLayoutParams(lp);
title.setText("Dialog");
title.setGravity(Gravity.CENTER);
dialog.setCustomTitle(title);
dialog.setMessage("Dialog box with custom title view ");
dialog.setCancelable(false);
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
Result :
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
TextView title = new TextView(this);
title.setTextColor(ContextCompat.getColor(this, android.R.color.black));
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
title.setTypeface(Typeface.DEFAULT_BOLD);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 20, 0, 0);
title.setPadding(0,30,0,0);
title.setLayoutParams(lp);
title.setText("Dialog");
title.setGravity(Gravity.CENTER);
dialog.setCustomTitle(title);
dialog.setMessage("Dialog box with custom title view ");
dialog.setCancelable(false);
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
Replace this code it will work
You can avoid setting up all this methods to your AlertDialog by using DialogFragment. Just use getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); in onCreateView method and make your own custom view for the dialog in the way you create a common fragment. It's a better way to make proper dialogs.
I suggest you to use DialogFragment to show custom layout as alert Dialog its very easy and we can customize our dialog as per requirement..for more detail about it : https://developer.android.com/reference/android/app/DialogFragment.html
Below is the layout as per your requirement, You can use the below xml layout to show your custom layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/margin10dp"
android:layout_weight="1"
android:gravity="center"
android:text="My Dialog"
android:textColor="#color/white"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:padding="10dp">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="My Dialog Box Description !"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp" />
<Button
android:id="#+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView2"
android:background="#android:color/transparent"
android:text="OK"
android:textSize="22dp" />
</RelativeLayout>
</RelativeLayout>
I have a dialog/modal that I need to match_parent along its width. However, it keeps doing wrap_content. Any ideas how to rectify this?
Here is my layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#55555555" >
<Button
android:id="#+id/dogs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#AAAAAA"
android:clickable="true"
android:gravity="center"
android:onClick="onDogsClicked"
android:text="Dogs" />
<Button
android:id="#+id/cats"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#AAAAAA"
android:clickable="true"
android:gravity="center"
android:onClick="Cats"
android:text="Cat" />
<Button
android:id="#+id/snake"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#FF0000"
android:clickable="true"
android:gravity="center"
android:onClick="onSnakeClicked"
android:text="Snake" />
<Button
android:id="#+id/sheep"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#AAAAAA"
android:clickable="true"
android:gravity="center"
android:onClick="onSheepClicked"
android:text="Sheep" />
</LinearLayout>
</LinearLayout>
Here is the program fragment
Dialog animalsView;
public void onAnimalsClicked(View v) {
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.animals_layout,
(ViewGroup) findViewById(R.id.my_root));
animalsView = new Dialog(this, R.style.CustomDialog);
WindowManager.LayoutParams wmlp = animalsView.getWindow()
.getAttributes();
wmlp.gravity = Gravity.BOTTOM;
wmlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
animalsView.getWindow().setAttributes(wmlp);
animalsView.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
animalsView.setContentView(dialoglayout);
animalsView.getWindow().setBackgroundDrawable(new ColorDrawable(0));
animalsView.show();
}
I had the same problem and I couldn't find a solution for doing it in the XML. But there is a solution in the .java file.
WindowManager.LayoutParams params = animalsView.getWindow().getAttributes();
params.gravity = Gravity.BOTTOM;
params.height = 100;
params.width = android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Try this...
public void onAnimalsClicked(View v) {
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.animals_layout,
(ViewGroup) findViewById(R.id.my_root));
animalsView = new Dialog(this, R.style.CustomDialog);
........
animalsView.setContentView(dialoglayout);
animalsView.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
......
animalsView.show();
}
I think you should try this. I was working with an overlay when I faced this issue. Hope this helps you.
Dialog animalsView;
public void onAnimalsClicked(View v) {
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.animals_layout,
(ViewGroup) findViewById(R.id.my_root));
animalsView = new Dialog(this, R.style.CustomDialog);
WindowManager.LayoutParams wmlp = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSPARENT);
wmlp.gravity = Gravity.BOTTOM;
wmlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
animalsView.getWindow().setAttributes(wmlp);
animalsView.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
animalsView.setContentView(dialoglayout);
animalsView.getWindow().setBackgroundDrawable(new ColorDrawable(0));
animalsView.show();
}
Got similar issue and went through tons of StackOverflow posts concerning this issue.
I tried all possible solutions (changing layout configuration of the dialog window in all possible ways mostly), but none of them works.
However, when comparing the XML layout of a dialog for which the match_parent worked and the XML layout of dialog for which it does not, I finally found a weird way to make it work:
I have to set the layout_height of the dialog to match_parent as follows:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="#dimen/activity_horizontal_margin"
android:layout_width="match_parent"
android:layout_height="match_parent">
// ...
</LinearLayout>
My guess is that when it comes to Dialogs, Android tries to maintain the minimum possible ratio between height and width. When the layout_height is set to wrap_content, then this ratio is bounded by the height and the width is shrunk.
But by switching to match_parent for the layout_height, then the ratio is bigger and bounded that the width that now fills the entire screen.
Not sure if that's the correct explanation, but that's what I deduce from this observation.
Even though this is counter-intuitive, that may sound logical in that way...
Also, I tried on both landscape and portrait, both works after this change.
Finally, note that I have strictly nothing related to window configuration in my java code:
final Dialog dialogLayout = new Dialog(mContext);
dialogLayout.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogLayout.setCancelable(false);
View layout = mContext.getLayoutInflater().inflate(R.layout.popup_dialog_introduce_myself, null);
dialogLayout.setContentView(layout);
// ...
dialogLayout.show();