Change tab color, ActionBarCompat - android

I want to change the color of my tab, this is my code
action_bar_stacked_background.xml
<!-- STATES WHEN BUTTON IS NOT PRESSED -->
<!-- Non focused states -->
<item android:drawable="#drawable/tab_unselected" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="#drawable/tab_selected" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
<!-- Focused states (such as when focused with a d-pad or mouse hover) -->
<item android:drawable="#drawable/tab_unselected" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="#drawable/tab_selected" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
My styles.xml
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#drawable/action_bar_background</item>
<item name="background">#color/background_lista</item>
<item name="actionModeBackground">#color/actionbar_color</item>
<item name="titleTextStyle">#style/MyActionBarTitleText</item>
<item name="subtitleTextStyle">#style/MyActionBarSubtitleText</item>
<item name="homeAsUpIndicator">#drawable/ic_actionbar_back_button</item>
<!-- Support library compatibility -->
<item name="actionBarTabStyle">#style/MyActionBarTabs</item>
Support library compatibility -->
<!-- <item name="background">#drawable/actionbar_background</item> -->
</style>
<style name="MyActionBarTabs" parent="#style/Widget.AppCompat.ActionBar.TabView">
<item name="android:background">#drawable/action_bar_stacked_background</item>
</style>
My values-v11/styles.xml
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#drawable/action_bar_background</item>
<item name="background">#color/actionbar_color</item>
<item name="android:actionModeBackground">#color/actionbar_color</item>
<item name="android:titleTextStyle">#style/MyActionBarTitleText</item>
<item name="android:subtitleTextStyle">#style/MyActionBarSubtitleText</item>
<item name="android:homeAsUpIndicator">#drawable/ic_actionbar_back_button</item>
<item name="android:actionBarTabStyle">#style/MyActionBarTabs</item>
<!-- Support library compatibility -->
<!-- <item name="background">#drawable/actionbar_background</item> -->
</style>
<style name="MyActionBarTabs" parent="#style/Widget.AppCompat.ActionBar.TabView">
<item name="android:background">#drawable/action_bar_stacked_background</item>
</style>
tab_selected.xml
<!-- draw bottom line to fill the spaces between tabs -->
<item android:top="63dp">
<shape android:shape="rectangle" >
<solid android:color="#898989" />
</shape>
</item>
<!-- leave bottom line only 1px of height, the rest is masked out -->
<item
android:bottom="1px"
android:top="63dp">
<shape android:shape="rectangle" >
<solid android:color="#color/color_background_stacked" />
</shape>
</item>
<!-- draw tab background -->
<item
android:left="#dimen/tab_space"
android:right="#dimen/tab_space">
<shape android:shape="rectangle" >
<corners
android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="0.1dp"
android:topLeftRadius="#dimen/corner_radius"
android:topRightRadius="#dimen/corner_radius" />
<gradient
android:angle="90"
android:endColor="#ccc"
android:startColor="#color/color_background_stacked" />
<stroke
android:width="1px"
android:color="#898989" />
</shape>
</item>
<!-- mask out the bottom line of the tab shape -->
<item
android:left="#dimen/tab_space_plus1"
android:right="#dimen/tab_space_plus1"
android:top="63dp">
<shape android:shape="rectangle" >
<solid android:color="#color/color_underline_stacked" />
</shape>
</item>
and tab_unselected.xml
<item android:top="63dp">
<shape android:shape="rectangle" >
<solid android:color="#898989" />
</shape>
</item>
<item
android:bottom="1px"
android:top="63dp">
<shape android:shape="rectangle" >
<solid android:color="#color/color_background_stacked" />
</shape>
</item>
<item
android:left="#dimen/tab_space"
android:right="#dimen/tab_space">
<shape android:shape="rectangle" >
<corners
android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="0.1dp"
android:topLeftRadius="#dimen/corner_radius"
android:topRightRadius="#dimen/corner_radius" />
<gradient
android:angle="90"
android:endColor="#aaa"
android:startColor="#ddd" />
<stroke
android:width="1px"
android:color="#898989" />
</shape>
</item>
The tabs are shown with grey bcakground and blue underline, I just want to change de underline color. How can I solve it?
Greetings.

