Zhu
Zhu

Reputation: 3905

onClick() to change button border color rather changing button color

I am new to android, Working in an alarm app. For that in this app, I have 7 round shaped buttons each denotes the days of a week starting from Sunday to Saturday.

What I want to do is when the user clicked on it I want to change the border color of the clicked button and the button text appears on a textview .

And then if the user selects the buttons(days) randomly but the textview text (selected day ) should be in an ordered way .

ex: the user first select Wednesday then Sunday the textview first show the Wednesday then Sunday here Sunday comes 1 st automatically and Wednesday next .(each day will have a specific position if the user select the days randomly, textview will show all the days in an arranged manner )

demo.xml

<Button
    android:id="@+id/mbtn_Sun"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/gray_button"
    android:gravity="center_vertical|center_horizontal"
    android:text="Sun"
    android:textAllCaps="false"
    android:textColor="#e20a1f" />

Can anyone help me to solve this?

Upvotes: 1

Views: 959

Answers (3)

crgarridos
crgarridos

Reputation: 9283

I suggest using a Checkbox instead.

Here below an example that shows a simple implementation:

<CheckBox
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:gravity="center"
    android:button="@null"
    android:background="@drawable/checkbox_custom_background" />

Where checkbox_custom_background.xml is:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/gray_button" android:state_checked="false"/>
    <item android:drawable="@drawable/green_button" android:state_checked="true"/>
    <item android:drawable="@drawable/gray_button"/>
</selector>

Upvotes: 2

Luca Nicoletti
Luca Nicoletti

Reputation: 2427

You can do as follow:

<Button
    android:id="@+id/mbtn_Sun"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/button_background"
    android:gravity="center_vertical|center_horizontal"
    android:text="Sun"
    android:textAllCaps="false"
    android:textColor="#e20a1f" />

and then use a selector

button_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/green_button" android:state_pressed="true"/>
    <item android:drawable="@drawable/green_button" android:state_focused="true"/>
    <item android:drawable="@drawable/gray_button"/>
</selector>

Upvotes: 0

Abhra
Abhra

Reputation: 92

first of all initialise your button.

mButton=(Button)findViewById(R.id.mbtn_Sun);

then do like this

mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mButton.setBackgroundResource(R.drawable.green_button);

        }
    });

Upvotes: 0

Related Questions