How to capitalize button text pre-Lollipop? - android

I tried using adding it into styles.xml like so:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">#color/accent</item>
<!-- buttons will not have shadows -->
<item name="android:buttonStyle">#style/Widget.AppCompat.Button.Borderless</item>
<item name="android:textAppearanceButton">#style/DefaultButtonTextStyle</item>
<item name="android:colorBackground">#android:color/white</item>
</style>
<!--The button text style to be used throughout the app-->
<style name="DefaultButtonTextStyle" parent="#style/Base.TextAppearance.AppCompat.Button">
<!-- Ensure that the buttons are all caps even pre Lollipop-->
<item name="android:textAllCaps">true</item>
<item name="textAllCaps">true</item>
</style>

You only need to create a custom style for your button
<style name="CustomButton">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_centerVertical">true</item>
<item name="android:textAllCaps">true</item>
</style>
Then set it to your button like:
<Button
android:id="#+id/total_bill"
style="#style/CustomButton" />
Or you can do something like:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="buttonStyle">#style/MyButton</item>
</style>
<style name="MyButton" parent="Widget.AppCompat.Button">
<item name="android:textAllCaps">true</item>
</style>
The second method will change it for all Button across the application.One another way is to extend the Button and create a custom Button where textcaps is set to true and use that Button instead of default one.

Related

How to set unique TextView text color through Android Theme when multiple Themes are available

I have a couple of Themes in an app and it works ok. Now I wanted to set a chat bubble text color to color red when user select BaseTheme.Red, and text color orange when user select BaseTheme.Orange (see code below)
It´s only the chat bubble text right that I want to be like red for the ´Red´ and Orange for the Orange Theme and all other TextView text color in the app will have default Theme color.
I try to learn Android Themes and got into trouble setting this chat TextView text color to another color then this global:
<item name="android:textColor">#color/white</item>
I created this: Inside the BaseTheme.Red
<item name="chatBubbleTextColor">#color/material_red_500</item>
and thought I could use it in the TextView xml like
android:textColor="?attr/chatBubbleTextColor"
but i cannot get it to work maybe it does not work like that?
How can I with the Themes below make this work?
Here is two Themes Red and Orange:
<!-- Base Theme -->
<style name="BaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Attributes for all APIs -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="dialogTheme">#style/AppTheme.Dialog</item>
<item name="alertDialogTheme">#style/AppTheme.Dialog.Alert</item>
<item name="colorControlHighlight">#color/selector_black_pressed</item>
<!-- Theme for the Preferences -->
<item name="preferenceTheme">#style/AppPreferenceTheme</item>
<!-- Theme for the pacv_placesAutoCompleteTextV -->
<item name="pacv_placesAutoCompleteTextViewStyle">#style/Widget.AppCompat.EditText</item>
<!-- Default App Theme -->
<style name="AppTheme" parent="BaseTheme">
<!-- API specific attributes 14+ -->
<item name="selectableRectDrawable">#drawable/state_list_selectable_rect_black</item>
<item name="selectableRectDrawableInverse">#drawable/state_list_selectable_rect_white</item>
<item name="selectableRectDrawableColored">#drawable/state_list_selectable_rect_black</item>
<item name="selectableRoundedRectDrawable">#drawable/state_list_selectable_rounded_rect_black</item>
<item name="selectableRoundedRectDrawableInverse">#drawable/state_list_selectable_rounded_rect_white</item>
<item name="selectableRoundedRectDrawableColored">#drawable/state_list_selectable_rounded_rect_black</item>
</style>
<!-- Orange App Theme -->
<style name="BaseTheme.Orange" parent="AppTheme">
<!-- Attributes for all APIs -->
<item name="colorPrimary">#color/material_orange_500</item>
<item name="colorPrimaryDark">#color/material_orange_700</item>
<item name="colorAccent">#color/material_orange_a700</item>
<item name="dialogTheme">#style/AppTheme.Dialog.Orange</item>
<item name="alertDialogTheme">#style/AppTheme.Dialog.Alert.Orange</item>
<item name="android:windowBackground">#color/material_orange_300</item>
</style>
<style name="AppTheme.Orange" parent="BaseTheme.Orange">
<!-- API specific attributes 14+ -->
<item name="selectableRectDrawableColored">#drawable/state_list_selectable_rect_orange</item>
<item name="selectableRoundedRectDrawableColored">#drawable/state_list_selectable_rounded_rect_orange</item>
<!-- Add your custom overall styles here -->
</style>
<!-- Red App Theme -->
<style name="BaseTheme.Red" parent="AppTheme">
<!-- Attributes for all APIs -->
<item name="colorPrimary">#color/material_red_500</item>
<item name="colorPrimaryDark">#color/material_red_700</item>
<item name="colorAccent">#color/material_red_a700</item>
<item name="dialogTheme">#style/AppTheme.Dialog.Red</item>
<item name="alertDialogTheme">#style/AppTheme.Dialog.Alert.Red</item>
<item name="android:windowBackground">#color/material_red_300</item>
<!-- Chat bubble attribute not working-->
<item name="chatBubbleTextColor">#color/material_red_500</item>
</style>
<style name="AppTheme.Red" parent="BaseTheme.Red">
<!-- API specific attributes 14+ -->
<item name="selectableRectDrawableColored">#drawable/state_list_selectable_rect_red</item>
<item name="selectableRoundedRectDrawableColored">#drawable/state_list_selectable_rounded_rect_red</item>
<!-- Add your custom overall styles here -->
</style>
I found the answer to my own question here
Basically, it goes like this:
In the file attr.xml, I define this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="ChatBubbleBackGroundColor" format="reference|color" />
<attr name="ChatBubbleTextColor" format="reference|color" />
</resources>
Next I add to my two BaseTheme:
<style name="BaseTheme.Red" parent="AppTheme">
<item name="ChatBubbleBackGroundColor">#color/material_red_a200</item>
<item name="ChatBubbleTextColor">#color/material_red_a700</item>
</style>
<style name="BaseTheme.Orange" parent="AppTheme">
<item name="ChatBubbleBackGroundColor">#color/material_orange_a200</item>
<item name="ChatBubbleTextColor">#color/material_orange_a700</item>
</style>
and finally in my layout
<TextView
android:id="#+id/quoteTitle"
android:textColor="?ChatBubbleTextColor"
android:BackGround="?ChatBubbleBackGroundColor"
...
</TextView>