actionBarTabStyle attribute should be with the base Application/Activity theme , not under ActionBar style
Create a custom theme like this
<style name="AppTheme" parent="Theme.Base.AppCompat">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarTabStyle">#style/MyActionBarTabView</item>
<item name="actionBarTabStyle">#style/MyActionBarTabView</item>
</style>
And, custom ActionBarTab theme like this
<style name="MyActionBarTabView" parent="Widget.AppCompat.Base.ActionBar.TabView">
<item name="android:background">#drawable/action_bar_stacked_background</item>
</style>
Set the AppTheme to whole Application theme or for an Activity . Let me know how it works.

Related

How to Override material design statepressed in for Android Button

Hello Learned Friends,
I am using Material Design Theme but I need to overried its statePressed so that when the button is clicked it changes color (highlighted for a moment) as demonstrated below.
For this I have a drawable which I set on the buttons as follows
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape>
<solid android:color="#color/primaryColor" />
<stroke android:color="#color/primaryDarkColor" android:width="#dimen/stroke_width"/>
<corners android:radius="#dimen/corner_radius" />
</shape>
</item>
<item android:state_pressed="true">
<shape>
<solid android:color="#color/secondaryColor" />
<stroke android:color="#color/secondaryLightColor" android:width="#dimen/stroke_width" />
<corners android:radius="#dimen/corner_radius" />
</shape>
</item>
I also got a ThemeOverlay to override the state pressed
<style name="ThemeOverlay.Red.UnitedStates" parent="">
<item name="android:colorPressedHighlight">#color/secondaryColor</item>
</style>
Unfortunately when I click the buttons the highlight doesn't happen.
What could I be missing?
This is Material Theme XML
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryVariant">#color/primaryLightColor</item>
<item name="colorPrimaryDark">#color/primaryDarkColor</item>
You can use just the app:backgroundTint attribute:
<com.google.android.material.button.MaterialButton
app:backgroundTint="#color/custom_button_selector"
../>
with:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/red600" android:state_pressed="true"/>
<item android:color="?attr/colorPrimary" android:state_enabled="true"/>
<item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
</selector>
Normal state:
Pressed state:

How to make a Custom Button with XML Files?

Since I followed a Video from "Coding in Flow", I did everything (Link: https://www.youtube.com/watch?v=Z1w3y0saKjY)
And of course, I like to add Custom Buttons into my Game. But the Problem is, that the View shows still the standard button or doesn't even like me (that won't even show in preview). And my laptop doesn't have that much space available, then I can't run on AVD Device. And I'll do the first tests on my 25% development completion.
Still, nothing. This is the code (all 5 codes including styles.xml):
<?xml version="1.0" encoding="utf-8"?>
<!--button_default.xml-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#2D0D0D"
android:startColor="#7E0707" />
<padding
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
<stroke
android:width="2dp"
android:color="#4E2828" />
<corners android:radius="15dp" />
</shape>
<!--button_pressed.xml-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#543838"
android:startColor="#CA7676" />
<padding
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
<stroke
android:width="2dp"
android:color="#4E2828" />
<corners android:radius="15dp" />
</shape>
<!--button_disabled.xml-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#989898" />
<padding
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
<stroke
android:width="2dp"
android:color="#191919" />
<corners android:radius="15dp" />
</shape>
<!--custom_button.xml-->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed" android:state_pressed="true" />
<item android:state_enabled="false" android:drawable="#drawable/button_disabled" />
<item android:drawable="#drawable/button_default" />
</selector>
<!--styles.xml-->
<resources>
<style name="AppTheme" parent="#android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">#color/colorPrimary</item>
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:colorAccent">#color/colorAccent</item>
<item name="android:colorControlHighlight">#color/colorControlHighlight</item>
<item name="android:colorControlNormal">#color/colorControlNormal</item>
<item name="buttonStyle">#style/CustomButton</item>
</style>
<style name="FullScreen" parent="#android:style/Theme.Material.Light.NoActionBar.Fullscreen">
<item name="android:colorPrimary">#color/colorPrimary</item>
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:colorAccent">#color/colorAccent</item>
<item name="android:colorControlHighlight">#color/colorControlHighlight</item>
<item name="android:colorControlNormal">#color/colorControlNormal</item>
<item name="buttonStyle">#style/CustomButton</item>
</style>
<style name="NoActionBar" parent="#android:style/Theme.Material.Light.NoActionBar">
<item name="android:colorPrimary">#color/colorPrimary</item>
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:colorAccent">#color/colorAccent</item>
<item name="android:colorControlHighlight">#color/colorControlHighlight</item>
<item name="android:colorControlNormal">#color/colorControlNormal</item>
<item name="buttonStyle">#style/CustomButton</item>
</style>
<style name="NoStatusBar" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
<item name="buttonStyle">#style/CustomButton</item>
</style>
<style name="CustomButton" parent="Widget.AppCompat.Button">
<item name="android:background">#drawable/custom_button</item>
<item name="android:textColor">#FFF</item>
</style>
</resources>
I hope for any answer. It could be the case of "AndroidX" thing.

