Set color tab host indicator - android

How do you set the color of the tab host indicator color I want to change the light blue default color to lets says RED.
And I need to this programatically as I am making the tabs programatically.
I did some research and looked at this example but it does not work for me. With the progamtic approach.
TabWidget current tab bottom line color
Thanks

You can do this programmatically, even change the color as you want, by following the solution in the linked question you mention, plus adding a ColorFilter to adjust the color.
So:
Create the appropriate drawable. The easiest way, as mentioned in one of the answers, is using https://jgilfelt.github.io/android-actionbarstylegenerator/
Place into your project the tab_indicator_ab_example.xml (in drawable) plus the 6 associated png files (tab_*.png) for each drawable density.
After creating the tabs, use the code that iterates over the TabWidget child views to set their background, however:
Instead of setting the drawable as-is, use a color filter to change its color to the desired one.
Instead of this code:
for(int i = 0; i < widget.getChildCount(); i++) {
... /* same as before */
v.setBackgroundResource(R.drawable.your_tab_selector_drawable);
}
write something like this:
for(int i = 0; i < widget.getChildCount(); i++) {
... /* same as before */
Drawable d = getResources().getDrawable(R.drawable.tab_indicator_ab_example);
d.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN);
v.setBackgroundDrawable(d);
}

Related

button dynamic background color with rounded edges

I managed to have rounded edge for my button.
I also managed to have a dynamic background color (taken from a webservice).
The problem is when doing this :
btn.setBackgroundResource(R.drawable.radio_button_selector);
btn.setBackgroundColor(Color.parseColor(currentQuestion.backgroundColorButton));
One overrides the other, therefore I cannot have rounded edges AND dynamic background color.
I cannot use a dynamic color in the selector (as it's a static XML).
I cannot set the rounded edges programmatically (the method doesn't exists as far as I know).
How do I do ?
Use this
String backgroundColor= "#fc0000"; // set dynamic color here
btn.setBackgroundColor(Color.parseColor(backgroundColor));
another Examples:
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(5, Color.MAGENTA);
drawable.setColor(Color.BLACK);
btnBlackColor.setBackgroundDrawable(drawable);
use this :-
final int color = Color.parseColor(homeCatPOJOS.get(position).getColor());
then implement it in background color :-
btn.setBackgroundResource(R.drawable.radio_button_selector);
btn.setBackgroundColor(color));
I think you are getting color code in String, first convert into int then implement it.
And for round edges make XML file for it and implement it statically.

Color set by setColorFilter disappears on prelollipop devices

I have a TabLayout which has icons. The idea is to change the colors runtime.
I have and xml drawable file with states: state_pressed, state_selected and default with the same white picture so I can put color later.
I take the drawables for different states:
Drawable[] drawables = stateListDrawable.getConstantState();
and the for every drawable state I put color from another array:
drawables[i].setColorFilter(colors[i], PorterDuff.Mode.MULTIPLY);
The issue is that the color is visible in the beginning, but when I start to click on the icons all the icons becomes white again and I lose the tint.
Everything is working as expected on lollipop and above.
Use the tint method from the v4 Support library
drawables[i] = DrawableCompat.wrap(drawables[i])
DrawableCompat.setTint(drawables[i], colors[i])
I have found my solution, which does not look clean at all, but at least it is working :)
I have created CustomStateListDrawable which extends from StateListDrawable and added the drawables for the different states. Then I have overridden all the methods in the class to see which ones are called and tried to change the colors there. The called late enough(my changes will not be overridden after I make them) was getState(). I have created also a ColorStateList object to hold my colors so the code will look like this:
private ColorStateList colorStateList;
public int[] getState() {
if (colorStateList != null) {
// Resolve the color for the current state
int color = colorStateList.getColorForState(super.getState(), 0);
// Get the current drawable and changed its color.
if (getCurrent() != null) {
getCurrent().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
}
}
return super.getState();
}
Basically every time when there is a change in the state I get the current drawable and change its color.

Android: Tab Indicator line disappears when tab background is changed

My question is more or less similar to this question, but the response approved as the answer for the same doesn't solve my issue.
I'm implementing tab structures using the FragmentTabHost class. When the tab background color is not explicitly specified, the tab indicator line seems to appear as Android provides. But when I programatically change the tab's background color, the tab indicators disappear (hidden).
Currently, I assign the tab background color this way :
private void customizeTabbs() {
for (int i = 0; i < mTabHost.getTabWidget().getTabCount(); i++) {
View tabChild= mTabHost.getTabWidget().getChildAt(i);
..
..
tabChild.setBackgroundResource(someColorResource);
}
}
I also tried to change the background color in the XML way by adding to styles.xml , following this as:
<item name="tabBackground">#color/someColor</item>
<item name="tabIndicatorColor">?#color/someOtherColor</item>
The solution suggested for one similar question also did not help me.

Change Android TabWidget bottom bar color

How do I change the color of the bottom bar for TabWidget? I have successfully changed the tab background color but the bottom bar is still grey/orange and I couldn't find any info in the Android doc and source regarding this. Thanks.
See:
to enable/disable this line:
tabHost.getTabWidget().setStripEnabled(boolean);
to set drawable at left for this line:
tabHost.getTabWidget().setLeftStripDrawable(drawable);
to set resourse at left for this line
tabHost.getTabWidget().setLeftStripDrawable(resId);
to set drawable at right for this line:
tabHost.getTabWidget().setRightStripDrawable(drawable);
to set resourse at right for this line:
tabHost.getTabWidget().setRightStripDrawable(resId);
I'm guessing that "bottom bar" refers to the optional horizontal line that separates the tabs and the content. Take a look at the various tabStrip attributes described in the TabWidget API doc. You can set different drawables for the left and right parts of the strip.
public void setTabColor(TabHost tabhost) {
int totalTabs = tabhost.getTabWidget().getChildCount();
for(int i=0;i<totalTabs;i++) {
if(tabHost.getTabWidget().getChildAt(i).isSelected()){
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab_selector); //selector xml for selected
tabHost.getTabWidget().setStripEnabled(true);
tabHost.getTabWidget().setRightStripDrawable(R.drawable.tab_strip_thin);
tabHost.getTabWidget().setLeftStripDrawable(R.drawable.tab_strip_thin);
}
}
}

How to control the contrast of text?

How do you change the color contrast of text when the background changes? for example If I was to have a black background, black text would not be visible.
This might be helpful: http://developer.android.com/guide/topics/resources/color-list-resource.html
It is a way you can set a color that will change based on certain circumstances.
For example, say you have a TextView that you want to have white text while it is enabled and black text when it is disabled. You can set that up in a xml file using the references in the link above, and then in your xml layout where you define the TextView set the android:textColor to #color/my_text_color. (my_text_color being the xml color list file you created)
Then, as the TextView changes from enabled to disabled (or whatever you end up setting up in the xml file) the color will change automatically as well.
That's one way to do it. However, you might want to try to clarify what you are looking for as it isn't perfectly clear in your question.
Update
After Matt's comment, here is a method you could use to get an inverted color value. There is probably a better way but this should work.
private int getInverseColor(int color){
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
int alpha = Color.alpha(color);
return Color.argb(alpha, 255-red, 255-green, 255-blue);
}
You could programmaticly get the color int from a view such as a TextView using one of the getTextColor() methods. You may have to tinker with a Color State List like I linked to above to get the color you want. Then pass that color to the method above to get the inverted color int and set it with one of the setTextColor() methods.

Categories

Resources