I am trying to create a custom DialogFragment, that extends over the whole width of my screen (or rather, parent fragment). Although I can make the borders of the DialogFragment transparent, there still is a padding on the right and left that I cannot get rid of.
This is my Fragment:
public static class LoaderDialog extends DialogFragment {
static LoaderDialog newInstance() {
LoaderDialog f = new LoaderDialog();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.loader_f, container, false);
WindowManager.LayoutParams p = getDialog().getWindow().getAttributes();
p.y = getSupportActionBar().getHeight();
getDialog().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
getDialog().getWindow().setGravity(Gravity.TOP);
getDialog().getWindow().setAttributes(p);
return view;
}
}
This is a picture, how it looks like:
As you can see, the DialogFragment (the red thing) has some margins on the side. I want those to be gone. Any idea how to do this (in java, if possible)?
You can use:
WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
wmlp.gravity = Gravity.FILL_HORIZONTAL;
Full example:
public class TextEditor extends DialogFragment {
public TextEditor () {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_text_editor, container);
WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
wmlp.gravity = Gravity.FILL_HORIZONTAL;
return view;
}
}
try this:
p.horizontalMargin = 0;
use this style for DialogFragment
<item name="android:windowNoTitle">true</item>
<item name="android:padding">0dp</item>
or use this code in onCreateView method of DialogFragment
Display display = getActivity().getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, **220**, getResources().getDisplayMetrics());
getDialog().getWindow().setLayout(width,px);
ps. 220 is DialogFragment height, change it as u wish
Create a style element in your style.xml file. Copy the code below to your style.xml file
<style name="CustomDialog" parent="#android:style/Theme.Holo.Light" >
<item name="android:windowBackground">#null</item>
<item name="android:windowIsFloating">true</item>
Then in the createDialog method of your DialogFragment class,
dialog = new Dialog(getActivity(), R.style.CustomDialog);
This is working for me and hope this will help you too
Try to use LayoutParams.MATCH_PARENT instead. Fill_parent is drepecated. Moreover if you have set a padding for your view that is normal that is not fill its parent's view.
Related
I am unable to set the View Size matching the Display Size, below is the code, it is a Transparent View
TestView testView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
testView = new TestView(this);
SetContentView(testView);
}
The Default Height of the View when the app runs is height=1206
but the Display Height is 1280, i have used the following methods to set the View Size match the Screen Size, but none works
Does not work
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
testView = new TestView(this);
IWindowManager window = null;
window = this.Application.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
Display defaultDisplay = window.DefaultDisplay;
DisplayMetrics display = new DisplayMetrics();
defaultDisplay.GetRealMetrics(display);
int width = display.WidthPixels;
int height = display.HeightPixels;
RelativeLayout.LayoutParams parms = new
RelativeLayout.LayoutParams(width, height);
testView.LayoutParameters = parms;
SetContentView(testView);
}
Result:
Doesn not work:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
testView = new TestView(this);
SetContentView(testView);
if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
{
Window.SetFlags(WindowManagerFlags.LayoutNoLimits,
WindowManagerFlags.LayoutNoLimits);
}
}
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
Result:
How to set the Display height to the View?
You need to declare it in your manifest file that your activity should be of full screen:
<activity android:name=".ActivityName"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"/>
And if you want to get it through code just use these lines in your activity's onCreate method:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Or Add the below line to the style.xml file
//your theme
<style name="Theme.AppCompat.Transparent.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
//XML lines for full screen
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
For more help take a look at this answer:
Fullscreen Activity in Android?
It seems BottomSheetDialogFragment is coded with an anchor, where if your fragment layout exceeds 360dp in height onShow() will cause the dialog to peek to 360dp and you have to manually drag the sheet up to show all of your layout.
any way to bypass this behavior or any other recommendations for a modal bottom dialog where I can use a fragment?
you may check the behavior as follows
Activity.java
MyDialog myDialog = new MyDialog();
mtDialog.show(getChildFragmentManager(),"my_dialog_fragment");
MyDialog.java
public class MyDialog extends BottomSheetDialogFragment{
public View onCreate(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState){
return inflater.inflate(R.layout.dialog, container, false);
}
}
dialog.xml
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="432dp"
android:backgroud="#color/blue"/>
that's pretty much the code. I've tried using setStyle and creating a BottomSheetDialog style and all those permutations and nope. but setting the height to 360dp is where it expands fully, but I need some more area.
OP here answering the Q.
public class MyDialogFragment extends BottomSheetDialogFragment {
#Override
public void setupDialog(Dialog dialog, int style) {
View v = LayoutInflater.from(getActivity()).inflate(R.layout.dialog, null);
dialog.setContentView(v);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) v.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
((BottomSheetBehavior) behavior).setState(BottomSheetBehavior.STATE_EXPANDED);
}
The above fixed the issue of not expanding to the full height declared in the layout. Might want to add a check on the behavior to make sure its not null. Now just need to add my arithmetic, not sure if it needs to be on oncreateview or here in setup dialog... we shall see.
Happy coding :)
I want to create a Dialog with custom layout. I want it's width to be same as the phone screen's width and height as WRAP_CONTENT.
Here is what I tried:
Dialog dialog = new Dialog(context, R.style.DialogSlideAnim);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_share);
dialog.setCanceledOnTouchOutside(true);
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(dialog.getWindow().getAttributes());
layoutParams.width = width;
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
layoutParams.gravity = Gravity.BOTTOM;
dialog.getWindow().setAttributes(layoutParams);
The issue is that the dialog takes up only about 90% of the screen width, there is some margin on the left and right side of the dialog. How can I make it completely fill the width of the phone?
The accepted solution is simple and working, but you can also try the below solution to achieve the requirement too.
Step 1: Create Subclass of Dialog class as you want to create custom dialog.
public class ARProgressDialog extends Dialog
{
Activity context;
public ARProgressDialog(Context context,int id) {
// TODO Auto-generated constructor stub
super(context,id);
this.context=(Activity) context;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.progress_dialog); // Your custom layout
// BELOW CODE IS USED TO FIND OUT WIDTH OF ANY DEVICE
DisplayMetrics displaymetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
// BELOW CODE IS USED TO SET WIDHT OF DIALOG
LinearLayout layout = (LinearLayout)findViewById(R.id.dialogLinearLayout); // this is the id of your parent layout defined in progress_dialog.xml
LayoutParams params = layout.getLayoutParams();
params.width = width;
layout.setLayoutParams(params);
...// add your remaining code
}
}
Step 2: Show dialog.
ARProgressDialog dialog=new ARProgressDialog(this,R.style.MyTheme);
dialog.show();
Step 3: Code of MyTheme.xml
<style name="MyTheme" parent="android:Theme.Holo.Dialog">
<item name="android:windowBackground">#00000000</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
This may be helpful for you:
dialog.setContentView(R.layout.my_custom_dialog);
dialog.getWindow().setBackgroundDrawable(null);
or you can try with style class like this:
<style name="Theme_Dialog" parent="android:Theme.Holo.Dialog">
...
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>
</style>
This question already has answers here:
Position of DialogFragment in Android
(8 answers)
Closed 9 years ago.
I'm trying to locate my dialog in a specific location on screen.
Here is my dialog implementation :
public class DayDialog extends android.support.v4.app.DialogFragment {
public static DayDialog newInstance() {
DayDialog f = new DayDialog();
f.setCancelable(true);
f.setStyle(STYLE_NO_TITLE, 0);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.dialog_calendar_day, container);
WindowManager.LayoutParams params = getDialog().getWindow()
.getAttributes();
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;//b.getInt("x");
params.y = 0;//b.getInt("y");
getDialog().getWindow().setAttributes(params);
return layout;
}
here is how my dialog shown! how can I locate it (0,0) ? it's like there is a frame or something around it!
screenshot
There is fact a frame around it, that is defined in styles.xml, more specific it's the this 9-patch. The way i see it you have two options:
Start playing around with negative top/left margin params until you align it exactly where you want it (watch out to use dpi values).
Apply the DialogFragment.STYLE_NO_FRAME like so:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_FRAME, 0);
}
I have been trying many commands to setup the size of my DialogFragment. It only contains a color-picker, so I have removed the background and title of the dialog:
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
However I also want to position the dialog where I want and it is problematic. I use:
WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = LayoutParams.WRAP_CONTENT;
params.height = LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.LEFT;
getDialog().getWindow().setAttributes(params);
But one (big) obstacle remains: even though my dialog pane is invisible, it still has a certain size, and it limits the positions of my dialog. The LayoutParams.WRAP_CONTENT are here to limit the size of this pane to my color-picker, but for some reason it does not work.
Has anyone been able to do something similar?
i met a similar question that is you can't set the dialogFragment's width an height in code,after several try ,i found a solution;
here is steps to custom DialogFragment:
1.inflate custom view from xml on method
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().setCanceledOnTouchOutside(true);
View view = inflater.inflate(R.layout.XXX,
container, false);
//TODO:findViewById, etc
return view;
}
2.set your dialog's width an height in onResume(),remrember in onResume()/onStart(),seems didn't work in other method
public void onResume()
{
super.onResume();
Window window = getDialog().getWindow();
window.setLayout(width, height);
window.setGravity(Gravity.CENTER);
//TODO:
}
After some trial and error, I have found the solution.
here is the implementation of my DialogFragment class :
public class ColorDialogFragment extends SherlockDialogFragment {
public ColorDialogFragment() {
//You need to provide a default constructor
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_color_picker, container);
// R.layout.dialog_color_picker is the custom layout of my dialog
WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
wmlp.gravity = Gravity.LEFT;
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.colorPickerStyle);
// this setStyle is VERY important.
// STYLE_NO_FRAME means that I will provide my own layout and style for the whole dialog
// so for example the size of the default dialog will not get in my way
// the style extends the default one. see bellow.
}
}
R.style.colorPickerStyle corresponds to :
<style name="colorPickerStyle" parent="Theme.Sherlock.Light.Dialog">
<item name="android:backgroundDimEnabled">false</item>
<item name="android:cacheColorHint">#android:color/transparent</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
I simply extend a default Dialog style with my needs.
Finally, you can invoke this dialog with :
private void showDialog() {
ColorDialogFragment newFragment = new ColorDialogFragment();
newFragment.show(getSupportFragmentManager(), "colorPicker");
}
For my use case, I wanted the DialogFragment to match the size of a list of items. The fragment view is a RecyclerView in a layout called fragment_sound_picker. I added a wrapper RelativeLayout around the RecyclerView.
I had already set the individual list item view's height with R.attr.listItemPreferredHeight, in a layout called item_sound_choice.
The DialogFragment obtains a LayoutParams instance from the inflated View's RecyclerView, tweaks the LayoutParams height to a multiple of the list length, and applies the modified LayoutParams to the inflated parent View.
The result is that the DialogFragment perfectly wraps the short list of choices. It includes the window title and Cancel/OK buttons.
Here's the setup in the DialogFragment:
// SoundPicker.java
// extends DialogFragment
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getActivity().getString(R.string.txt_sound_picker_dialog_title));
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View view = layoutInflater.inflate(R.layout.fragment_sound_picker, null);
RecyclerView rv = (RecyclerView) view.findViewById(R.id.rv_sound_list);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
SoundPickerAdapter soundPickerAdapter = new SoundPickerAdapter(getActivity().getApplicationContext(), this, selectedSound);
List<SoundItem> items = getArguments().getParcelableArrayList(SOUND_ITEMS);
soundPickerAdapter.setSoundItems(items);
soundPickerAdapter.setRecyclerView(rv);
rv.setAdapter(soundPickerAdapter);
// Here's the LayoutParams setup
ViewGroup.LayoutParams layoutParams = rv.getLayoutParams();
layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
layoutParams.height = getListItemHeight() * (items.size() + 1);
view.setLayoutParams(layoutParams);
builder.setView(view);
builder.setCancelable(true);
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
// ...
});
builder.setPositiveButton(R.string.txt_ok, new DialogInterface.OnClickListener() {
// ...
});
return builder.create();
}
#Override
public void onResume() {
Window window = getDialog().getWindow();
window.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
super.onResume();
}
private int getListItemHeight() {
TypedValue typedValue = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.listPreferredItemHeight, typedValue, true);
DisplayMetrics metrics = new android.util.DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
return (int) typedValue.getDimension(metrics);
}
Here is fragment_sound_picker:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_sound_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
use this code for resize of Dialog Fragment android
public void onResume() {
super.onResume();
Window window = getDialog().getWindow();
window.setLayout(250, 100);
window.setGravity(Gravity.RIGHT);
}