0

I have constructed a BottomSheetDialog with a couple of buttons in it. Once the user clicked on a button I want to highlight that button next time the dialog opens such that the user knows what their selected last. I mean one of the button has to be highlighted as if it was pressed.

I am using outline style buttons.

 val dialog = BottomSheetDialog(this)
 dialog.setContentView(R.layout.layout_dialog)

 dialog.findViewById<Button>(R.id.button_1)?.setOnClickListener {
        ...
    }
 dialog.findViewById<Button>(R.id.button_2)?.setOnClickListener {
        ...
    }
DeKekem
  • 589
  • 10
  • 20
  • You might want to use a toggle button: https://www.material.io/components/buttons/android#toggle-button - the container allows you to select multiple buttons, or just one (like a radio group). You'll need to store the current selected state somewhere (like `SharedPreferences`) when a button is clicked (e.g. in the `OnButtonCheckedListener`) and then read that in when the dialog is created, so you can initialise with the correct button checked – cactustictacs Jan 10 '23 at 19:08
  • Thanks @cactustictacs, I have tried the toggle button, but the look and feel is not what I am looking for. I want the buttons to be aligned under each other vertically which is not possible with toggle buttons. – DeKekem Jan 10 '23 at 20:56
  • Well if I were you I'd have a `setSelected(id: Int)` function that sets one of your buttons (or none if the ID is *-1* or something) as selected, and deselects the others, and stores that ID like I mentioned. That way your button listeners can call that (passing their ID or *-1* if they're already selected and want to toggle off) and your initial setup can call it as well, when you read the stored state during the dialog creation. (Making `id` nullable would be better but using *-1* is easier if you're using `SharedPreferences`) – cactustictacs Jan 10 '23 at 22:09

1 Answers1

0

I eventually abandon the idea of using Buttons. Setting select did not work. I have used TextView instead with a selector as background to change the background color as desired. Setting textview.activated=true or false does what I need

DeKekem
  • 589
  • 10
  • 20