I have multiple buttons that all have the same textSize, layout_width, etc. and I do not want to copy and paste these attributes over and over as it makes for hard to edit code. In other words, I am looking to take many of these:
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/round_button_normal"
android:text="#string/button1"
android:textSize="50sp" />
And instead be able to use a preset button (where the layout_width, layout_height, layout_weight, background, and textSize are all set to the above values, by default):
<PresetButton
android:id="#+id/button1"
android:text="#string/button1" />
Just use styles.
Styles and Themes
Example:
In: res/values/styles.xml
<style name="Numbers">
<item name="android:inputType">number</item>
</style>
Use like this:
<EditText
style="#style/Numbers" />
You can use styles.xml to create a style, then you can apply that styling to each button.
http://developer.android.com/guide/topics/ui/themes.html
in your values -> style.xml file put the xml attribute
<style name="buttonConfirm" parent="#android:style/Widget.Button">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">15dip</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
and in your layout call it by this xml
<Button
android:id="#+id/button1"
style="#style/buttonConfirm"
android:text="Button"
/>
Related
I think this snippet from values/styles.xml says it all:
<!-- Style for the label above a button -->
<style name="baseLabel">
<item name="android:gravity">center</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_columnWeight">1</item>
<item name="android:layout_gravity">center_horizontal|fill_horizontal</item>
</style>
And a corresponding entry from layout/main.xml might look like:
<TextView
style="#style/baseLabel" android:text="#string/lr12"
app:layout_row="0"
app:layout_column="1" />
There are no build errors, but the layout_gravity attribute has had no effect. (It works fine if I set it in the layout file: app:layout_gravity="center_horizontal|fill_horizontal)
You're setting android:layout_gravity in your style, but app:layout_gravity in what you say you're setting in the layout file. Those attributes need to match if you want them to do the same thing, so you should be using layout_gravity and layout_columnWeight in your style.
I am trying to add a specific style for a button, the issue is that it's works in themes.xml when I write this :
themes.xml
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorControlHighlight">#color/background</item>
<item name="colorButtonNormal">#color/white</item>
</style>
mylayout.xml
<Button
android:id="#+id/play"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text="Play"
android:textColor="#color/guillotine_background"
android:textSize="25dp"
android:textStyle="bold"/>
but I don't get the same result when I use this :
styles.xml
<style name="MyColorButton" parent="AppTheme">
<item name="colorControlHighlight">#color/background</item>
<item name="colorButtonNormal">#color/white</item>
</style>
I added this line to the button tag in mylayout.xml
mylayout.xml
style="#style/MyColorButton"
Isn't that must return the same result ?
colorControlHighlight and colorButtonNormal are theme attributes. They are for styles that are used as themes for an Activity. They are not attributes of a Button. If you want to use it on the Button but not the whole Activity, use android:theme attribute instead:
<Button
android:theme="#style/MyColorButton"
... />
dmk you are doing it right , you just need to change the parent in the style of the button .
just set the parent="Widget.AppCompat.Button" your code will work fine..
<style name="appbutton" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/themeColordark</item>
<item name="android:textColorPrimary">#color/white</item>
</style>
thanks
edit:
for anyone coming across this, the way I got it to work is to either:
Set the layout directly for each preference item, as in:
<CheckboxPreference
android:layout="#layout/checkbox_preference_item"/>
OR set the style via a theme as I was doing before, but define a widgetlayout and set it instead of an actual layout, like so, where preferenceStyle is set to #style/Preference and checkBoxPreferenceStyle is set to #style/Preference.Checkbox:
<Style name="Preference">
<item name="android:layout">#layout/checkbox_preference_item</item>
</Style>
<Style name="Preference.Checkbox">
<item name="android:widgetLayout">#layout/your_custom_widget</item>
</Style>
I'm trying to customize the layout of a Checkbox Preference item and the checkbox itself is not showing up. I'm modeling after android code and not sure where I'm going wrong. The text views are working fine as well as the rest of the layout but the checkbox is not showing up. Would appreciate any help with this.
Relevant code:
theme.xml:
<style name="Theme.Settings" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/SettingsActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:preferenceCategoryStyle">#style/PreferenceCategory</item>
<item name="android:preferenceStyle">#style/Preference</item>
<item name="android:checkBoxPreferenceStyle">#style/Checkbox</item>
</style>
style.xml
<style name="Checkbox">
<item name="android:layout">#layout/checkbox_preference_item</item>
</style>
<style name="Preference.Text">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textSize">#dimen/preferences_item_font</item>
<item name="android:textColor">#color/preference_text</item>
</style>
<style name="Preference.Text.Extra" parent="Preference.Text">
<item name="android:textSize">#dimen/preferences_header_font</item>
</style>
checkbox_preference_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/preferences_item_height"
android:background="#color/white"
android:paddingLeft="#dimen/preferences_item_left">
<LinearLayout android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center_vertical">
<TextView android:id="#android:id/title"
style="#style/Preference.Text"/>
<TextView android:id="#android:id/summary"
style="#style/Preference.Text.Extra"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible"
android:id="#android:id/widget_frame"
android:layout_alignParentRight="true"/>
</RelativeLayout>
I am not seeing a CheckBox in the layout xml. Am I missing something? You need to specify something like:
<CheckBox android:id="..." ... />
Then you may need to specify something like
<CheckBoxPreference android:key="..." ... />
in the preference xml file. Setting should be android:widgetLayout to the custom CheckBox.
See this post may help you more customizing checkbox preference
I have created several styles for font in my app.
How can I add these styles to views? - 1) Using style attribute 2) Using textAppearance.
To use style is not an option because views may have other attributes (margins, paddings, min width, etc - I cant specify 2 styles for a view), so I need to specify text-related attributes separately, but android:textColor doesnt work here:
styles.xml:
<style name="TextAppearance.baseText" parent="#android:style/TextAppearance">
<item name="android:textAppearance">?android:textAppearanceSmall</item>
<item name="android:typeface">sans</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textSize">15sp</item>
</style>
layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="65dp"
android:orientation="horizontal" >
<Button
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sometext"
android:textAppearance="#style/TextAppearance.baseText"/>
</RelativeLayout>
Why doesnt it work? How can I specify textcolor in textappearance?
As Oderik has mentioned in the comments, the Button view has a default value for it's textColor attribute. This textColor value overrides whatever value is set through the textAppearance attribute. Therefore you have two options:
Set the textColor to #null in the style:
<style name="Application.Button">
<item name="android:textAppearance">#style/Application.Text.Medium.White</item>
<item name="android:textColor">#null</item>
</style>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Application.Button" />
Set the textColor to #null in the Button XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Application.Button"
android:textColor="#null" />
It works. Your text color is white - the same as default color. Put there any other color like <item name="android:textColor">#AA6699</item> and you will see difference.
Second thing is that you are trying to set text appearance in text appearance - doen's make sense. If you want to set small letters do this using parent parent="#android:style/TextAppearance.Small and delete line <item name="android:textAppearance">?android:textAppearanceSmall</item>
Third thing is that you want to have small letters and put additional textSize - doesn't make sense.
Try this, works for me:
<style name="BaseText" parent="#android:style/TextAppearance.Small">
<item name="android:typeface">sans</item>
<item name="android:textColor">#AA7755</item>
</style>
If you want to set everything in style:
<style name="BaseText" parent="#android:style/TextAppearance.Large">
<item name="android:typeface">sans</item>
<item name="android:textColor">#AA7755</item>
</style>
<style name="BaseText.Margins">
<item name="android:layout_margin">10dip</item>
</style>
Where BaseText.Margins inherits from BaseText.
Then you can just use:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
style="#style/BaseText.Margins" />
If you want to use android:TextAppearance instead of style: to set your android:textColor, you need to set android:textColor=#null on your TextView.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:textAppearance="#style/TextAppearance.AppCompat.Medium.MySubheader"
android:textColor="#null"
tools:text="Section" />
Then it will inherit correctly from your android:TextAppearance
<style name="TextAppearance.AppCompat.Medium.MySubheader">
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/black_54</item>
</style>
I've defined some style resources that include TextAppearance with a defined TextColor. I then apply the styles to some TextViews and Buttons. All the styles come through with the TextView, but not the Button. For some reason, the textColor attribute is not showing up. Is this a bug, or am I missing something in the case of the Button?
Here is the style definition:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="TestApp">
</style>
<!-- Text Appearances -->
<style name="TestApp.TextAppearance">
<item name="android:typeface">sans</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">16px</item>
<item name="android:textColor">#6666FF</item>
</style>
<!-- Widget Styles -->
<style name="TestApp.Widget">
<item name="android:layout_margin">3sp</item>
</style>
<style name="TestApp.Widget.Label">
<item name="android:textAppearance">#style/TestApp.TextAppearance</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="TestApp.Widget.Switch">
<item name="android:textAppearance">#style/TestApp.TextAppearance</item>
<item name="android:layout_width">100px</item>
<item name="android:layout_height">100px</item>
</style>
</resources>
and here's the layout where I attempt to apply them:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
style="#style/TestApp.Widget.Label"
android:text="This is my label." />
<TextView
style="#style/TestApp.Widget.Label"
android:text="This is my disabled label."
android:enabled="false" />
<Button
style="#style/TestApp.Widget.Switch"
android:text="This is my switch." />
<Button
style="#style/TestApp.Widget.Switch"
android:text="This is my disabled switch."
android:enabled="false" />
</LinearLayout>
Apparently the way to achieve this is to override the textColor attribute in the button styling. Android does it this way in their Global Theme Styles:
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/styles.xml
I'd love to understand why this is the case.
For the case of Buttons, there are two ways to define text color through attributes: textColor and through a style defined by textAppearance.
The value set by textColor (which is set by the default style) will override any text color value set by the textAppearance style. Therefore you must set the textColor attribute to #null in one of two ways.
Set the textColor to #null in the style:
<style name="Application.Button">
<item name="android:textAppearance">#style/Application.Text.Medium.White</item>
<item name="android:textColor">#null</item>
</style>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Application.Button" />
Set the textColor to #null in the Button XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Application.Button"
android:textColor="#null" />
There appears to be a textAppearanceButton as well:
http://developer.android.com/reference/android/R.attr.html#textAppearanceButton
You may want to try that.