Giới thiệu về Full-screen Dialog
Full-screen Custom Dialog hiển thị dialog chiếm toàn bộ màn hình, phù hợp cho: onboarding, terms/conditions, image viewer, form phức tạp, bottom sheet style.
Cách tiếp cận: Tính toán kích thước màn hình, set minimum width/height cho dialog view.
Ví dụ minh họa
Bước 1: Tạo project
Android Studio → File → New Project.
Bước 2: Layout chính (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/parent"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/customDialog"
android:text="Custom Dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Bước 3: Layout Custom Dialog (res/layout/customview.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Success"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Headline" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla eu erat tincidunt lacus fermentum rutrum."
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<Button
android:id="@+id/buttonOk"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:background="@color/colorPrimary"
android:text="Ok"
android:textColor="#FFF" />
</LinearLayout>
</LinearLayout>
Bước 4: Theme tùy chỉnh (res/values/styles.xml)
<style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowBackground">@drawable/popup_background</item>
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>
<item name="android:windowIsFloating">false</item>
</style>
Bước 5: Drawable background (res/drawable/popup_background.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:radius="0dp" /> <!-- Full screen không bo góc -->
</shape>
Bước 6: Code (src/MainActivity.java)
package com.example.andy.myapplication;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.customDialog).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 1. Lấy kích thước màn hình hiển thị
Rect displayRectangle = new Rect();
Window window = MainActivity.this.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
// 2. Tạo builder với custom theme
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this, R.style.CustomAlertDialog);
// 3. Inflate custom view
ViewGroup viewGroup = findViewById(android.R.id.content);
View dialogView = LayoutInflater.from(v.getContext())
.inflate(R.layout.customview, viewGroup, false);
// 4. QUAN TRỌNG: Set minimum width/height = màn hình
dialogView.setMinimumWidth((int)(displayRectangle.width() * 1f));
dialogView.setMinimumHeight((int)(displayRectangle.height() * 1f));
builder.setView(dialogView);
final AlertDialog alertDialog = builder.create();
// 5. Xử lý nút OK
Button buttonOk = dialogView.findViewById(R.id.buttonOk);
buttonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
});
}
}
Giải thích kỹ thuật quan trọng
// Lấy kích thước vùng hiển thị thực tế (trừ status/navigation bar) Rect displayRectangle = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(displayRectangle); // Ép dialog view mở rộng toàn màn hình dialogView.setMinimumWidth(displayRectangle.width()); dialogView.setMinimumHeight(displayRectangle.height()); // Theme hỗ trợ full screen <item name="android:windowIsFloating">false</item> <item name="android:windowMinWidthMajor">100%</item>
Cách hiện đại hơn - DialogFragment Full Screen
public class FullScreenDialogFragment extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.FullScreenDialogStyle);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.customview, container, false);
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
Window window = dialog.getWindow();
if (window != null) {
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
window.setBackgroundDrawableResource(android.R.color.transparent);
}
}
}
}
// Style
<style name="FullScreenDialogStyle" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">false</item>
</style>
So sánh các cách làm Full-screen Dialog
| Cách | Độ phức tạp | Rotation handling | Khuyến nghị |
|---|---|---|---|
| AlertDialog + setMinWidth/Height | Trung bình | Manual | Đơn giản, nhanh |
| DialogFragment + STYLE_NORMAL | Thấp | Tự động | Best practice |
| BottomSheetDialog (expanded) | Thấp | Tự động | Material Design |
| Activity với Theme.Dialog | Cao | Tự động | Complex flow |
Lưu ý
- DialogFragment tự handle rotation, lifecycle
- Full-screen dialog nên có nút đóng rõ ràng (back button, close icon)
- Cân nhắc dùng BottomSheetDialog cho UX tốt hơn
- WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS cho edge-to-edge