-3

I am in new in android development. I want to change my button background through java coding in Android.

enter image description here

What I want is when I click button it turns to yellow and white(both male-female button)click after click.

0xDEADC0DE
  • 2,453
  • 1
  • 17
  • 22
Sarvesh
  • 11
  • 1
  • 6

4 Answers4

1

You could do something like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    Button buttonMale = (Button) findViewById(R.id.btn_male);
    Button buttonFemale = (Button) findViewById(R.id.btn_female);

    buttonMale.setOnClickListener(getGenderOnClickListener(buttonFemale));
    buttonFemale.setOnClickListener(getGenderOnClickListener(buttonMale));
}

private Consumer<View> getGenderOnClickListener(View other) {
    return view -> {
        view.setBackgroundColor(Color.YELLOW);
        other.setBackgroundColor(Color.WHITE);         
    }
}
Simon Schiller
  • 644
  • 1
  • 8
  • 23
0

first your need to bind your button in activity like this

btnMale=(Button) findViewById(R.id.btnMale);
btnFemale=(Button) findViewById(R.id.btnFemale);

than setOnClickListener() of our button like this

btnMale.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //btnMale.setBackgroundResource(R.drawable.background);// change background your button like this

            btnMale.setBackgroundColor(ContextCompat.getColor(MainActivity.this,R.color.colorPrimary));// use this to set color as background 

            btnFemale.setBackgroundResource(android.R.drawable.btn_default;);// use this to set color as background 
        }
    });



btnFemale.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


        btnFemale.setBackgroundColor(ContextCompat.getColor(MainActivity.this,R.color.colorPrimary));// use this to set color as background 

        btnMale.setBackgroundResource(android.R.drawable.btn_default;);// use this to set color as background 
    }
});
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0
Button11.setBackgroundColor(getResources().getColor(R.color.red));

// OR, if you're not:

Button11.setBackgroundColor(Button11.getContext().getResources().getColor(R.color.red));

Or, alternatively:

Button11.setBackgroundColor(Color.RED); // From android.graphics.Color Or, for more pro skills:

Button11.setBackgroundColor(0xFFFF0000); // 0xAARRGGBB

Vijay Chaudhary
  • 228
  • 2
  • 12
0

Based On @Simon Schiller answer: For Multiple text views on click listener (like tabview UI)

val TYPE_DEFAULT_DRAWABLE = R.drawable.background_button_default
val TYPE_SELECTED_DRAWABLE = R.drawable.background_button_selected

val TYPE_SELECTED_STYLE: Int = R.style.ButtonTertiaryForSelected
val TYPE_DEFAULT_STYLE: Int = R.style.ButtonTertiaryForDefault


private fun getOnClickListener(other: List<AppCompatTextView>): View.OnClickListener {
    return View.OnClickListener { view ->
        (view as AppCompatTextView).setBackgroundResource(TYPE_SELECTED_DRAWABLE)
        (view as AppCompatTextView).setTextAppearance(TYPE_SELECTED_STYLE)
        for (i in other.indices) {
            other[i].setBackgroundResource(TYPE_DEFAULT_DRAWABLE)
            other[i].setTextAppearance(TYPE_DEFAULT_STYLE)
        }

    }
}
Priyanka
  • 247
  • 4
  • 19