i am using databinding to change the color of the selected tab of tab layout
#BindingAdapter(value = ["tabIndicatorColor", "context"])
fun setSelectedTabIndicatorColor(tabLayout: TabLayout, color: Int, context: Context) {
tabLayout.setSelectedTabIndicatorColor(getColor(context, color))
}
and setting it from the tabLayout view
<variable
name="professionalTypeColor"
type="Integer" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tl_images"
android:layout_width="0dp"
android:layout_height="5dp"
tabIndicatorColor="#{professionalTypeColor}"
context="#{context}"
app:tabPaddingEnd="8dp"
app:tabPaddingStart="8dp" />
what i did until here is exactly as i wanted it to be, but for the unselected tabs i couldn't make a databinding Adapter for it so i change its color dynamically ,
i tried using
app:tabBackground="#color/grey"
or
app:tabBackground="#drawable/selector_tab_indicator"
but this need to be predefined color or a drawable with two colors (selected,unselected) which is not my desired result,
my question is how to make a databinding adapter to set the tabBackground dynamically , ( i couldn't find a setter in tablayout with attribute of tabbackground )
Create a style in style.xml and call in xml layout like below
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#color/colorAccent</item>
<item name="tabIndicatorHeight">2dp</item>
<item name="tabTextAppearance">#style/MyCustomTabTextAppearance</item>
<item name="tabSelectedTextColor">#color/colorAccent</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">#dimen/title_text_size</item>
<item name="android:textColor">#color/secondaryText</item>
<item name="textAllCaps">false</item>
<item name="android:textStyle">normal</item>
</style>
Call here
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="#android:color/white"
app:tabMode="scrollable"
**style="#style/MyCustomTabLayout"**
app:tabGravity="fill" />
Related
I am new to Android and Kotlin. I am trying to develop an app that contains a TabLayout with 2 tabs. The titles are rather large, and no matter what I do, I cannot get things to look right. The app displays in horizontal mode only and so there is plenty of space in the top bar for the titles, but the tab titles appear to be fixed. As a result of this, the text size is too small. I have tried to change the text size, change the Styles, and even tried to increase the size of the Tab Layout, but nothing helps. Nothing seems to change the text size in the tab itself. Can someone please help me with some code to make this change?
Thank you in advance for your help!
Styles:
<style name="MyTabLayout" parent="Base.Widget.Design.TabLayout">
<item name="tabTextAppearance">#style/MyTabTextAppearance</item>
<item name="android:textSize">36sp</item>
</style>
<style name="MyTabTextAppearance">
<item name="android:textSize">36sp</item>
<item name="android:textColor">#android:color/white</item>
<item name="textAllCaps">true</item>
</style>
Tab Layout:
<com.google.android.material.tabs.TabLayout
app:tabTextAppearance="#style/MyTabTextAppearance"
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:background="?attr/colorPrimary"
android:textAlignment="center"
android:textSize="36sp"
app:tabGravity="fill" />
Try out with this style :
<style name="MyTabStyle" parent="Base.Widget.Design.TabLayout">
<item name="android:textSize">36sp</item>
<item name="android:textAllCaps">true</item>
</style>
And apply in Tablayout :
<com.google.android.material.tabs.TabLayout
app:tabTextAppearance="#style/MyTabTextAppearance"
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
app:tabTextAppearance="#style/TabLayoutStyle"
android:background="?attr/colorPrimary"
app:tabGravity="fill" />
I'm trying to customize a TextInputLayout with material style. I managed to set the focused state to the colors I want:
Using
<com.google.android.material.textfield.TextInputLayout
style="#style/LoginTextInputLayoutStyle"
android:theme="#style/LoginTextInputLayoutStyle"
android:textColorHint="#fff"
app:boxStrokeColor="#fff"
.....>
<EditText ...
where the style is:
<style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="colorAccent">#fff</item>
</style>
But when the textinput is not focused I get this look:
How can I change the color of the black line to be white too?
Use this style to apply border color and border width like this :
<style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">#fff</item>
<item name="boxStrokeWidth">2dp</item>
</style>
get Additional details about styling from this link
Add below line in your colors.xml file that overrides default color for TextInputLayout
<color name="mtrl_textinput_default_box_stroke_color" tools:override="true">#fff</color>
As of version 1.1.0-alpha02 of the Material Components for Android it works to simply create a ColorStateList for these items. The procedure is as follows:
Create a new resource directory "color" in res and inside color add a color resource file named "text_input_box_stroke.xml" res/color/text_input_box_stroke.xml put something like the following:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#fcc" android:state_focused="true"/>
<item android:color="#cfc" android:state_hovered="true"/>
<item android:color="#ccf"/>
</selector>
Then in your styles.xml you would put:
<style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">#color/text_input_box_stroke</item>
</style>
Finally indicate your style for the actual TextInputLayout:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/my_layout_id"
style="#style/LoginTextInputLayoutStyle"
...
As of Material Components Alpha 7 you simply create a color selector file as so:
colors/text_input_outline_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#color/buttonDark"/>
<item android:state_hovered="true" android:color="#color/buttonDark"/>
<item android:state_focused="true" android:color="#color/buttonDark"/>
<item android:color="#color/buttonDark"/>
</selector>
For more context into how this is being set. Here is relevant source code:
ColorStateList boxStrokeColorStateList =
MaterialResources.getColorStateList(context, a, R.styleable.TextInputLayout_boxStrokeColor);
if (boxStrokeColorStateList != null && boxStrokeColorStateList.isStateful()) {
defaultStrokeColor = boxStrokeColorStateList.getDefaultColor();
disabledColor =
boxStrokeColorStateList.getColorForState(new int[] {-android.R.attr.state_enabled}, -1);
hoveredStrokeColor =
boxStrokeColorStateList.getColorForState(new int[] {android.R.attr.state_hovered}, -1);
focusedStrokeColor =
boxStrokeColorStateList.getColorForState(new int[] {android.R.attr.state_focused}, -1);
} else {
// If attribute boxStrokeColor is not a color state list but only a single value, its value
// will be applied to the box's focus state.
focusedStrokeColor =
a.getColor(R.styleable.TextInputLayout_boxStrokeColor, Color.TRANSPARENT);
defaultStrokeColor =
ContextCompat.getColor(context, R.color.mtrl_textinput_default_box_stroke_color);
disabledColor = ContextCompat.getColor(context, R.color.mtrl_textinput_disabled_color);
hoveredStrokeColor =
ContextCompat.getColor(context, R.color.mtrl_textinput_hovered_box_stroke_color);
}
From this list you can see that you want to ensure you are using a color selector with all states defined, or it will default back to another color.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#FFFFFF"/>
<item android:state_focused="false" android:color="#FFFFFF"/></selector>
create the color directory and inside that create a resource file
paste the above code in color directory xml file
and in text input layout style paste the below line
<item name="boxStrokeColor">#color/focus_tint_list</item>
First Remove from your TextInputLayout
<item name="boxStrokeColor">#color/YourColor</item>
Second, add a new color attribute
<color name="mtrl_textinput_default_box_stroke_color" tools:override="true" >YourColor</color>
Must write the same name mtrl_textinput_default_box_stroke_color do n't change
I'm creating dynamically my screen. I'm using TextInputLayout and create my dynamic edit text in TextInputLayout. If you want to give TextInputLayout to the border, do the following steps in order.
1- include Build.gradle;
implementation 'com.google.android.material:material:1.0.0'
2- in Kotlin code;
val textInputLayout = TextInputLayout(this)
textInputLayout.apply {
boxStrokeColor = Color.parseColor("#E68A06")
setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE)
setHintTextAppearance(R.style.ValidatableInputLayoutStyle_OutlineBox_HintInputLayoutStyle)
setBoxCornerRadii(16f, 16f, 16f, 16f)
setPadding(4, 0, 0, 0)
}
3- style.xml
<style name="ValidatableInputLayoutStyle.OutlineBox.HintInputLayoutStyle" parent="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="android:textColor">#color/colorPrimary</item>
<item name="android:textSize">14sp</item>
My Component image link
Matrial Edit Text
Step 1: Add library in build.gradle(Module App) module dependency section
implementation 'com.android.support:design:28.0.0-alpha1'
Step 2 : xml Code
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:boxStrokeColor="#0000FF"
app:boxStrokeWidth="2dp"
android:layout_gravity="center"
>
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/txtusername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/lable" />
</com.google.android.material.textfield.TextInputLayout>
I created a default config.
<style name="Widget.Design.TextInputLayout" parent="AppTheme">
<item name="hintTextAppearance">#style/AppTheme.TextFloatLabelAppearance</item>
<item name="errorTextAppearance">#style/AppTheme.TextErrorAppearance</item>
<item name="counterTextAppearance">#style/TextAppearance.Design.Counter</item>
<item name="counterOverflowTextAppearance">#style/TextAppearance.Design.Counter.Overflow</item>
</style>
<style name="AppTheme.TextFloatLabelAppearance" parent="TextAppearance.Design.Hint">
<!-- Floating label appearance here -->
<item name="android:textColor">#color/colorAccent</item>
<item name="android:textSize">20sp</item>
</style>
<style name="AppTheme.TextErrorAppearance" parent="TextAppearance.Design.Error">
<!-- Error message appearance here -->
<item name="android:textColor">#ff0000</item>
<item name="android:textSize">20sp</item>
</style>
Step 1. Use 1.2.0-alpha05 or later
implementation 'com.google.android.material:material:1.2.0-alpha05'
Step 2 - Important!. Make sure your app theme is or is a descendant of Theme.MaterialComponents. See here
Once that's set all attribute setting work as expected.
Step 3. Use the attribute setting from the specification
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/filledTextField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_text"
app:helperTextEnabled="true"
app:helperText="#string/helper_text"
app:counterEnabled="true"
app:counterMaxLength="20"
app:startIconContentDescription="#string/leading_icon_content_desc"
app:startIconDrawable="#drawable/baseline_favorite_24">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
Create a Theme and override "colorOnSurface" attr.
<style name="AppTheme.LoginScreenTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorOnSurface">#FFF</item>
</style>
Apply the theme to your login activity.
<activity
android:name=".login.ui.login.LoginActivity"
android:label="#string/title_activity_login"
android:launchMode="singleInstance"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.LoginScreenTheme"
android:windowSoftInputMode="adjustResize|stateHidden"/>
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
app:boxStrokeColor="#fff"
android:textColorHint="#fff"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
***app:boxStrokeColor="#fff"***
android:inputType="textPassword"
android:textColor="#fff"
/>
</com.google.android.material.textfield.TextInputLayout>
Step 1. add this line to your colors.xml file
<color name="mtrl_textinput_default_box_stroke_color">#4169E1</color>
Step 2. add this property in TextInputLayout
app:boxStrokeColor="#color/mtrl_textinput_default_box_stroke_color"
Summary
Steps to follow
1. Create ColorStateList for boxStrokeColor
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorSurface" android:state_focused="true" />
<item android:alpha="0.87" android:color="?attr/colorSurface" android:state_hovered="true" />
<item android:alpha="0.12" android:color="?attr/colorSurface" android:state_enabled="false" />
<item android:alpha="0.38" android:color="?attr/colorSurface" />
</selector>
2. Create ColorStateList for android:textColorHint
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.38" android:color="?attr/colorSurface" android:state_enabled="false" />
<item android:alpha="0.6" android:color="?attr/colorSurface" />
</selector>
3. Set view attributes - There are 3 ways that you can do this.
I. Using attribute set
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:textColorHint="#color/text_color_hint"
app:boxStrokeColor="#color/box_stroke_color"
app:hintTextColor="?attr/colorSurface">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
II. Using explicit style
Define custom style
<style name="CustomTextInputStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxStrokeColor">#color/box_stroke_color</item>
<item name="hintTextColor">?attr/colorSurface</item>
<item name="android:textColorHint">#color/text_color_hint</item>
</style>
Set style
<com.google.android.material.textfield.TextInputLayout
style="#style/CustomTextInputStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
III. Using default style attribute - set global style for TextInputLayout
<!-- Base application theme. -->
<style name="Theme.TestApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
...
<!-- Status bar color. -->
...
<!-- Customize your theme here. -->
<item name="textInputStyle">#style/CustomTextInputStyle</item>
</style>
Explanation
There are various ways to change TextInputLayout box stroke color and hint text color.
The responsible attribute for box outline color is boxStrokeColor.
First let's create ColorStateList in xml format. Create Android color resource directory and create new resource file named box_stroke_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorSurface" android:state_focused="true" />
<item android:alpha="0.87" android:color="?attr/colorSurface" android:state_hovered="true" />
<item android:alpha="0.12" android:color="?attr/colorSurface" android:state_enabled="false" />
<item android:alpha="0.38" android:color="?attr/colorSurface" />
</selector>
I referred resources of material design library. See how this is done in material design library https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/textfield/res/color/mtrl_outlined_stroke_color.xml
To change hint text color, we have to set two attributes,
hintTextColor (The color of the label when it is collapsed and the text field is active)
android:textColorHint (The color of the label in all other text field states, such as resting and disabled)
How do I know which attributes are required to change? I checked attributes defined in theme Widget.MaterialComponents.TextInputLayout.OutlinedBox. Look at parent theme if not defined in the child theme. https://github.com/material-components/material-components-android/blob/788866e4483621e2222f649e617ee95f7aa9caa6/lib/java/com/google/android/material/textfield/res/values/styles.xml#L88 (This may vary in master branch)
app:hintTextColor="?attr/colorSurface"
Note that hintTextColor is not stateful.
But android:textColorHint is stateful.
Let's create custom ColorStateList for android:textColorHint.
Referred this https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/textfield/res/color/mtrl_indicator_text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.38" android:color="?attr/colorSurface" android:state_enabled="false" />
<item android:alpha="0.6" android:color="?attr/colorSurface" />
</selector>
Note that I have used ?attr/colorSurface, because generally it's value is white in light theme and black in dark theme. You could use #android:color/white directly if you don't want dynamic color adjustments.
There are several ways that you can set box stroke color attribute value,
Attribute values are resolved using context.theme.obtainStyledAttributes().
If value is defined in multiple places, the following order determines which attribute value is ultimately applied.
AttributeSet - Value defined in layout xml file.
Style - Value defined in explicit style. (Retrieve it through theme.getExplicitStyle(AttributeSet))
defStyleAttr - Default style attribute, which is the third argument of view class constructor.
defStyleRes - Default style resource, which is the fourth argument of view class constructor.
Theme - If not defined in all of the above, theme attribute value is resolved.
Let's see them one by one
Using AttributeSet
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:textColorHint="#color/text_color_hint"
app:boxStrokeColor="#color/box_stroke_color"
app:hintTextColor="?attr/colorSurface">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
Using explicit style
Define custom style in styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTextInputStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxStrokeColor">#color/box_stroke_color</item>
<item name="hintTextColor">?attr/colorSurface</item>
<item name="android:textColorHint">#color/text_color_hint</item>
</style>
</resources>
Define explicit style for the widget
<com.google.android.material.textfield.TextInputLayout
style="#style/CustomTextInputStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
Using default style attribute - This is used to define global style for a view. How do you know which attribute is required to set? Just check default value of third argument of any view constructor. For material TextInputLayout this value is com.google.android.material.R.attr.textInputStyle.
<!-- Base application theme. -->
<style name="Theme.TestApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
...
<!-- Status bar color. -->
...
<!-- Customize your theme here. -->
<item name="textInputStyle">#style/CustomTextInputStyle</item>
</style>
Using default style resource - This is only applicable when creating widgets programmatically. If you check fourth argument of the constructor of any view except material design views, you can see defStyleRes parameter. If theme.obtainStyledAttributes() cannot resolve from the above ways, then it looks for attribute in default style resource. This is not applicable in material design library widgets, because this value is hard coded in those widgets and not exposed to change programmatically. (It is internally applied through theme.applyStyle())
Using app theme - This is not possible in material design widgets, because defStyleRes is hard coded in material design widgets and it takes precedence over app theme attributes.
<!-- Customize your theme here. -->
<item name="boxStrokeColor">#color/box_stroke_color</item>
<item name="hintTextColor">?attr/colorSurface</item>
<item name="android:textColorHint">#color/text_color_hint</item>
Above is only applicable in Android Sdk provided widgets.
---> First customize style
<style name="Widget.TextInputLayout.FilledBox" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
<item name="boxStrokeColor">?attr/colorSecondary</item>
<item name="hintTextColor">?attr/colorSecondary</item>
</style>
Second If you want to this style for all TextinputLayout in whole app.
so add this style to your parent theme
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryVariant">#color/colorPrimaryDark</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorSecondary">#color/colorSecondary</item>
<item name="colorSecondaryVariant">#color/colorSecondaryVariant</item>
<item name="android:textColorPrimary">#color/textColorPrimary</item>
<item name="textInputStyle">#style/Widget.TextInputLayout.FilledBox</item>
</style>
If you just want to add only for this particular input field
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.TextInputLayout.FilledBox"
.....>
<style name="VerifyTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">#color/colorWhite</item>
<item name="boxStrokeWidth">2dp</item>
<item name="colorOnSurface">#color/colorPrimary</item>
<item name="colorPrimary">#color/colorWhite</item>
</style>
For the border color:
app:boxStrokeColor="#color/gray" //for border color
For the hint color:
app:hintTextColor="#color/puce" //for hint color
This worked for me:
<com.google.android.material.textfield.TextInputLayout
android:textAppearance="?attr/textAppearanceBody2"
style="#style/LoginTextInputLayoutStyle"
android:theme="#style/LoginTextInputLayoutStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/txtUserName"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textColorHighlight="#FFFFFF"
android:shadowColor="#FFFFFF"
android:outlineAmbientShadowColor="#FFFFFF"
android:outlineSpotShadowColor="#FFFFFF"
android:hint="Email"
android:textSize="#dimen/textxhdpi"/>
</com.google.android.material.textfield.TextInputLayout>
In the Styles:
<resources
xmlns:app="http://schemas.android.com/apk/res-auto">
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<item name="textAppearanceBody2">#style/TextAppearance.neurosales.Body2</item>
</style>
<style name="TextAppearance.neurosales.Body2" parent="TextAppearance.MaterialComponents.Body2">
<item name="colorAccent">#color/white</item>
<item name="android:textColorHint">#color/white</item>
<item name="colorControlNormal">#color/white</item>
<item name="colorControlActivated">#color/white</item>
<item name="colorControlHighlight">#color/white</item>
<item name="android:textColor">#color/white</item>
</style>
<style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">#fff</item>
<item name="android:textColorHint">#fff</item>
<item name="app:hintTextColor">#fff</item>
</style>
In Color:
<color name="mtrl_textinput_default_box_stroke_color" tools:override="true">#fff</color>
I would like to know that is it possible to change selected tab colour in new design tablayout? I found the solution for selected tab text color but I would like to know to change tab colour itself.
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
style="#style/TabLayout.Theme"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#+id/tabs"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</RelativeLayout>
<style name="TabLayout.Theme" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#color/black</item>
<item name="tabIndicatorHeight">4dp</item>
<item name="tabTextAppearance">#style/TextAppearance.Jacksonville.Tab</item>
<item name="tabSelectedTextColor">#color/text_dim</item>
<item name="tabBackground">#color/color_heading</item>
</style>
i need to change selected tab colour like this.
you just need to set app:tabBackground attribute
app:tabBackground="#drawable/tab_selector_color"
and create a drawable file as tab_selector_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/tab_selected" android:state_selected="true"/>
<item android:drawable="#color/tab_unselected"/>
</selector>
so The complete xml code will be look like
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="android:attr/listPreferredItemHeight"
android:minWidth="0dp"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabTextColor="#color/white"
app:tabIndicatorHeight="0dp"
app:tabSelectedTextColor="#color/white"
app:tabIndicatorColor="#color/mainBlue"
app:tabBackground="#drawable/tab_color_selector"/>
hi you can make the hight of the indicator to equal the height of the tablayout so the indicator will cover the all size of the selected item in tab layout
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<!-- the color you want in selected tab -->
<item name="tabIndicatorColor">#50000000</item>
<item name="tabTextAppearance">#style/MyCustomTabTextAppearance</item>
<!-- set the indicator hieght equal to tablayout height -->
<item name="tabIndicatorHeight">60dp</item>
<item name="tabSelectedTextColor">#222222</item>
I have two question with TabLayout
1)Can i remove TabLayout highlight or change highlight color of tab layout?
2)Can i add ripple effect for tab. Each tab contain TextView i try to add custom background something like this
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:drawable="#drawable/btn_white_bg" />
</ripple>
but it doesn't work.
To remove the highlight, add the below line to your XML:
app:tabRippleColor="#android:color/transparent"
Another solution that works for me
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
app:tabMode="fixed"
app:tabGravity="fill"
android:clipToPadding="false"
android:elevation="0dp"
style="#style/MyCustomTabLayout"
android:background="#color/colorPrimary"
app:tabBackground="?attr/selectableItemBackground"
app:tabIndicatorColor="#color/app_yellow"
app:tabIndicatorHeight="4dip"
android:layout_height="wrap_content"/>
I just added following lines
android:background="#color/colorPrimary"
app:tabBackground="?attr/selectableItemBackground"
You can customize the TabLayout like this:
Make a xml file inside values MyCustomTabLayout.xml and then put these
<resources>
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabMaxWidth">#dimen/tab_max_width</item>
<item name="tabIndicatorColor">#color/black</item>
<!-- <item name="tabIndicatorColor">?attr/colorAccent</item> -->
<item name="tabIndicatorHeight">5dp</item>
<item name="tabPaddingStart">12dp</item>
<item name="tabPaddingEnd">12dp</item>
<item name="tabBackground">?attr/selectableItemBackground</item>
<item name="tabTextAppearance">#style/MyCustomTabTextAppearance</item>
<item name="tabSelectedTextColor">?android:textColorPrimary</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">16sp</item>
<item name="android:textColor">?android:textColorSecondary</item>
<item name="textAllCaps">true</item>
</style>
and inside ur layout add this:
<android.support.design.widget.TabLayout
android:id="#+id/mainSlidingTab"
style="#style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tool_bar"
android:background="#color/ColorPrimary" />
<!-- app:tabMode="scrollable" when many tabs -->
Alternatively, you can make the ripple transparent programmatically:
val tabs = findViewById<TabLayout>(R.id.your_tablayout)
for (n in 0 until tabs.tabCount) {
val tab = (tabs.getChildAt(0) as ViewGroup).getChildAt(n)
tab?.let {
val ripple = it.background as? RippleDrawable
ripple?.setColor(ColorStateList.valueOf(Color.parseColor("#00000000")))
}
}
This approach can also be used to set own ripple color for each tab.
I have difficulties changing the text size of the tabs of design library tablayout (android.support.design.widget.TabLayout).
I managed to change it by assigning tabTextAppearance in TabLayout
app:tabTextAppearance="#style/MyTabLayoutTextAppearance"
the following style
<style name="MyTabLayoutTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse">
<item name="android:textSize">14sp</item>
</style>
but I have 2 side effects :
1) I lost the accent color of the selected tab
2) The tab text is not capitalized any more.
<style name="MineCustomTabText" parent="TextAppearance.Design.Tab">
<item name="android:textSize">16sp</item>
</style>
Use is in TabLayout like this
<android.support.design.widget.TabLayout
app:tabTextAppearance="#style/MineCustomTabText"
...
/>
Go on using tabTextAppearance as you did but
1) to fix the capital letter side effect add textAllCap in your style :
<style name="MyTabLayoutTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse">
<item name="android:textSize">14sp</item>
<item name="android:textAllCaps">true</item>
</style>
2) to fix the selected tab color side effect add in TabLayout xml the following library attributes :
app:tabSelectedTextColor="#color/color1"
app:tabTextColor="#color/color2"
Hope this helps.
Work on api 22 & 23
Make this style :
<style name="TabLayoutStyle" parent="Base.Widget.Design.TabLayout">
<item name="android:textSize">12sp</item>
<item name="android:textAllCaps">true</item>
</style>
And apply it to your tablayout :
<android.support.design.widget.TabLayout
android:id="#+id/contentTabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/list_gray_border"
app:tabTextAppearance="#style/TabLayoutStyle"
app:tabSelectedTextColor="#color/colorPrimaryDark"
app:tabTextColor="#color/colorGrey"
app:tabMode="fixed"
app:tabGravity="fill"/>
Do as following.
1. Add the Style to the XML
<style name="MyTabLayoutTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">14sp</item>
</style>
2. Apply Style
Find the Layout containing the TabLayout and add the style. The added line is bold.
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
app:tabTextAppearance="#style/MyTabLayoutTextAppearance"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Try the snipped which is mentioned below, it works for me also.
In my layout xml where I have my TabLayout, have added style to the TabLayout like below :
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
style="#style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
and in my style.xml I have defined the style that is used in my layout xml, check code for styles added below :
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="android:background">YOUR BACKGROUND COLOR</item>
<item name="tabTextAppearance">#style/MyCustomTabText</item>
<item name="tabSelectedTextColor">SELECTED TAB TEXT COLOR</item>
<item name="tabIndicatorColor">SELECTED TAB INDICATOR COLOR</item>
</style>
<style name="MyCustomTabText" parent="TextAppearance.AppCompat.Button">
<item name="android:textSize">YOUR TEXT SIZE</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#android:color/white</item>
</style>
I hope it will work for you.....
I have similar problem and similar resolution:
1) Size
in the xml you have TabLayout,
<android.support.design.widget.TabLayout
...
app:tabTextAppearance="#style/CustomTextStyle"
...
/>
then in style,
<style name="CustomTextStyle" parent="#android:style/TextAppearance.Widget.TabWidget">
<item name="android:textSize">16sp</item>
<item name="android:textAllCaps">true</item>
</style>
If you do not want the characters in uppercase put false in "android:textAllCaps"
2) Text color of selected or unselected Tabs,
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
tabLayout.setTabTextColors(getResources().getColorStateList(R.color.tab_selector,null));
} else {
tabLayout.setTabTextColors(getResources().getColorStateList(R.color.tab_selector));
}
then in res/color/tab_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/white" android:state_selected="true" />
<item android:color="#color/white" />
TabLayout tab_layout = (TabLayout)findViewById(R.id.tab_Layout_);
private void changeTabsFont() {
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/"+ Constants.FontStyle);
ViewGroup vg = (ViewGroup) tab_layout.getChildAt(0);
int tabsCount = vg.getChildCount();
for (int j = 0; j < tabsCount; j++) {
ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
int tabChildsCount = vgTab.getChildCount();
for (int i = 0; i < tabChildsCount; i++) {
View tabViewChild = vgTab.getChildAt(i);
if (tabViewChild instanceof TextView) {
((TextView) tabViewChild).setTypeface(font);
((TextView) tabViewChild).setTextSize(15);
}
}
}
}
This code is works for me using tablayout.
It will change size of fonts and also change font style.
This will also help you guys please check this link
https://stackoverflow.com/a/43156384/5973946
This code works for Tablayout change text color,type face (font style) and also text size.
I was using Android Pie and nothing seemed to worked so I played around with app:tabTextAppearance attribute. I know its not the perfect answer but might help someone.
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabTextAppearance="#style/TextAppearance.AppCompat.Caption" />
XML FILE IN VALUES
<style name="tab">
<item name="android:textSize">#dimen/_10ssp</item>
<item name="android:textColor">#FFFFFF</item>
</style>
TAB LAYOUT
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="#dimen/_27sdp"
android:layout_marginLeft="#dimen/_10sdp"
android:layout_marginRight="#dimen/_10sdp"
app:layout_constraintEnd_toEndOf="parent"
app:tabTextAppearance="#style/tab"
app:tabGravity="fill"
android:layout_marginTop="#dimen/_10sdp"
app:layout_constraintStart_toStartOf="parent"
>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TAB 1"
android:scrollbarSize="#dimen/_4sdp"
/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbarSize="#dimen/_6sdp"
android:text="TAB 2" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbarSize="#dimen/_4sdp"
android:text="TAB 3" />
</com.google.android.material.tabs.TabLayout>
> **create custom style in styles.xml** <style name="customStylename"
> parent="Theme.AppCompat">
> <item name="android:textSize">22sp</item> <item name="android:color">colors/primarydark</item>
> </style>
>
> **link to your material same name **
> <android.support.design.widget.TabLayout
> android:layout_width="match_parent"
> android:layout_height="wrap_content"
> android:id="#+id/tabs"
> app:tabTextAppearance="#style/customStylename"
> />
this is my solution
fun TabLayout.customizeTabSizeAndFont() {
val tabFont = Typeface.createFromAsset(context.assets, "font.ttf")
val tabTextSize = 21f
val viewGroup = this.getChildAt(0) as ViewGroup
for (tabVGPos in 0..viewGroup.childCount) {
val tabViewGroup = viewGroup.getChildAt(tabVGPos) as ViewGroup?
tabViewGroup?.let {
for (tabPos in 0..tabViewGroup.childCount) {
val tab = tabViewGroup.getChildAt(tabPos)
if (tab is TextView) {
tab.typeface = tabFont
tab.setTextSize(TypedValue.COMPLEX_UNIT_SP, tabTextSize)
}
}
}
}
}