Reference a style without "#" symbol?

i have this code:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/primary_color</item>
<item name="colorPrimaryDark">#color/primary_dark_color</item>
<item name="actionBarStyle">#style/MiwokAppBarStyle</item>
<item name="android:windowContentOverlay">#null</item>
</style>
<!-- App bar style -->
<style name="MiwokAppBarStyle" parent="style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<!-- Remove the shadow below the app bar -->
<item name="elevation">0dp</item>
</style>
<!-- Style for a tab that displays a category name -->
<style name="CategoryTab" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#android:color/white</item>
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextAppearance">#style/CategoryTabTextAppearance</item>
</style>
<!-- Text appearance style for a category tab -->
<style name="CategoryTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textColor">#A8A19E</item>
</style>
</resources>
why in MiwokAppBarStyle we don't use # symbol in referencing the parent style like this parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse" ?, and why we referencing parent style using style/ insted of referencing without style/ prefix like parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse" ??

Android: Can't seem to theme SearchView

I've been trying real hard to theme SearchView. My min API is sdk 21 too, so I should be able to use the new styling options.
Specifically, I want the in-app-bar text field that appears when I click on the search icon to be theme-able. I want to ultimately make the background white, and the text/cursor either blue or black.
Here is my styles.xml. The last theme is the new SearchTheme I'm working on. The actual values don't matter, they're garbage values I threw in just trying to make a change happen.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#android:color/white</item>
<item name="android:searchViewStyle">#style/AppTheme.SearchTheme</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.PreferencesTheme" parent="AppTheme">
<item name="android:windowBackground">#android:color/white</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:windowBackground">#android:color/white</item>
<item name="android:searchViewStyle">#style/AppTheme.SearchTheme</item>
</style>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">
<item name="android:windowBackground">#android:color/white</item>
<item name="android:searchViewStyle">#style/AppTheme.SearchTheme</item>
</style>
<style name="AppTheme.SearchTheme" parent="Theme.AppCompat.Light">
<item name="android:closeIcon">#drawable/contacts5icon</item>
<item name="android:voiceIcon">#drawable/contacts5icon</item>
<item name="android:queryBackground">#android:color/black</item>
<item name="android:windowBackground">#android:color/white</item>
</style>
</resources>
I've assigned the AppTheme.SearchTheme to the other main themes that I'm using in my layout. But nothing I do seems to have any effect. The values don't seem to change.
I haven't really messed with Android theming before, so I'm a bit lost.
This should help you get on the right track, per Chris Banes' walk-through for using material design on pre-lollipop androids. For you, in a nutshell: set item property searchViewStyle under your main style with a Widget.AppCompat.SearchView style extension.
You can use the queryBackground item property for altering the text field's background. Other properties are detailed below.
<style name=”Theme.MyTheme” parent=”Theme.AppCompat”>
<item name=”searchViewStyle”>#style/MySearchViewStyle</item>
</style>
<style name=”MySearchViewStyle” parent=”Widget.AppCompat.SearchView”>
<!-- Background for the search query section (e.g. EditText) -->
<item name="queryBackground">...</item>
<!-- Background for the actions section (e.g. voice, submit) -->
<item name="submitBackground">...</item>
<!-- Close button icon -->
<item name="closeIcon">...</item>
<!-- Search button icon -->
<item name="searchIcon">...</item>
<!-- Go/commit button icon -->
<item name="goIcon">...</item>
<!-- Voice search button icon -->
<item name="voiceIcon">...</item>
<!-- Commit icon shown in the query suggestion row -->
<item name="commitIcon">...</item>
<!-- Layout for query suggestion rows -->
<item name="suggestionRowLayout">...</item>
</style>
more here

