0

I am trying to beautify some of the UI components in Android. I need to create a button with black border and background color. Everything is fine if I hardcode the variables into XML. But i have different buttons with different background color and different border color and size and etc. all the variations you could say. And it is surely wrong if I go and create all the variations in separate XML files. At the moment, I am creating styles in code but it looks a little inefficient.

gradient.setColor(getContext().getResources().getColor(R.color.ORANGE));
gradient.setCornerRadius(5);
gradient.setStroke(5, 0xFF000000);
button.setBackground(gradient);

And if I want to have button with different background color, I need to create another gradient and initialize it and set to button's resource. Another thing, when I am setting the background of the button, I am also losing the color change when the button is pressed. How do you create an XML drawable resource that can be customized in the interface? Or what is the easier way?

Thanks.

OmerHalit
  • 401
  • 5
  • 18
  • 1
    Create a button selector. Check this out http://stackoverflow.com/questions/14023886/android-button-selector – Rohit5k2 Aug 10 '15 at 10:44
  • @Rohit5k2 But I still need to hardcode the values for attributes. How do I do that without having to pre-define my colors? – OmerHalit Aug 10 '15 at 10:48
  • Its possible but you will keep facing similar problems every time and by the time you achieve what you are trying to, your code would be very difficult to manage. I would again suggest xml selectors. – Rohit5k2 Aug 10 '15 at 10:51

1 Answers1

0

used this:-

 Drawable mDrawable = ContextCompat.getDrawable(youContext, R.drawable.youdrawable)
            mDrawable.setColorFilter(PorterDuffColorFilter(youColor, PorterDuff.Mode.MULTIPLY)
           button.setbackground(mDrawable)
Ashwani kumar
  • 164
  • 15