본문 바로가기
안드로이드 자바

[JAVA][Android] BottomSheetDialogFragment 에 값 전달하고 받기

by teamnova 2024. 8. 4.
728x90

오늘은 BottomSheetDialogFragment 를 커스텀하여 메인 액티비티에서 입력한 값을 BottomSheet에 띄우고, BottomSheet에서 입력한 값을 메인 액티비티에서 받아보겠습니다. 

 

레이아웃 xml 파일 코드(custom_bottomsheet_layout.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
    android:backgroundTint="#FF5722">

    <TextView
        android:id="@+id/bottomSheet_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="BottomSlide"
        android:textColor="@color/black"
        android:textSize="25dp"

        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.widget.Button
        android:id="@+id/bottomSheet_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:background="@color/purple_200"
        android:text="메인으로 보내기"
        android:textSize="25dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bottomSheet_editText" />

    <EditText
        android:id="@+id/bottomSheet_editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="7"
        android:layout_marginTop="50dp"

        android:hint="텍스트 입력란"
        android:inputType="text"
        android:textSize="25dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

 

레이아웃 xml 파일 코드(main_layout.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/main_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BottomSheet로 보내기"
        android:textSize="20dp"
        app:layout_constraintTop_toBottomOf="@+id/editText"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text"
        android:hint="텍스트 입력란"
        android:textSize="20dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/main_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:textSize="20dp"
        android:text="받은텍스트"
        app:layout_constraintBottom_toTopOf="@+id/editText"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

custom_BottomSheet 클래스 자바 코드

public class custom_BottomSheet extends BottomSheetDialogFragment {
        private String getText;
        EditText bottomSheet_editText;
        Context context;
        Button bottomSheet_button;
        TextView bottomSheet_textView;

        BottomSheetBehavior bottomSheetBehavior;
        public custom_BottomSheet(Context context) {
            this.context = context;
        }


    @Nullable
        @Override
        public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.custom_bottomsheet_layout, container, false);
            bottomSheet_editText=  view.findViewById(R.id.bottomSheet_editText);
            bottomSheet_button=  view.findViewById(R.id.bottomSheet_button);
            bottomSheet_textView=  view.findViewById(R.id.bottomSheet_textView);

            Bundle args = getArguments();  //메인 엑티비티에서 받아오는 값
            getText = args.getString("send_text");
            bottomSheet_textView.setText(getText);
            bottomSheet_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onBtnListener != null) {
                        //메인 엑티비티에서 설정한 dialog의 setOnBtnClickListener의 onBtnClick에 넘길 값
                        //메인 액티비티로 bottomSheet_editText에 있는 값을 넘김
                        onBtnListener.onBtnClick(bottomSheet_editText.getText().toString());
                    }
                }
            });
            return view;
        }
        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            View bottomSheet = (View) view.getParent();
            bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
            bottomSheet.getLayoutParams().height = (int) (getResources().getDisplayMetrics().heightPixels * 0.5);//bottomSheet 크기를 메인 액티비티의 반을 채우도록 함
            bottomSheet.setLayoutParams(bottomSheet.getLayoutParams());
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }


    public interface OnBtnListener {
        void onBtnClick(String BottomSheet_text);
    }

    public OnBtnListener onBtnListener;
    public void setOnBtnClickListener(OnBtnListener listener) {
        this.onBtnListener = listener;
    }


}

 

 

메인 액티비티 자바 코드

public class main_Activity extends AppCompatActivity {

    EditText editText;
    Button button;

    TextView main_textView;


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

        editText = findViewById(R.id.editText);
        button=findViewById(R.id.main_button);
        main_textView=findViewById(R.id.main_textView);
        custom_BottomSheet dialog = new custom_BottomSheet(this);
        dialog.setOnBtnClickListener(new custom_BottomSheet.OnBtnListener() {
            @Override
            public void onBtnClick(String BottomSheet_text) {
                //custom_BottomSheet 에서 bottomSheet_editText에 입력된 값을 BottomSheet_text으로 받아옴.
                //BottomSheet_text로 받아온 값을 메인엑티비티에 있는 main_textView에 출력함
                main_textView.setText(BottomSheet_text);
                dialog.dismiss();
            }
        });


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getSupportFragmentManager().findFragmentByTag("custom_BottomSheet")==null){
                    //custom_BottomSheet에 넘길 값을 설정
                    Bundle args = new Bundle();
                    args.putString("send_text", editText.getText().toString());
                    dialog.setArguments(args);
                    dialog.show(getSupportFragmentManager(), "custom_BottomSheet");
                }

            }
        });

    }
}