0

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();```
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
e5lite
  • 11
  • 6

1 Answers1

0

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.

HMR
  • 146
  • 1
  • 7
  • Perfect, thanks a lot! Everything except one thing is working now. The link you kindly sent me is not quite fitting for my problem since I am just trying to access an already existing TextView of the .xml file and not trying to create a new one. I tried using : ```TextView textView1 = (TextView)findViewById(R.id.tvcolor); textView1.setText("Hello!!");``` However, when trying to run the code the application is just crashing:(. Any idea on how to fix? – e5lite Nov 13 '21 at 14:54
  • Does that textview is in the LinearLayout? – HMR Nov 13 '21 at 15:02
  • Check the edit section of my answer. It might solve your problem. – HMR Nov 13 '21 at 15:13