I'm using PagerTabStrip with a ViewPager to go between the different views of my app and it works well. I use drawables as the PageTitles with this method:
https://stackoverflow.com/a/12837635/7459644
This also works really well, I do however want to change the colors of the drawables when they are selected, is there a way to do this? I use a onPageListener, so I do have a callback when a certain page is selected, I simply don't know how to change the color of the given Page-title drawable when that page is selected. For text there is a build-in method, but I can't find any information regarding drawables in the official documentation.
As #rupinderjeet stated in the comments, I solved it by keeping the drawable in an array before adding them to the PagerTabStrip. Since I had a reference to the drawables, I simply added a method that changes the colors of the drawables and call it from my onPageListener from my ViewPager. Works like a charm!
drawable.setColorFilter(0xffff0000, PorterDuff.Mode.SRC_ATOP);
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 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've created my own custom preference objects that extend Preference. I've only created them because there are no Preferences for these custom data types.
Everything is working, but my custom preferences don't have the same appearance because they are missing the horizontal divider that the system preference objects have. I've looked for the code that is creating the horizontal divider, but I can't find where it is done.
The built in divider isn't just a thin bar. There is a bit of a gradient to it. I'm thinking that this may be in a layout file, but I can't find the layouts for the system preferences.
Anybody got an idea how this is implemented?
Very old post, but to those who stumble upon this. Wasn't sure if the OP was asking how to change the divider or where the divider images come from. So I'll address both.
How
Preferences uses a ListView for populating all the individual preferences. That means you can change the divider by using the .setDivider() method from ListView. A PreferenceActivity will already have the getListView() method for you. However for PreferenceFragments just use the android.R.id.list ID to find it.
If you don't want to change the divider through code, you can always use a theme by overriding the listDivider attribute. Eg:
<item name="android:listDivider">#drawable/custom_divider</item>
Note, that will change the divider for EVERY ListView in your app.
Where
The listDivider drawable used depends on what Android theme is activated. You'll find all these images in the installed Android SDK at this location:
[Android SDK]/platforms/[API]/data/res/drawable-[DPI]/
Just do a search for `*divider_horizontal*`, and you'll turn up quite a few. They are nine-patched and not all of them are solid colors.
I have been trying to apply a style to my TabWidget.
I've tried several methods such as
for(int i=0;i < tabHost.getTabWidget().getChildCount();i++) tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#7392B5"));
from
How do I change the background of an Android tab widget?
However, this code does not work for me. I've also tried messing with some XML styles. The closest I have gotten is to set the Divider, however that makes the entire tab widget turn one solid color and the Tabs are no longer drawn on top.
Please help. Mark's books only touch on setting the Icons for the tabs, not changing the color. I feel this should be simple, but TabWidgets and Hosts make everything harder.
I've tried this code targeting both the 1.6 and 2.2 platforms, but neither API works.
Thanks
The background of a tab is actually a NinePatch image, set into a StateListDrawable. When you call setBackgroundColor(), you're replacing the set StateListDrawable with a simple color, so the entire tab turns into that color. What you'll need to do is actually modify (or draw your own) NinePatch tab images that are the color and style that you want for each state (e.g. focused, pressed, etc.).
Alternately, in code you could set a ColorFilter as described here (getBackground() will work for a TabWidget as well as a button) but I'd recommend going the NinePatch route personally.