How to move a DialogFragment out of the center? - android

Is it possible by using a DialogFragment to be able to move it out of the center and place it anywhere on the screen?

You can use
getDialog().getWindow().setAttributes(param);
in
onCreateView()
like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
getDialog().getWindow().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
WindowManager.LayoutParams param = getDialog().getWindow().getAttributes();
param.width = LayoutParams.MATCH_PARENT;
param.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;
param.x = 100;
param.y = 100;
.
.
getDialog().getWindow().setAttributes(p);
.
.
}

I have success using
Window window = dialog.getWindow();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.gravity = Gravity.TOP | Gravity.RIGHT;
lp.x = 100;
lp.y = 100;
window.setAttributes(lp);
puts my dialog in the Top Right slightly down from the corner. this code is in onCreateDialog().

I suffered a lot trying all the programmatically solutions with no results. Finally I've done it from the XML file without any extra code within the Java class.
All I've done is Make the parent height match_parent and set a gravity for it with value center
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
<!--The following two lines-->
android:layout_height="match_parent"
android:gravity=“center"
android:orientation="vertical">
<Button
android:id="#+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:background="#android:color/white"
android:gravity="center"
android:padding="10dp"
android:text="#string/hide"
android:textColor="#android:color/holo_blue_dark" />
<Button
android:id="#+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#android:color/white"
android:gravity="center"
android:padding="10dp"
android:text="#string/cancel"
android:textColor="#android:color/holo_blue_dark" />

Related

Finding the height of a custom dialogBox

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();

how to add close button on the top-center of BottomSheetDialog android?

want to add close button on BottomSheetDialog to close it.
I'm using with custom view(Linearlayout) for BottomSheetDialog.
filterDialog = new BottomSheetDialog(context);
filterDialog.setContentView(rootView);
filterDialog.getWindow().setWindowAnimations(R.style.CustomDialogAnimation);
filterDialog.show();
below image is my filterDialog:
And this is what I want to achieve:
Any help would be greatly appreciated.
I have solved this by using Dialog(full screen) with custom(bottom) animations.
Design:
<LinearLayout 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="match_parent"
android:background="#color/colorTransparent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<ImageView
android:id="#+id/img_close"
android:layout_width="#dimen/fifty_dp"
android:layout_height="#dimen/thirty_dp"
android:src="#drawable/ic_close_filter"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<!-- All your required widgets here-->
</LinearLayout> </LinearLayout>
Java Code:
Dialog filterDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.dialog_filter, null);
final ImageView close = (ImageView) rootView.findViewById(R.id.img_close);
builder.setView(rootView);
filterDialog = builder.create();
WindowManager.LayoutParams lp2 = new WindowManager.LayoutParams();
Window window = filterDialog.getWindow();
filterDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
lp2.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp2.width = ViewGroup.LayoutParams.MATCH_PARENT;
lp2.height = ViewGroup.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp2);
Window dialogWindow = filterDialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
dialogWindow.setGravity(Gravity.BOTTOM );
filterDialog.getWindow().setWindowAnimations(R.style.CustomDialogAnimation);
filterDialog.show();
You can do it as -
<!--you bottom sheet linear layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--Layout contains button-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#android:color/transparent"
android:clickable="false"
>
<!--Your button-->
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="your drawable"
android:background="#android:color/transparent"
android:clickable="true"/>
</LinearLayout>
<!--put Your other views in below linear layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
Note : clickable false is very important point. Doing this you can access view behind your linear layout. Also set background color to transparent

Make dialog smaller on screen

I want my dialog to not fit the whole screen like the image shown. How should i make it so it is not as long (height).
public class SidemenuInfoDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.sidemenu_info_main, container, true);
return layout;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(new ContextThemeWrapper(getActivity(), R.style.Theme_Window_NoMinWith));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
}
The View:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/sidemenu_info_main_relative">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/text1"
android:text="TEST TEXT "/>
</RelativeLayout>
Let me know if there is something else needed to be able to help me get on the right path of fixing this.
Give your textview inside your dialog view height and width as "wrap_content".Like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/sidemenu_info_main_relative">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text1"
android:text="TEST TEXT "/>
</RelativeLayout>
Try changing you xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/sidemenu_info_main_relative">
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:id="#+id/text1"
android:text="TEST TEXT "/>
</RelativeLayout>
If that does not work with you here is another approach that I usually do :
public class PopUp {
public void poupUP(final Activity activity) {
final Dialog dialog = new Dialog(activity, R.style.dialogwithoutTitle);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//here add the layout
dialog.setContentView(R.layout.pop_up);
// The comments are to change the position of the alert dialogu
// WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
//wmlp.gravity = Gravity.TOP | Gravity.END;
//wmlp.x = 100; //x position
//wmlp.y = 100; //y position
dialog.show();
}
}
Then wherever you want it to show do this :
PopUp p = new PopUp();
p.popUP();
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.sidemenu_info_main, container, false);
Try this:
alert.getWindow().setLayout(dialogWidth, LayoutParams.WRAP_CONTENT);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(alert.getWindow().getAttributes());
layoutParams.x = xPosition;
layoutParams.y = yPosition;
alert.getWindow().setAttributes(layoutParams);

Alert Box doesn't fit with screen in android

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);

modal/Dialog not applying match parent layout on width

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();

Categories

Resources