This question already has answers here:
Changing the background color of a Tab in TabLayout (Android design support library) doesn't occupy the entire tab space
(4 answers)
Closed 5 years ago.
I am using tabLayout to use tabs in my app. I want to change the background color & text color of selected tab. I am changing the background color of entire tabLayout by doing this
android:background="#color/colorAccent"
and changing textcolor and selectedtextcolor
app:tabTextColor="#000000"
app:tabSelectedTextColor="#color/colorAccent"
but all i want to do now is to change the background color of that specific tab when it is selected ? How to do that ?
Thanks in advance :)
you can do it like this in your tab layout widget
<android.support.design.widget.TabLayout
app:tabBackground="#drawable/selector"
/>
and define your selector.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/tab_selected_color"
android:state_selected="true"/>
<item
android:drawable="#color/tab_unselected_color"
android:state_selected="false"/>
</selector>
Change the background colour of tab in TabLayout is fairly simple
using the design support library that Android provides. You can simply
change the background of the whole TabLayout using the
app:tabBackground property and you can change the tab indicator colour
using the app:tabIndicatorColor property, but there are better ways if
you want more functionality. A better way to change the tab-layout
colour is using selectors, using selectors you can have different
background for different sates of tab i.e selected, unselected etc.
Please follow the below steps:
1. Create a drawable, tab_selected_background, that will be use as the background for the selected tab
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/colorPrimary" />
<corners android:radius="4dp" />
</shape>
2. Create a selector, tab_selector that will be used as the background for tab layout:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/tab_selected_background" android:state_selected="true"/>
<item android:drawable="#color/tab_background_unselected"/>
</selector>
3. Now finally create the tab layout and use the selector that we've just create as the background of the tabLayout.
<android.support.design.widget.TabLayout
android:id="#+id/subChordTabs"
android:layout_width="match_parent"
android:layout_height="56dp"
app:tabGravity="center"
app:tabMode="scrollable"
app:tabBackground="#drawable/tab_selector"
app:tabIndicatorColor="#color/tabIndicator"
android:padding="8dp"
app:tabIndicatorHeight="2dp"/>
You gotta the result like below,
Related
How do I change the color of the selected tab? I would like to each tab to have its own color attribute. So tab 1 selected would be Red. Tab 2 selected would be Blue. Tab 3 selected is Yellow. When not selected they go back to the tab original color.
Currently, I am using a selector to change the background of the selected tab. However, that only allows for one color. I would like to have multiple colors
This is the unselected_tab.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/colorPrimaryDark" />
</shape>
This is the selected_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/tabSelectedColor" />
</shape>
This is the selector I am using
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="50"
android:exitFadeDuration="50" >
<item android:drawable="#drawable/tab_selected"
android:state_selected="false" />
<item android:drawable="#drawable/tab_unselected"
android:state_selected="true"/>
</selector>
And I am applying it to the tablayout background
<com.google.android.material.tabs.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
android:elevation="5dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabIndicatorColor="#android:color/white"
app:tabMode="scrollable"
app:tabMaxWidth="100dp"
app:tabBackground="#drawable/tab_selector"
/>
Below here are some screen shots of how it looks currently.
Ideally, I would like for each tab to have its own separate color.
How can accomplish this?
EDIT: IDEALLY I WOULD LIKE TO CHANGE EACH TAB COLOR PROGRAMATICALLY
For anyone who is looking for a solution to this. I was able to find one. You can apply the color to the layout of the child of the tabLayout. For some reference code this is what I used a worked really well for me
final LinearLayout tabsContainerLayout = (LinearLayout)expenseTabs.getChildAt(0);
Linear LayouttempLinearLayout = (LinearLayout)tabsContainerLayout.getChildAt(selectedTabPosition);
tempLinearLayout.setBackgroundColor(Color.RED);
I have a question which I can't find an answer to and really have to solve.
I am trying to change the background for the tab layout indicator but JUST FOR IT, i.e. if the entire tablayout is green and the tablayoutindicator height is 4dp I want the ENTIRE bottom 4dp of the tablayoutview will be in another color (lets say red) is that possible?
Before you answer please note I'm not talking about app:tabIndicatorColor property but on the entire height of the tabIndicator (regardless of the tabIndicator itself).
For example, on the attached image the app:tabIndicatorColor is white but the background of it is black while the entire view is a gardient of blue.
Edit -
I managed to do it using a FrameLayout having the bottom view in black while the tablayout is above it with its properties, I'm still looking for a more "clean" way to achieve the same thing
To achieve what you are looking for you can try the following
Create a selector tab_layout_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:state_pressed="false">
<layer-list>
<!-- Stripe bottom indicator color -->
<item android:top="-5dp" android:left="-5dp" android:right="-5dp">
<shape android:shape="rectangle">
<stroke android:color="#color/red" android:width="4dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
Finally, in your TabLayout add
<android.support.design.widget.TabLayout
<!-- Whole tab background -->
android:background="#color/white"
<!-- Line color -->
android:tabBackground="#drawable/tab_layout_selector"
.... Others....
/>
I want to implement colored BottomNavigationView with elevation (current design lib version 25.2.0, test device 7.1.2). Some code for start:
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:elevation="8dp"
app:elevation="8dp"
app:menu="#menu/bottom_navigation" />
elevation (shadow) is not drawn, because there is no background. and there is known issue that this background must be white... ok, so I'm adding this line
android:background="#android:color/white"
but I wanted to set some color for background and white icons and text... with below lines
app:itemIconTint="#color/white"
app:itemTextColor="#color/white"
whole bar is solid white, including content, obviusly... so if background must be white also then lets change every item background with
app:itemBackground="#drawable/bottom_navigation_item_background"
and for drawable/
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/mycolor_dark"/>
<item android:drawable="#color/mycolor"/>
</selector>
for drawable-v21/ (ripple)
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#android:color/white">
<item android:drawable="#color/mycolor"/>
</ripple>
and now we have colored items and... no elevation again! and it will work when mycolor will be white (elevation shown/drawn)... so when I want elevation for BottomNavigationView both background and itemBackground must be white...
interesting part (proof of issue?) - lets check elevation with only this selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#android:color/white"/>
<item android:drawable="#color/mycolor"/>
</selector>
bar is drawn without elevation at all, but when I press any menu item then its background change to white and... there is elevation above this item, ONLY this item!
when I remove <item android:drawable="#color/mycolor"/> line from drawable-v21/ XML file (or set mycolor to white), then elevation is shown. ripple color have no impact, it might be white or any other (working).
the question is: how to style BottomNavigationView with custom color for background keeping elevation working?
I used app:theme="#style/AppTheme.PopupOverlay" in order to use my application colors. They are automatically set to them.
For example, if your primary color is green, icons and text will be displayed in green.
You need to keep white background and elevation...
I want to have a tablayout, in which the current tab would have a purple background and white text while the unselected would have white background and purple background. It is pretty simple to implement using a linear layout and textviews, and setting their background when onPageSelect is called.
I was looking for a way to shift the background when the viewpager is moving. Like this
For this I have two linearlayouts, one containing the tabs and one the background.I was moving the background using a empty view using a onPageChangeListener(). But I am not able to change the textColor in between the transition states.
How should I do it?
You can use the selector in drawable.Selector look like the below
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/your color" android:state_selected="true"/>
<item android:drawable="#color/your color"/>
</selector
and add selector to the background of_tablayout.
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="#color/circular_progress_default_progress"
app:tabMode="fixed"
app:tabMaxWidth="0dp"
app:tabBackground="#drawable/tab_selector_color"
app:tabGravity="fill"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
I have to create a custom compound button bar control in Android like the one here. To create a button bar like this, I am thinking of extending a LinearLayout and add my buttons to it. The buttons have a black background but with a gradient fill; I am not keen on using PNGs since the color of the buttons can change at runtime.
How can I get similar effect in Android?
Thanks.
u have to use imageview as button.
set two image view you can change color the button
use xml file to src of that imageview like
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="#drawable/back_normal" />
<item android:state_pressed="true"
android:drawable="#drawable/back_pressed" />
</selector>
for gredient style.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90" android:startColor="#663300"
android:centerColor="#330000"
android:endColor="#330000"/>
</shape>
thats it.