Ritesh Sharma
Ritesh Sharma

Reputation: 27

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

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

Upvotes: 0

Views: 69

Answers (4)

Mikhail
Mikhail

Reputation: 191

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

Upvotes: 0

Harshad07
Harshad07

Reputation: 608

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.

Upvotes: 0

Aman Gupta
Aman Gupta

Reputation: 271

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.

Upvotes: 0

igarasi
igarasi

Reputation: 137

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

Upvotes: 1

Related Questions