Reputation: 3692
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?
Upvotes: 1
Views: 876
Reputation: 2641
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>
Upvotes: 3
Reputation: 9225
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>
Upvotes: 2