Search code examples
javaandroidxmlandroid-bottomsheetdialog

How do I make my Bottom Sheet Dialog dynamic?


it's the first time I am trying to implement a BottomSheetDialog into my Android Studio project. In order to get a little bit more familiar with the process I tried following this tutorial on Youtube: https://youtu.be/hfoXhiMTc0c. In my actual Java Class, the BottomSheet is activated when I am scanning an NFC-Chip containing different information. However I am not able to display the information from the Chip dynamicly on the Sheet. I guess that is due to the Sheet being static? How would I be able to display the information from the chip which is already stored in a variable in my Java class to be displayed in a textfield of the BottomSheet?

Any help is appreciated, thank you!

here's the code snippet of the java class where the BottomSheet is extended:

final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(

                Scan.this, R.style.BottomSheetDialogTheme
        );
        View bottomSheetView = LayoutInflater.from(getApplicationContext())
                .inflate(
                        R.layout.layout_bottom_sheet,
                        (LinearLayout)findViewById(R.id.bottomSheetContainer)

                );
        bottomSheetView.findViewById(R.id.addToCloset).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomSheetDialog.dismiss();
            }
        });
        bottomSheetDialog.setContentView(bottomSheetView);
        bottomSheetDialog.show();```

Solution

  • I am not familiar with BottomSheetDialog. But,

            View bottomSheetView = LayoutInflater.from(getApplicationContext())
                .inflate(
                        R.layout.layout_bottom_sheet,
                        (LinearLayout)findViewById(R.id.bottomSheetContainer)
    
                );
    

    You should be able to replace above code with,

        View bottomSheetView = LayoutInflater.from(getApplicationContext())
            .inflate(
                    R.layout.layout_bottom_sheet,
                    null
    
            );
    

    Now go to layout_bottom_sheet.xml layout file. According to your code it should have a linear layout with no id. Give it a id. I'll take that id as "test_ll".

    Now below the above code you can,

    LinearLayout ll = bottomSheetView.findViewById(R.id.test_ll);
    

    After that you can add views dynamically to ll. For adding views dynamically to LinearLayout refer,

    Add text view to Linear layout

    Edit:

    If you want to work with Views inside LinearLayout, you can do it by,

    View view = ll.findViewById(R.id.view_id);
    

    If your textview is in the ll,

    TextView textView1 = ll.findViewById(R.id.tvcolor);       
    textView1.setText("Hello!!");
    

    This will solve your problem.