Change background color in custom Tab programmatically - android

I'm trying to create a custom Tabs. I implemented the indicator color with 9.png with Android Action Bar Style Generator. But now I want to change the background color for each Tab programmatically. How I can do? Thank you.
JAVA
TabWidget tw = (TabWidget)tabs.findViewById(android.R.id.tabs);
View tabView = tw.getChildTabViewAt(0);
tabView.setBackgroundResource(R.drawable.tab_indicator_blanco);
View tabView2 = tw.getChildTabViewAt(1);
tabView2.setBackgroundResource(R.drawable.tab_indicator_blanco);
tab_indicator_blanco.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected_blanco" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected_blanco" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected_focused_blanco" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected_focused_blanco" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_unselected_pressed_blanco" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_blanco" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_unselected_pressed_blanco" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_blanco" />
</selector>

Related

Android Button change style on click

How create buttons with underline light when is clicked.
like tabs but i need only for buttons.
Checkout these tutorial.You can use TabHost
http://www.learn-android-easily.com/2013/07/android-tabwidget-example.html
http://www.androidhive.info/2011/08/android-tab-layout-tutorial/
You need to use a custom Selector (similar to Tabs Selector) as background to your Button.
<Button
android:id="#+id/button"
android:background="#drawable/button_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My button" />
Your button_background.xml :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected_holo" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected_focused_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected_focused_holo" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_unselected_pressed_holo" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_unselected_pressed_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_holo" />
</selector>
tab_unselected_holo
tab_selected_holo
tab_unselected_focused_holo
tab_selected_focused_holo
tab_unselected_pressed_holo
tab_selected_pressed_holo
PS: I get this example from here.

Android actionbar tabs and indicator width issue

I have an activity with action bar and action bar tabs. I am using appcompat for action bar. I have attached the screen shots. on emulator 4.4 and htc 4.1.1 tabs widths id correct but on nexus 4, 4.4 it adds some margin due to which it looks ugly.
Edit
how i am adding tabs.
ActionBar ab = getSupportActionBar();
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ab.setTitle("Purchase media");
ab.setSubtitle("To share with members");
ab.addTab(ab.newTab().setText("Audios").setTabListener(this));
ab.addTab(ab.newTab().setText("Videos").setTabListener(this));
my style for tabs is as follow
<style name="ActionBarTabStyle.Example" parent="#style/Widget.AppCompat.ActionBar.TabView">
<item name="android:background">#drawable/tab_indicator_ab_example</item>
</style>
and tab indicator selector is as follow
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected_example" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected_focused_example" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected_focused_example" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_unselected_pressed_example" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_example" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_unselected_pressed_example" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_example" />
how it looks.
how it should be
anybody have any idea about this behavior?

Change the tabhost style android

I want to change the default blue color of the tabhost to red.
<style name="AppTheme" parent="android:Theme.Light.NoTitleBar">
<item name="android:tabWidgetStyle">#drawable/tab_indicator_holo</item>
</style>
tab_indicator_holo.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected_holo" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected_focused_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected_focused_holo" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_unselected_pressed_holo" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_unselected_pressed_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_holo" />
</selector>
But the tab style is not applied to the tabhost. The default blue color is not changed to red.
I am getting this
Any ideas or suggestions please.
You might have already found the answer, but for those who may face the same problem, Here is what I have done.
go to custom holo theme and set tabwedget to yes and choose preferred color.
Download the zip, copy over to my project.
Add to tabadapter with the view created by inflating tab_indicator_holo.
View mIndicator = inflater.inflate(R.layout.tab_indicator_holo, mTabHost.getTabWidget(), false);
TextView title1 = (TextView) mIndicator.findViewById(android.R.id.title);
title1.setText("TAB1");
mTabsAdapter.addTab(mTabHost.newTabSpec("TAB1").setIndicator( mIndicator), FRAGMENT1.class, null);
View mIndicator2 = inflater.inflate(R.layout.tab_indicator_holo, mTabHost.getTabWidget(), false);
TextView title2 = (TextView) mIndicator2.findViewById(android.R.id.title);
title2.setText("TAB2");
mTabsAdapter.addTab(mTabHost.newTabSpec("TAB2").setIndicator(mIndicator2), FRAGMENT2.class, null);

Action Bar Sherlock, change colour of line under tab?