Button with background and shape doesn't have a press effect

I can't figure out why my buttons doesn't have the press effect.
i guess my styles is the problem.
The button is displayed inside a fragment hosted by AppCombatActivity
v21 style.xml is
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="MyTheme" parent="MyTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
</style>
</resources>
style.xml is
<style name="MyTheme" parent="MyTheme.Base">
</style>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Your customizations go here -->
<item name="buttonStyle">#style/LoginButton</item>
<!-- Override the color of UI controls -->
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:textColorSecondary">#color/white</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="colorPrimary">#color/my_blue</item>
<item name="colorPrimaryDark">#color/my_blue</item>
<item name="colorAccent">#color/my_purple</item>
</style>
<style name="LoginButton" parent="Widget.AppCompat.Button.Colored">
<item name="android:textAllCaps">false</item>
<item name="colorControlHighlight">#color/my_purple</item>
<item name="android:textColor">#color/white</item>
</style>
button code:
<Button
android:theme="#style/LoginButton"
android:background="#drawable/roundedbutton_do_this"
android:backgroundTint="#color/dark_blue"
android:id="#+id/SelectDateButton"
android:layout_width="match_parent"
android:layout_height="52dp"
android:gravity="center"
android:text="#string/ChooseDateTime_SelectDate"
android:layout_margin="12dp"
android:singleLine="false"
android:layout_gravity="center_vertical" />
background code:
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/green"/>
<corners android:radius="10dp" />
</shape>
minSdkVersion:16
targetSdkVersion:27
Now i don't see any effect on the button when i press it. test it on android 6 and 7
EDIT: i have try this but the buttons dont have shape (rounded corners)
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#color/green" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:drawable="#color/green" />
<item>
<shape android:shape="rectangle">
<solid android:color="#color/green"/>
<corners android:radius="10dp" />
</shape>
</item>
</selector>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
app:cardCornerRadius="#dimen/_3sdp"
app:cardBackgroundColor="#android:color/white">
You can use selector as background drawable, refer to this answer.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#color/green" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/ANY_OTHER" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/ANY_OTHER" />
<item android:drawable="#color/green" />
</selector>
I have manage to create a press effect with rounded corners that fades on click this way:
selector.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_shortAnimTime">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/colorbutton" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:drawable="#drawable/colorbutton" />
</selector>
gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp" />
<gradient android:angle="90" android:startColor="#880f0f10" android:centerColor="#880d0d0f" android:endColor="#885d5d5e"/>
</shape>
</item>
</layer-list>
colorbutton.xml
<?xml version="1.0" encoding="utf-8" ?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/green"/>
<corners android:radius="10dp" />
</shape>
and on button i set:
<Button
android:background="#drawable/selector" />
thanks #Omkar for the help.

