การสร้าง Dialog Fragment แบบ Full Screen


ผมใช้ Dialog Fragment เพื่อแสดงข้อความ แต่มีปัญหาคือ Dialog ไม่เต็มหน้าจอ มีขอบว่าง
เลยต้องหาทาง กำจัดขอบออก ให้แสดงเต็มจอ

การสร้าง Dialog Fragment แบบ Full Screen
1. สร้าง Project ใหม่ และสร้าง MainActivity โดยมีปุ่ม 1 ปุ่ม เมื่อคลิกไป จะไปเปิด Dialog Fragment

MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.support.v4.app.FragmentTransaction;


public class MainActivity extends AppCompatActivity {

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

    public void showDialog(View view){

        String myTitle = "ตัวอย่าง Fullscreen Dialog";
        String myMessage = "\r\n1. สวัสดีครับ " +
                "\r\n2. jadf djasf dsjfsd fjddfjkdsf " +
                "\r\n3. fkjd fdsklfj dfljfl ladks" +
                "\r\n4. jdfjds fds";

        Bundle args = new Bundle();
        args.putString("key", myMessage);
        args.putString("title", myTitle);
        MyFullScreenFragment newFragment = new MyFullScreenFragment();
       FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        newFragment.setArguments(args);
        newFragment.show(ft, "TAG");
    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.532"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.109" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Button"
        android:onClick="showDialog"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.557"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.395" />


</android.support.constraint.ConstraintLayout>

2. สร้าง layout ของ dialog โดยมี 3 ส่วน คือ ส่วนหัว ส่วนแสดงข้อความ และ ส่วนท้าย
   ส่วนหัวแสดงชื่อ ใช้เป็น textView
   ส่วนแสดงข้อความ ใช้เป็น ScrollView เพื่อให้เลื่อนหน้าจอ ถ้ามีข้อความยากเกินหน้า
   ส่วนท้าย แสดงปุ่ม เมื่อคลิก จะปิด Dialog
full_scr_dialog.xml
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true">
    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:background="#5863a7"
        android:gravity="center"
        android:layout_gravity="center">
        <TextView
            android:id="@+id/header_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="***ส่วนหัว***"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:textStyle="bold"/>

    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="#5863a7"
        android:gravity="center"
        android:layout_gravity="center">
        <Button
            android:id="@+id/btnExpOK"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ตกลง" />

    </RelativeLayout>
    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/footer"
        android:layout_below="@+id/header"
        android:fillViewport="true">
        <LinearLayout
            android:id="@+id/myLayout"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:fillViewport="true">



            <TextView
                android:id="@+id/expText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#313131"
                android:gravity="left" />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>
ปรับจาก
https://www.concretepage.com/android/android-scrollview-with-fixed-header-and-footer-layout-example

3. สร้าง java class สำหรับแสดง Dialog

MyFullScreenFragment.java
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MyFullScreenFragment extends android.support.v4.app.DialogFragment  implements View.OnClickListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // this style is cool. 
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog d = getDialog();
        if (d!=null){
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            d.getWindow().setLayout(width, height);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.full_scr_dialog, container, false);

        // get a variable from MainActivity.java when calling this Dialog. The variable is in the form of key and value.
        Bundle mArgs = getArguments();

        String msg = mArgs.getString("key");
        String thisTitle = mArgs.getString("title");


        final TextView header =  (TextView) root.findViewById(R.id.header_text);
        header.setText(thisTitle);

        final TextView myMsg =  (TextView) root.findViewById(R.id.expText);
        myMsg.setText(msg);
        myMsg.setTextSize(20);
        
        Button btnOK = (Button) root.findViewById(R.id.btnExpOK);
        btnOK.setOnClickListener(MyFullScreenFragment.this);
        return root;
    }
    
    @Override
    public void onClick(View v) {
        dismiss();
    }

}

เท่านี้เอง ก็จะได้ Dialog Fragment แบบเต็มหน้าจอ

หน้าจอแรก
หน้าจอ Dialog Fragment แบบเต็มจอ



ขอบคุณ
http://formatmysourcecode.blogspot.com/
ในการจัดรูปแบบโค้ด เพื่อแสดงบน html ใน Blogger นะครับ

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

อุปมา อุปไมย สำนวนการเปรียบเทียบ ของไทย

แนวข้อสอบ เงื่อนไขสัญลักษณ์

ความสามารถทั่วไปด้านเหตุผล การหาความสัมพันธ์จาก ภาพ สัญลักษณ์