In my app I need to be able to change the background color of a button and back to the default color. Changing the color to a custom color works, but my code for reversing the process has given me issues.
My button code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.custom_practice, container, false);
mNomButton = (Button) view.findViewById(R.id.custom_practice_nom_button);
mNomButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mNomIsSelected = !mNomIsSelected;
mNomButton.setBackgroundResource(mNomIsSelected ? R.color.buttonSelected : android.R.drawable.btn_default);
updateView(mNomButton);
}
});
return view;
}
When I reset the button resource I end up with a bordered button where I had borderless before:
On layout inflate:
OnClick the first time:
OnClick the second time:
I would like to avoid having to create a custom drawable that mimics the flat button. Is there a way to get the default borderless button resource?
when u set background in second time set it by getting background from another button which has the default background so for ex let's say button in red is b1 and button in default is b2
Then code to set background for b1 to become default is
It took some time because many of the answers included deprecated code, but I have a solution to my issue. Using the suggestion of Mohamed I grabbed the default Drawable value of one of the buttons and stored it. The biggest issue was finding a way for both the default color and my color from colors.xml to be the same type for my ternary if to work.