Android TabWidget set background color error - android

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.

Related

Android - imageView.background.setTint() changes color of background drawable throughout the application

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.

Change color of drawable in PagerTabStrip

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);

Custom RadioButtons with different color

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" />

Changing the color of the spinner

In this picture you can see (barely lol) that the spinner icons that rest on the bottom and bottom right are very hard to see due to the black background that I am using. What would I need to do to make that greyish looking color to white or any color for that matter?
Try this tutorial:
Android Custom Spinners
A similar process is used for working with custom buttons and other elements.
Updated as the link is broken. Try this stackoverflow answer
Spinner Background Design

Coloring Default Buttons - color filter only on unfocused state

I want to buttons of different colors, but I want to do so while using
the default button background resource in order to preserve the
onfocus and onclick states. This is because I want to use the default
highlight color of the OS for my app, which is NOT always orange (HTC
Sense makes it green).
I found that adding a color filter to the button's background drawable
works great (in this case, blue):
myButton.getBackground().setColorFilter(Color.parseColor(this.getString
(R.color.button_blue)), Mode.MULTIPLY);
BUT, when the button is focused or clicked, it turns a nasty
orange_blue because it mixes the color filter with the orange of the
background drawable.
I want to ONLY set this color filter for the unfocused/unclicked nine-
patch drawable within the default button's statelistdrawable.
I'm not sure how else to do this.
I see a similar solution here:
Fixed: "Android: Detecting focus/pressed color"...
but I have some concerns with that solution, mainly what if the OS
changes the graphic of the default button? Since the normal unfocused/
unpressed graphic is now hardcoded into the app, it would break the
flow.
Maybe can someone comment on whether it would be good or bad practice
to hardcode the default graphic into the app? What are the chances of
the OS completely changing the graphic?
Any help please? Thanks very much!!

Categories

Resources