0

enter image description here

I need to create this type of UI. But it should work similar to Radio Button that is when one button got selected other two should be deselected.

There are various approaches like using three Images for each button and also images for pressed and unpressed state.

But how to achieve this using Button Tag.

Abhi
  • 5,501
  • 17
  • 78
  • 133

3 Answers3

1

Perhaps here may be useful to library work

https://github.com/pucamafra/android-segmentedtab

emrekose26
  • 683
  • 1
  • 8
  • 21
1

You can create 3 buttons and control other buttons with OnClickListener. For example:

layout:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>  

class:

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           btn1.setSelected(true);
           btn2.setSelected(false);
           btn3.setSelected(false);
           //Do something
        }
    });

    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           btn1.setSelected(false);
           btn2.setSelected(true);
           btn3.setSelected(false);
           //Do something
        }
    });

    btn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           btn1.setSelected(false);
           btn2.setSelected(false);
           btn3.setSelected(true);
           //Do something
        }
    });
Aleksandar G
  • 1,163
  • 2
  • 20
  • 25
1

Sure, here .. keep it in selected state in OnClickListener when event x is fired. when event y is fired, keep button2 in selected state *& don't forget to remove button1 from selected state, Just try to improve this, it will work

I just made this , you are welcome to use & commit changes too!

Community
  • 1
  • 1
Ahmed I. Elsayed
  • 2,013
  • 2
  • 17
  • 30