how to change a button background and enable it after other buttons are pressed

Related searches

How do I enable the "Next" button after 'Yes' or 'No' from each question is pressed? I want to make my "Next" button active (change the background and enable) only after all the questions have been answered. I don't know whether I should use loop or if it's possible by if-else. Any help is very appreciated.

I have developed my program in a way that users can either press the 'yes' or 'no' button and after answering all questions the "Next" button should get activated (change the background and enable).

int one = 0;
int two = 0;
int three = 0;
int four = 0;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_price_estimate_question_one);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String phone_price = extras.getString("phone_price");
        final String numonly = phone_price.replaceAll("[^0-9]", "");

        final int phoneprice = Integer.parseInt(numonly);



        final Button ques_one_yes = (Button)findViewById(R.id.ques_one_yes);
        final Button ques_one_no = (Button)findViewById(R.id.ques_one_no);
        final Button ques_two_yes = (Button)findViewById(R.id.ques_two_yes);
        final Button ques_two_no = (Button)findViewById(R.id.ques_two_no);
        final Button ques_three_yes = (Button)findViewById(R.id.ques_three_yes);
        final Button ques_three_no = (Button)findViewById(R.id.ques_three_no);
        final Button ques_four_yes = (Button)findViewById(R.id.ques_four_yes);
        final Button ques_four_no = (Button)findViewById(R.id.ques_four_no);
        final Button nextstep = (Button)findViewById(R.id.next_step);


        ques_one_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                one = 1;
                ques_one_yes.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
                ques_one_yes.setBackground(getDrawable(R.drawable.button_background));
                ques_one_no.setTextColor(getApplication().getResources().getColor(R.color.black));
                ques_one_no.setBackground(getDrawable(R.drawable.inactive_button_background));
            }
        });

        ques_one_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                one = 1;
                ques_one_no.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
                ques_one_no.setBackground(getDrawable(R.drawable.button_background));
                ques_one_yes.setTextColor(getApplication().getResources().getColor(R.color.black));
                ques_one_yes.setBackground(getDrawable(R.drawable.inactive_button_background));

            }
        });

        ques_two_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                two = 1;
                ques_two_yes.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
                ques_two_yes.setBackground(getDrawable(R.drawable.button_background));
                ques_two_no.setTextColor(getApplication().getResources().getColor(R.color.black));
                ques_two_no.setBackground(getDrawable(R.drawable.inactive_button_background));

            }
        });

        ques_two_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                two = 1;
                ques_two_no.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
                ques_two_no.setBackground(getDrawable(R.drawable.button_background));
                ques_two_yes.setTextColor(getApplication().getResources().getColor(R.color.black));
                ques_two_yes.setBackground(getDrawable(R.drawable.inactive_button_background));
            }
        });

        ques_three_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                three = 1;
                ques_three_yes.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
                ques_three_yes.setBackground(getDrawable(R.drawable.button_background));
                ques_three_no.setTextColor(getApplication().getResources().getColor(R.color.black));
                ques_three_no.setBackground(getDrawable(R.drawable.inactive_button_background));
            }
        });

        ques_three_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                three = 1;
                ques_three_no.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
                ques_three_no.setBackground(getDrawable(R.drawable.button_background));
                ques_three_yes.setTextColor(getApplication().getResources().getColor(R.color.black));
                ques_three_yes.setBackground(getDrawable(R.drawable.inactive_button_background));
            }
        });

        ques_four_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                four = 1;
                ques_four_yes.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
                ques_four_yes.setBackground(getDrawable(R.drawable.button_background));
                ques_four_no.setTextColor(getApplication().getResources().getColor(R.color.black));
                ques_four_no.setBackground(getDrawable(R.drawable.inactive_button_background));
            }
        });

        ques_four_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                four = 1;
                ques_four_no.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
                ques_four_no.setBackground(getDrawable(R.drawable.button_background));
                ques_four_yes.setTextColor(getApplication().getResources().getColor(R.color.black));
                ques_four_yes.setBackground(getDrawable(R.drawable.inactive_button_background));

            }
        });

    }
}

Quesions


The simplest implementation that comes to my mind is to check all of the int values you defined. You can do it in a helper method together with background and color changes