Backward compatibity using v-21/styles.xml not working

I am trying to style my button while maintaining backwards compatibility.
I have 2 files as follows:
values/styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">#color/accent</item>
<item name="android:textColorPrimary">#color/primary_text</item>
<item name="android:textColorSecondary">#color/secondary_text</item>
<item name="android:buttonStyle">#style/RoundedButton</item>
</style>
<style name="RoundedButton" parent="#style/Widget.AppCompat.Button">
</style>
<style name="AppName.NoActionBar" parent="AppTheme" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="DemoButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:minWidth">40dp</item>
</style>
</resources>
values-v21/styles.xml:
<resources>
<style name="RoundedButton" parent="Widget.AppCompat.Button">
<item name="android:background">#drawable/btn_rounded</item>
</style>
</resources>
But even then my button looses all attributes i.e. just a text is displayed as per android-studio's "XML preview". I just want the button to default to the usual Holo theme on pre-lolipop devices. It works as expected on Lolipop devices.
What am I doing wrong?
In values/styles.xml
<style name="RoundedButton" parent="#style/Widget.AppCompat.Button">
</style>
change it to as Follow :-
<style name="RoundedButton" parent="android:Widget.Holo">
</style>
I couldn't figure out the reason of why this was happening so I removed the line <item name="android:buttonStyle">#style/RoundedButton</item> from my values/styles.xml file, and added it to my values-v21/styles.xml.
I do not like this solution as there is a lot of code duplication. The only good thing about it is that it works.
Now my files look like:
values/styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">#color/accent</item>
<item name="android:textColorPrimary">#color/primary_text</item>
<item name="android:textColorSecondary">#color/secondary_text</item>
</style>
<style name="AppName.NoActionBar" parent="AppTheme" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="DemoButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:minWidth">40dp</item>
</style>
</resources>
v21/styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">#color/accent</item>
<item name="android:textColorPrimary">#color/primary_text</item>
<item name="android:textColorSecondary">#color/secondary_text</item>
<item name="android:buttonStyle">#style/RoundedButton</item>
</style>
<style name="RoundedButton" parent="Widget.AppCompat.Button">
<item name="android:background">#drawable/btn_rounded</item>
</style>
<style name="AppName.NoActionBar" parent="AppTheme" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="DemoButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:minWidth">40dp</item>
</style>
</resources>

can we set actionbar height manually in android theme.light

I am using "Theme.Light" theme for my android project and while setting up a navigation drawer how can i set actionbar size(height).
I used the below code and i got an error..
<style name="Theme.Light.ActionBar" parent="#android:style/Theme.Light">
<item name="android:windowNoTitle">false</item>
<item name="android:windowActionBar">true</item>
<item name="actionBarSize">36dip</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
can you please reply to my problem as soon as possible and thank you.
Use height attribute,for actionBarHeight
<item name="android:height">#dimen/bar_height</item>
you have to create style like this:
<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<!-- native widgets will now be "tinted" with accent color -->
<item name="colorAccent">#color/accent</item>
<!--Action bar style-->
<item name="android:actionBarStyle">#style/AppTheme.ActionBar</item>
<item name="actionBarStyle">#style/AppTheme.ActionBar</item>
</style>
<style name="AppTheme.ActionBar" parent="Widget.AppCompat.Light.ActionBar">
<item name="titleTextStyle">#style/AppTheme.ActionBar.TitleText</item>
<item name="android:titleTextStyle">#style/AppTheme.ActionBar.TitleText</item>
<item name="android:height">#dimen/bar_height</item>
</style>
<style name="AppTheme.ActionBar.TitleText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textSize">#dimen/bar_text_size</item>
<item name="android:textColor">#color/bar_text_color</item>
</style>
you have to try below code :
<resources>
<style name="Theme.FixedSize" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="actionBarSize">48dip</item>
<item name="android:actionBarSize">48dip</item>
</style>
</resources>
If you are using AppCompat, you can set the "actionBarSize" attribute in your theme (which must inherit from Theme.AppCompat.x). You can also use a Toolbar in your layout and set its layout_height to the desired value.

Categories

Resources