Hello, ๋‚˜๋‚˜'s world !

DialogFragment ๋ณธ๋ฌธ

๐Ÿ’š Android

DialogFragment

Nana0 2021. 1. 7. 22:10

DialogFragment Customize

 

์˜ค๋Š˜์€ DialogFragment๋ฅผ ์ปค์Šคํ…€ ํ•˜๋Š”๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ๋‹ค.

 

  1. ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๋„์šธ xml ๋ ˆ์ด์•„์›ƒ์— ๋ฒ„ํŠผ์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.

  2.  ๋‹ค์ด์–ผ๋กœ๊ทธ xml ๋ ˆ์ด์•„์›ƒ ๊ตฌํ˜„

  3.  DialogFragment๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค ๊ตฌํ˜„

  4.  1๋ฒˆ์˜ ํ”„๋ž˜๊ทธ๋จผํŠธ์— 3๋ฒˆ์„ ์—ฐ๊ฒฐํ•œ๋‹ค. 

 

 

1.  fragment_main.xml 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Button
        android:id="@+id/f_show_btn"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="Show" />
        
</androidx.constraintlayout.widget.ConstraintLayout>

 

 

2.  dialog_receiver.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:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:textSize="16dp"
        android:text="๋ฐ›๋Š” ์‚ฌ๋žŒ"
        android:background="@color/colorPrimary"/>
    <Spinner
        android:id="@+id/dialog_receiver"
        android:layout_width="match_parent"
        android:layout_height="50dp"/>

    <LinearLayout
        android:layout_marginTop="135dp"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/dialog_receiver_ok_btn"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_weight=".5"
            android:gravity="center"
            android:text="ํ™•์ธ"
            android:textSize="16dp"
            android:background="@color/colorPrimary"/>
        <View
            android:layout_width="3dp"
            android:layout_height="match_parent"
            android:background="#ffffff" />
        <Button
            android:id="@+id/dialog_receiver_cancle_btn"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_weight=".5"
            android:gravity="center"
            android:text="์ทจ์†Œ"
            android:textSize="16dp"
            android:background="@color/colorPrimary"/>
    </LinearLayout>

</LinearLayout>

 

 

3.  LetterReceiverDialog.java

public class LetterReceiverDialog extends DialogFragment implements View.OnClickListener {

    public static final String TAG_EVENT_DIALOG = "dialog_event";

    public LetterReceiverDialog(){}

    public static LetterReceiverDialog getInstance(){
        LetterReceiverDialog letterReceiverDialog = new LetterReceiverDialog();
        return letterReceiverDialog;
    }


    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.dialog_receiver, container);
        Spinner mSpinner = (Spinner)v.findViewById(R.id.dialog_receiver);

        ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(getActivity(),
               						    android.R.layout.simple_spinner_item,
              						    getResources().getStringArray(R.array.testSpinner));
        mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinner.setAdapter(mAdapter);

        Button mOkBtn = (Button)v.findViewById(R.id.dialog_receiver_ok_btn);
        Button mCancleBtn = (Button)v.findViewById(R.id.dialog_receiver_cancle_btn);
        mOkBtn.setOnClickListener(this);
        mCancleBtn.setOnClickListener(this);
        
        //ํ™”๋ฉดํ„ฐ์น˜์‹œ ๊บผ์ง ๋ง‰๊ธฐ
        setCancelable(false);

        return v;
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

	//๋‹ค์ด์–ผ๋กœ๊ทธ ์‚ฌ์ด์ฆˆ ์กฐ์ ˆ
    @Override
    public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();
        if(window == null) return;
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = 1000;
        params.height = 1000;
        window.setAttributes(params);
    }
}

 

 

 

4.  FragmentMain.java

onCreateView() ๋ฐ‘์— ์ž‘์„ฑํ•ด์ค€๋‹ค.

        Button mAddBtn = (Button)v.findViewById(R.id.f_show_btn);
       
        mAddBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LetterReceiverDialog mletterReceiverDialog = LetterReceiverDialog.getInstance();
                mletterReceiverDialog.show(getFragmentManager(), LetterReceiverDialog.TAG_EVENT_DIALOG);
            }
        });

 

 


๋‹ค์ด์–ผ๋กœ๊ทธ ์ƒ์ž๋ฅผ ๋””์Šคํ”Œ๋ ˆ์ด์˜ ๋น„์œจ์— ๋”ฐ๋ผ ์ผ์ •ํ•œ ํฌ๊ธฐ๋กœ ๋งŒ๋“ค๊ณ ์‹ถ์—ˆ๋Š”๋ฐ ์ž˜์•ˆ๋˜์—ˆ๋‹ค ใ…œใ…œ

display์˜ x,y๊ฐ’์„ ๋ฐ›์•„์˜ฌ์ˆ˜์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ fragment์—์„œ ๊ตฌํ˜„ํ•ด๋ณด์•„์•ผ๊ฒ ๋‹ค. ( dimen ์œผ๋กœ  %๋ฅผ ์ค˜๋ณด๋ คํ–ˆ์ง€๋งŒ...ใ…‹ใ…‹)

์š”๋ฒˆ์ฃผ Firebase DB์— ๊ฐ’๋„ฃ์„๋•Œ ์†๋ด์•ผ๊ฒ ๋‹ค. 

 

 

 


<๊ฐœ๋ฐœํ™˜๊ฒฝ>

java version "1.8.0_271"

android API 10.0 (Q)

android studio "4.0.1"

 

<์ฐธ๊ณ >

developer.android.com/guide/topics/ui/dialogs?hl=ko

 

๋Œ€ํ™”์ƒ์ž  |  Android ๊ฐœ๋ฐœ์ž  |  Android Developers

๋Œ€ํ™”์ƒ์ž๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ฒฐ์ •์„ ๋‚ด๋ฆฌ๊ฑฐ๋‚˜ ์ถ”๊ฐ€ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๋ผ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์ž‘์€ ์ฐฝ์ž…๋‹ˆ๋‹ค. ๋Œ€ํ™”์ƒ์ž๋Š” ํ™”๋ฉด์„ ๊ฐ€๋“ ์ฑ„์šฐ์ง€ ์•Š์œผ๋ฉฐ ๋ณดํ†ต์€ ์‚ฌ์šฉ์ž๊ฐ€ ๋‹ค์Œ์œผ๋กœ ๊ณ„์† ์ง„ํ–‰ํ•˜๊ธฐ ์ „์— ์กฐ

developer.android.com

developer88.tistory.com/132

pluu.github.io/blog/rxjava/2017/02/04/android-alertdialog

rmirabelle.medium.com/how-to-set-dialogfragment-width-and-height-733c5b174178

Comments