Abhi
Abhi

Reputation: 5561

How to Create Radio button functionality using <Button> Tag in Android

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.

Upvotes: 0

Views: 105

Answers (3)

Ahmed I. Elsayed
Ahmed I. Elsayed

Reputation: 2130

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!

Upvotes: 1

Aleksandar G
Aleksandar G

Reputation: 1181

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
        }
    });

Upvotes: 1

emrekose26
emrekose26

Reputation: 703

Perhaps here may be useful to library work

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

Upvotes: 1

Related Questions