Is there any way to make my Dialog view full screen, i.e dialog occupy the entire screen (like an Activity). I tried using the LayoutParams and styles like
Try wrapping your dialog_custom_layout.xml
into RelativeLayout
. That worked for me.
Here are steps how to create a full screen dialog with custom layout which occupies the entire screen without any padding from each site.
Step 1: Define your custom dialog layout named layout_fullscreen_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="match_parent"
android:background="@color/colorPrimary">
<!-- Your dialog content here -->
</LinearLayout>
Step 2: Define a new style in styles.xml
named FullScreenDialog
<style name="FullScreenDialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
</style>
Step 3: Write a method which creates and shows a dialog
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createAndShowDialog(this);
}
// This method used to create and show a full screen dialog with custom layout.
public void createAndShowDialog(Context context) {
Dialog dialog = new Dialog(context, R.style.FullScreenDialog);
dialog.setContentView(R.layout.layout_fullscreen_dialog);
WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
dialog.getWindow().setAttributes(layoutParams);
dialog.show();
}
}
I'm using a Activity with Dialog theme. In this case, fullscreen worked this way:
int mUIFlag = View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getDecorView().setSystemUiVisibility(mUIFlag);
getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
setContentView(R.layout.lockscreen_activity);
}
The answer does not work for me(sdk 21, samsung galaxy s5). After searching and testing, I find that the key point for setting a dialog for fullscreen is the <item name="android:windowIsFloating">false</item>
in your dialog style. Most styles of Dialog in sdk set it true! Set it false in a style and use it in
AlertDialog.Builder = new AlertDialog.Builder(getActivity(), R.style.YourStyle);
your style may look like
<style name="YourStyle">
<item name="android:windowIsFloating">false</item>
...
</style>
after setting it false, it should be fullscreen. And more, you can use
dialog.getWindow.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
to set it's height to wrap_content. Hope to help someone.
try this code
protected void openDialog() {
Dialog dialog = new Dialog(this);
dialog.addContentView(new View(this), (new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)));
dialog.show();
}