When using the "selector" to specify different images for buttons for different states, such as pressed, focused etc, do I have to write an xml file for each button? I have about 15-20 buttons in my app, so was wondering if there is a way to write just one xml and refer to parts of it?
Thanks
Chris
There is no way to refer to parts of a StateListDrawable, at least that I am aware of.
However, since StateListDrawables are usually used for button backgrounds, it is unclear why you need more than one in the first place.
StateListDrawable changes the Drawable based on the current state, as state changes at runtime. Choosing what Drawable to use initially is something you already do when specifying the Drawable in the first place.
Even classes like LevelListDrawable still require you to specify the level to the Drawable, not on the actual View the Drawable is used on and AFAIK Android automatically checks if a Drawable can handle states and if so passes them. The Drawable never gets reference to the View the Drawable is being used on.
I would just create multiple Drawable files for each button. If you want to share certain attributes of the Button like text color, padding, font sizing, etc you should use Android styles.
Android styles would let you have styles like BlueButton, RedButton, GreenButton which you can inherit styles. So you could have BlueButton that sets the text color, text size, text shadows, the drawable for blue etc, then create another style for Red that just inherits BlueButton and only changes the Drawable (although it can change any attribute it wishes) and then just use them on your Button widgets. You would still need to have multiple Drawable files for the styles to link to, but the styles can all be in one file.
Related
We have an imageView whose background has a vector asset drawable(ic_star.xml). When I change the background tint of this imageView programatically with a custom color like below, it causes to change the color of imageViews on other pages using this vector assset. In other words, whatever color I set last, that color stays on all other screens. I think this function directly affects the vector asset used throughout the application.
binding.imageView.background.setTint(Color.parseColor("#0E8E1D"))}
In addition, when I use android:backgroundTint attribute in XML, this problem does not occur. Other pages are not affected. But since I get the colors from the api, I have to do it programmatically. We do not prefer to use databinding in our project as it increases the build time. That's why I can't handle this with binding expression.
I found a solution when I wanted to look at the effect of the imageView.background.setTint(color : Int) function in a simple app. I wasn't too sure if it was the right solution. backgroundTintList property worked like android:backgroundTint attribute in xml.
binding.imageView.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#0E8E1D"))
You can review my example app in github and try the difference imageView.backgroundTintList and imageView.background.setTint() methods. I'm waiting for your comments.
https://github.com/tugceaktepe/ImageViewTint
Thanks.
I have a screen where multiple Buttons use the same background Drawable. I have reusable code I use in various projects to add an OnTouch listener that adds a gray color filter while a button is being touched. That usually works fine, but in this case ALL the buttons are tinted when any of them is pressed.
I see an explanation in http://developer.android.com/guide/topics/graphics/2d-graphics.html:
Note: Each unique resource in your project can maintain only one
state, no matter how many different objects you may instantiate for
it. For example, if you instantiate two Drawable objects from the same
image resource, then change a property (such as the alpha) for one of
the Drawables, then it will also affect the other.
The suggested solution is to use a TweenAnimation, which does not seem to work with color filters.
I also saw Android: Cloning a drawable in order to make a StateListDrawable with filters which suggests using drawable.getConstantState().newDrawable(). This does not seem to make a difference. I'm guessing that as long as the same physical image file is used, all Drawables will be affected by a change to any other Drawable using the same resource.
What solution is there, other than creating a second background image to show the pressed state? It would be nice to have a simple programmatic solution I can add to my code and use in every project.
Example that should work for you:
Drawable buttonBackground = context.getResources().getDrawable(R.drawable.bg);
buttonBackground = buttonBackground.mutate();
//Set your filter here
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.
used this:-
Drawable mDrawable = ContextCompat.getDrawable(youContext, R.drawable.youdrawable)
mDrawable.setColorFilter(PorterDuffColorFilter(youColor, PorterDuff.Mode.MULTIPLY)
button.setbackground(mDrawable)
I am new to android and try to create a simple color picker.
The idea is to have a few filled circles showing a color and the selected one should have an circle around it.
That didn't sound too hard. I draw the circles with <shape>, create two resources, one with only a filled circle, one with the filled circle and an transparent circle with a solid stroke. Then I created a <selector> and set this as the background of my <RadioButton>.
This worked fine for one color, however now I'd like to have the same styles, but with a different color for each <RadioButton>.
The only solotution I could come up with is to create this triplet of xml-files for each color. This would certainly work, but it strikes me as extremely inelegant.
I tried to access the background of the buttons, but it seems I can only access the <selector>, not it's children.
I also tried to create a FrameLayout that would parent the RadioButton and an ImageView so the RadioButton only needs to display the border. This didn't fully work. I could select a RadioButton, but it wouldn't get deselected upon selecting another one.
I guess the best solution is to have a background that can change itself according to the state of the button, just like <selector> does, but with a more comprehensive way of determining which drawable to use, at least with access to the tag-Property of the RadioButton, but I cannot see how to do this.
check this github repo
https://github.com/VishalJogiya/CustomRadioAndShapes
xml layout code
<customradio.vj.com.library.CustomRadio
android:id="#+id/radio9"
android:layout_width="#dimen/thirty_two_dp"
android:layout_height="#dimen/thirty_two_dp"
android:layout_marginBottom="#dimen/eight_dp"
android:layout_marginLeft="#dimen/sixteen_dp"
android:layout_marginRight="#dimen/sixteen_dp"
android:layout_marginTop="#dimen/eight_dp"
custom:radioColor="#AA00FF"
custom:radioShape="simple_circle2" />
I have a screen where multiple Buttons use the same background Drawable. I have reusable code I use in various projects to add an OnTouch listener that adds a gray color filter while a button is being touched. That usually works fine, but in this case ALL the buttons are tinted when any of them is pressed.
I see an explanation in http://developer.android.com/guide/topics/graphics/2d-graphics.html:
Note: Each unique resource in your project can maintain only one
state, no matter how many different objects you may instantiate for
it. For example, if you instantiate two Drawable objects from the same
image resource, then change a property (such as the alpha) for one of
the Drawables, then it will also affect the other.
The suggested solution is to use a TweenAnimation, which does not seem to work with color filters.
I also saw Android: Cloning a drawable in order to make a StateListDrawable with filters which suggests using drawable.getConstantState().newDrawable(). This does not seem to make a difference. I'm guessing that as long as the same physical image file is used, all Drawables will be affected by a change to any other Drawable using the same resource.
What solution is there, other than creating a second background image to show the pressed state? It would be nice to have a simple programmatic solution I can add to my code and use in every project.
Example that should work for you:
Drawable buttonBackground = context.getResources().getDrawable(R.drawable.bg);
buttonBackground = buttonBackground.mutate();
//Set your filter here