Style file to add border to all buttons?

How can I make a style file that adds borders to all buttons? All buttons already have a background image.
Create your style in file res/values/styles.xml
<style name="KeyBoardButton">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center</item>
<item name="android:layout_margin">4dp</item>
<item name="android:paddingTop">8dp</item>
<item name="android:paddingBottom">8dp</item>
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
<item name="android:textColor">#555</item>
<item name="android:background">#drawable/button_border</item>
</style>
Define the border that will be used in file res/drawable/button_border.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="4dp"/>
<solid
android:color="#d5cbc6" />
<stroke
android:width="2dp"
android:color="#dedede" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="4dp"/>
<solid
android:color="#f9f9f9" />
<stroke
android:width="2dp"
android:color="#dedede" />
</shape>
</item>
Now you can use it in your layout files, as follows:
<Button android:id="#+id/my_button"
style="#style/KeyBoardButton"
android:text="1" />

Background colour of tabs

I am trying to add colours to tabs of action bar with the help of following link-https://github.com/thecodepath/android_guides/wiki/ActionBar-Tabs-with-Fragments
I added following statements to style.xml-
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarTabStyle">#style/MyTheme.ActionBar.TabView</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:background">#12ABEE</item>
<item name="android:actionMenuTextColor">#ffffff</item>
<item name="android:titleTextStyle">#style/myTheme.ActionBar.Text</item>
</style>
<style name="myTheme.ActionBar.Text" parent="#android:style/TextAppearance">
<item name="android:textColor">#ffffff</item>
<item name="android:textStyle">bold</item>
<item name="android:gravity">center</item>
</style>
<style name="MyTheme.ActionBar.TabView" parent="android:style/Widget.Holo.ActionBar.TabView">
<item name="android:background">#drawable/tab_bar_background</item>
</style>
Also i created tab_bar_background.xml in drawable-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- UNSELECTED TAB STATE -->
<item android:state_selected="false" android:state_pressed="false">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Bottom indicator color for the UNSELECTED tab state -->
<item android:top="-5dp" android:left="-5dp" android:right="-5dp">
<shape android:shape="rectangle">
<stroke android:color="#65acee" android:width="2dp"/>
</shape>
</item>
</layer-list>
</item>
<!-- SELECTED TAB STATE -->
<item android:state_selected="true" android:state_pressed="false">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Tab background color for the SELECTED tab state -->
<item>
<shape>
<solid android:color="#cef9ff"/>
</shape>
</item>
<!-- Bottom indicator color for the SELECTED tab state -->
<item android:top="-5dp" android:left="-5dp" android:right="-5dp">
<shape android:shape="rectangle">
<stroke android:color="#5beea6" android:width="2dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
With these steps in place it should show tab with light blue backgroung(#cef9ff) and green bottom line(#5beea6) on selected and blue bottom line and normal background on unselected.But unfortunately while running i see that bottom line appears fine but the background colour of tab appears black both for selected and unselected tabs.I am not able to figure out wht went wrong..
Updating tab_bar_background.xml to this solved my problem
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- UNSELECTED TAB STATE -->
<item android:state_selected="false" android:state_pressed="false">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Bottom indicator color for the UNSELECTED tab state -->
<item >
<shape >
<stroke android:color="#e8e8e8" android:width="25dp" />
</shape>
</item>
</layer-list>
</item>
<!-- SELECTED TAB STATE -->
<item android:state_selected="true" android:state_pressed="false">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Tab background color for the SELECTED tab state -->
<!-- Bottom indicator color for the SELECTED tab state -->
<item>
<shape >
<stroke android:color="#ffd866" android:width="25dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>

Categories

Resources