private void helperMethodName (Button buttonNo, Button buttonYes){
    buttonNo.setTextColor(getApplication().getResources().getColor(R.color.colorAccent));
    buttonNo.setBackground(getDrawable(R.drawable.button_background));
    buttonYes.setTextColor(getApplication().getResources().getColor(R.color.black));
    buttonYes.setBackground(getDrawable(R.drawable.inactive_button_background));

    if (one != 0 && two != 0 && three != 0 && fore != 0) {
        nextstep.setEnabled(true);
    }
}

And then just call this method in your on clicks

public void onClick(View v) {
one = 1;
helperMethodName(ques_one_no, ques_one_yes);
}

Buttons, Overview � Enable the API � Integrate the plugin � Initialize the library and When the user clicks a button, the Button object receives an on-click To customize individual buttons with a different background, specify the android:background change appearance during different states, such as when clicked. For changing background color/image based on the particular event(focus, press, normal), you need to define a button selector file and implement it as background for button. For example:button_selector.xml (define this file inside the drawable folder) <?xml version="1.0" encoding="utf-8"?>


you can try "RadioGroup" and "RadioButton"

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    @Override
    public void onCheckedChanged(RadioGroup rb, int id){
            if (id == R.id.ques_one_yes)
    }
})

and in layout file

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <RadioButton
        android:id="@+id/ques_one_yes"
        android:layout_width="wrap_content"
        android:BackGround="@drawable/rb_bg"
        android:layout_height="wrap_content"/>
</RadioGroup>

and in drawable file rb_bg.xml

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

and the TextColor is other drawable file

Styling buttons, the right way, Learn how to create an accessible button style which can be used color: inherit ; background-color: transparent; /* show a hand cursor on Let's start with a style for the :active state, triggered briefly when your button or link is clicked: and activate form fields, buttons, links and other interactive elements. You would get an instance of each form that you want to change the backgound image and then set the background image in the current button click event. I am not presenting code as I do VB.NET not C#. Now an alternate is to setup a DataBinding for BackGroundImage i.e. simple example which you could tweak. create a PictureBox and assign an image


Your are taking the initial values for all the options as 0 and making it 1 on OnClick. Just give an if-else in your onCreate method like:

if(one = 1 && two = 1 && three = 1 && four = 1) button.setEnable(true); else button.setEnable(false);

if all values are 1 then enable the button, else disable it.

Change button color when clicked on Dynamic Page, just use enable or disable instead showing and hiding 2 different buttons. Just relate it on another event. $w("#yourbuttonid"). To change android button background color when the button is click; To toggle between two colors when the button is click. We will focus on the second option . But the good thing is that you can easily adopt this code to work in the case of option 1. activity_change_button_color.xml


In general if you want to achieve this implementation you can try the following ,

To enable/disable the next button:

fun enableNextButton(isEnable: Boolean) {
    if(isEnable){
        button_next?.alpha = 1.0f
        button_next?.isEnabled = true
    }else{
        button_next?.alpha = 0.5f
        button_next?.isEnabled = false
    }
}

So call enableNextButton(true) if you want to enable the next button otherwise pass the false param to method. Also alpha sets the button blur which helps you to show the disable button view.

Changing color on button click, After looking at this a little bit longer I was thinking that you could make it one line shorter by making another line longer by moving the ternary blue; height: 100px; width: 150px; } button.pressed { background-color: red; } </style> all the buttons on page then use class selector or if its only one button then� If you want to change the color of the button while it's pressed, you can change the PressedColor (for the font color) and PressedFill (for the background color) of the button. If you want to use the button as a toggle so that anytime it is pressed it will change the color of either the button itself or another control, you can use a context


How to Change Button Color OnClick in Javascript, In this video tutorial, you will learn how to change button color onClick in Change Background Duration: 4:21 Posted: Nov 11, 2019 I have some buttons which I set its background color as red, green and blue separately. When I press the button, click event is generated, but there is no change in gui for the user to know the button is pressed. Android button’s default background grayish color changes to orange and come back to grayish color after releasing the pressed state.


Each link has five different states: link, hover, active, focus and visited. Link is the normal appearance, hover is when you mouse over, active is the state when it's clicked, focus follows active and visited is the state you end up when you unfocus the recently clicked link.


Even buttons that have custom background images making them look like big solid buttons don’t do anything when you push them. They remain static and unresponsive. It doesn’t have to be this way. Just use the “active” state to give your buttons a “pressed” look or whatever other look you wish.