How can I change the colour of the line that appears under the highlighted Action Bar Sherlocks tab.
It's default is an electric blue.
Here an implementation based on the google i/o app implementation:
Set the theme style:
<style name="Theme.Styled" parent="Theme.Sherlock.Light">
<item name="android:actionBarTabBarStyle">#style/Widget.Styled.TabBar</item>
<item name="actionBarTabBarStyle">#style/Widget.Styled.TabBar</item>
</style>
<style name="Widget.Styled.TabView" parent="Widget.Sherlock.Light.ActionBar.TabView">
<item name="android:background">#drawable/tab_white_ab</item>
</style>
Create a drawable like this - notice that in the google I/O app the non focused state is transparent - in the example below I changed transparent to green:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/tab_green_unselected"
/>
<!-- android:drawable="#android:color/transparent"-->
<item android:state_focused="false"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/tab_white_selected" />
<!-- Focused states -->
<item android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/item_focused" />
<item android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/tab_white_selected_focused" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false"
android:state_selected="false"
android:state_pressed="true"
android:drawable="#drawable/item_pressed" />
<item android:state_focused="false"
android:state_selected="true"
android:state_pressed="true"
android:drawable="#drawable/tab_white_selected_pressed" />
<!-- Focused states -->
<item android:state_focused="true"
android:state_selected="false"
android:state_pressed="true"
android:drawable="#drawable/item_focused" />
<item android:state_focused="true"
android:state_selected="true"
android:state_pressed="true"
android:drawable="#drawable/tab_white_selected_pressed" />
Hope this helps.

How to change the colour of ActionBar navigation tabs?

I have an ActionBar in an app, and it has navigation tabs embedded in it (not TabHost!). By default the tabs show as dark grey, with a thin blue line under all the tabs, and a blue marker on the selected tab.
Which styles do I override to change those colours?
I have not changed the tabs themselves, but I would assume that you can do it with these styles from styles.xml...
<style name="Widget.Holo.TabWidget" parent="Widget.TabWidget">
<item name="android:tabStripLeft">#null</item>
<item name="android:tabStripRight">#null</item>
<item name="android:tabStripEnabled">false</item>
<item name="android:divider">?android:attr/dividerVertical</item>
<item name="android:showDividers">middle</item>
<item name="android:dividerPadding">8dip</item>
<item name="android:measureWithLargestChild">true</item>
<item name="android:tabLayout">#android:layout/tab_indicator_holo</item>
</style>
with tab_indicator_holo.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected_holo" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected_focused_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected_focused_holo" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_unselected_pressed_holo" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_unselected_pressed_focused_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_focused_holo" />
</selector>
Or you may also try
<style name="Widget.Holo.ActionBar.TabView" parent="Widget.ActionBar.TabView">
<item name="android:background">#drawable/tab_indicator_ab_holo</item>
<item name="android:paddingLeft">16dip</item>
<item name="android:paddingRight">16dip</item>
</style>
and tab_indicator_ab_holo.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#color/transparent" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/list_focused_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected_focused_holo" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/list_pressed_holo_dark" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_unselected_pressed_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_selected_pressed_holo" />
</selector>
Finally using the two png-9 drawables: tab_selected_holo and tab_unselected_holo. They look like the two thicker and thinner blue lines you are talking about.
Or do you mean the minitabs?
<style name="Widget.ActionBar.TabView" parent="Widget">
<item name="android:gravity">center_horizontal</item>
<item name="android:background">#drawable/minitab_lt</item>
<item name="android:paddingLeft">4dip</item>
<item name="android:paddingRight">4dip</item>
</style>
with in minitab_lt.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_selected="true"
android:drawable="#drawable/minitab_lt_press" />
<item android:state_selected="true"
android:drawable="#drawable/minitab_lt_selected" />
<item android:state_pressed="true"
android:drawable="#drawable/minitab_lt_unselected_press" />
<item android:drawable="#drawable/minitab_lt_unselected" />
</selector>
If you need another definition just search for TabWidget in here: https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml
Then as usual define your own style with all the required attributes and drawables...
If you want to customize easily your tab bars, you can use this great tool :
http://jgilfelt.github.io/android-actionbarstylegenerator
You just select the colors you want and it automatically generates the style XMLs, the PNGs, etc.

Categories

Resources