0

I initially set background of button with this code at onCreateView.

uc.setBackgroundResource(R.drawable.saat_button_none);

If I initially set background or textColor of button I want to prevent style change when I use onClick

public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bir:
            uc.setBackgroundResource(R.drawable.saat_button); //Should not work
            dort.setBackgroundResource(R.drawable.saat_button_sel);
            bes.setBackgroundResource(R.drawable.saat_button_sel);
 }
}

Is that possible?

Edit: I don't want to use if statement since I have lots of buttons I just want to lock style of button.

ilvthsgm
  • 586
  • 1
  • 8
  • 26

1 Answers1

1

To do this, create a custom view simply by extending View and override all methods related to background and put your logic their if background has changed once then overridden method should throw exception saying that you can't change the style as it has been changed while setup the default look and feel.

public class CustomButton extends Button {
  boolean backgroundChanged = true;
  public CustomButton(Context context) {
      this(context, null);
  }

  public CustomButton(Context context, @Nullable AttributeSet attrs) {
      this(context, attrs, 0);
  }

  public CustomButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
  }

  @Override
  public void setBackgroundResource(int resid) {
      if(backgroundChanged){
          throw new RuntimeException("you can't change the style as it has been changed while setup the default look and feel");
      }
      super.setBackgroundResource(resid);
  }
}

At last in layout file replace <Button tag with <CustomButton

Krishna Sharma
  • 2,828
  • 1
  • 12
  • 23