Custom AlertDialog แบบเต็มๆจอ?


วันนี้เราก็จะมาแนะนำวิธีการทำ Custom AlertDialog แบบเต็มจอ ในการพัฒนาแอปพลิเคชันก็รูปแบบในเสนอข้อมูลอยู่ เพราะในบางครั้งเราไม่อยากสร้าง Activity ดังนั้นจึงมานำเสนอวิธีแนวทางในการโค้ดครับ ลุยกันเลย
เปิดไฟล์ res/values/style/style.xml เพิ่ม style
<style name="MyAlertDialogTheme" parent="android:Theme.Dialog">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">match_parent</item>

        <!-- No backgrounds, titles or window float -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>
    </style>

สร้างไฟล์ layout/alert_dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_custom_dialog"
    android:background="#ffffff"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<Space
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>

สร้างเมธอดไว้ใน java/MainActivity.java

public void AlertCustomDialog() {
    LayoutInflater layoutInflaterAndroid = LayoutInflater.from(MainActivity.this);
    View mView = layoutInflaterAndroid.inflate(R.layout.alert_dialog_layout, null);
    AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(MainActivity.this,R.style.MyAlertDialogTheme);
    alertDialogBuilderUserInput.setView(mView);
    
    RelativeLayout layout = (RelativeLayout)mView.findViewById(R.id.layout_custom_dialog);
    alertDialogBuilderUserInput
            .setCancelable(true);
    final AlertDialog alertDialogAndroid = alertDialogBuilderUserInput.create();
    
    
    layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            alertDialogAndroid.cancel();
        }
    });
    alertDialogAndroid.show();
    
}

เปิดไฟล์ layout/activity_main.xml สร้างปุ๋มขึ้นมาเพื่อใช้ในการทดสอบ

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"       
    tools:context="com.geekcreator.MainActivity">
<Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click!"
            />

</RelativeLayout>


เปิดไฟล์ java/MainActivity.java

public class MainActivity extends AppCompatActivity{

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

 Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertCustomManagerData();
        }
    });
}

public void AlertCustomDialog() {...}

}

และนี่แหละครับเป็นวิธีในหารสร้าง AlertDialog แบบเต็มจอ เราก็สามารถนำไปประยุกต์ในเข้ากับงานของแอปพลิเคชันของเราได้ หากมีข้อสงสัยในขั้นตอนใดสามารถคอมเมนต์ไว้ได้ที่ใต้โพสต์ด้านล่างเลยครับ สำหรับวันนี้คงต้องลาไปก่อนสวัสดีครับ

ความคิดเห็น

บทความที่ได้รับความนิยม