dimanche 23 mai 2021

What's simple code/effective code to create CheckBox Programmatically in BottomSheetDialog when click every item in RecyclerView in Android (Java)

I have 100+ item in RecyclerView. these days I have tried to create simple code/effective code to create Checkbox in BottomSheetDialog Programmatically but no success.

My goal :

  • Every item click in RecyclerView will show BottomSheetDialog with different amount checkbox. example :

when click item[0] in RecyclerView will open BottomSheetDialog with 7 checkboxes, if click item[1] in RecyclerView will open BottomSheetDialog with 286 checkboxes, or if click item [2] will open BottomSheetDialog with 200 checkboxes, and so on.

This image will describe, what I meant.

I think my goal is possible instead I create 100+ xml for every item in RecyclerView. Using for looping, array and other... if no simple/effective code for programmatically create checkbox, never mind I will create 100+ xml for every itemListener at RecyclerView

What I have tried :

I successed add single checkbox, but failed to add other checkbox. Here is my code :

Note : I commented for looping because it didn't work. Oke, thanks all :)

        @Override
        public void onClick(View v) {
            int itemPosition = recyclerViewSurat.getChildLayoutPosition(v);
            String namaSurat = suratList.get(itemPosition).getNamaSurat();
//            Toast.makeText(context, namaSurat, Toast.LENGTH_SHORT).show();


            BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(context, R.style.BottomSheetDialogTheme);
            bottomSheetDialog.setContentView(R.layout.layout_bottom_sheet_ayat);

            ConstraintLayout constraintLayout = bottomSheetDialog.findViewById(R.id.constraintLayoutCheckBox);
            ConstraintSet constraintSet = new ConstraintSet();
            ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.MATCH_PARENT,
                    ConstraintLayout.LayoutParams.WRAP_CONTENT
            );

            int margin = (int) convertDpToPixel(30F, context);
            params.setMargins(margin,0,0,0);

//            for (int i = 0; i < 10; i++) {
//                AppCompatCheckBox compatCheckBox = new AppCompatCheckBox(context);
//                compatCheckBox.setId(i + 1);
//                compatCheckBox.setText("Ayat " + i);
//                compatCheckBox.setPadding(25, 0,0,0);
//                constraintLayout.addView(compatCheckBox, params);
//
//                constraintSet.clone(constraintLayout);
//                constraintSet.connect(compatCheckBox.getId(), ConstraintSet.TOP, compatCheckBox.getId() + i, ConstraintSet.BOTTOM, 0);
//                constraintSet.applyTo(constraintLayout);
//            }

            AppCompatCheckBox compatCheckBox = new AppCompatCheckBox(context);
            compatCheckBox.setId(R.id.checkbox2);
            compatCheckBox.setText("Ayat 1");
            compatCheckBox.setPadding(25, 0,0,0);
            constraintLayout.addView(compatCheckBox, params);

            constraintSet.clone(constraintLayout);
            constraintSet.connect(compatCheckBox.getId(), ConstraintSet.TOP, R.id.checkbox1, ConstraintSet.BOTTOM, 0);
            constraintSet.applyTo(constraintLayout);





//            constraintLayout.addView(checkBox);
            bottomSheetDialog.show();

        }

        private float convertDpToPixel(float dp, Context context) {
            return dp * ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);

        }
    }

Here my layout_bottom_sheet_ayat.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bottomSheetLinearLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bottom_sheet_ayat"
    android:orientation="vertical"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="15dp"
        android:text="Pilih Ayat"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold"/>
    
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="5dp"
        android:background="@color/white"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayoutCheckBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <androidx.appcompat.widget.AppCompatCheckBox
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/checkbox1"
            android:layout_marginStart="15dp"
            android:layout_marginTop="24dp"
            android:padding="10dp"
            android:checked="false"
            android:enabled="true"
            android:text="Semua Ayat"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="16dp" />



    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.appcompat.widget.LinearLayoutCompat>

Thanks very much for any sugesstion and help :)




Aucun commentaire:

Enregistrer un commentaire