I want to create a custom button class so that I don't need to always add styles in xml. I miss to add styles in xml sometimes and is created as a bug. By using Custom class, I want to remove this dependency of adding styles every time from styles.xml.
I want to use this approach for all textviews and editText but I am unable to find how to do this. Please suggest approach. Thanks.
It is not exactly what you are looking for.
Use the Material Components library and just define in your app theme the materialButtonStyle attribute with your favorite style.
It will define the style for the buttons globally in the app.
<style name="AppTheme" parent="Theme.MaterialComponents.*">
...
<item name="materialButtonStyle">#style/CustomButton</item>
</style>
<style name="CustomButton" parent="#style/Widget.MaterialComponents.Button">
...
</style>
Instead if you are still using an AppCompat Theme you can use the buttonStyle attribute.
<style name="AppTheme" parent="Theme.AppCompat.*"/>
...
<item name="buttonStyle">#style/Widget.AppCompat.Button</item>
</style>
Related
I am using the AppCompat theme and I want set the minHeight attribute on my buttons:
<style name="Theme.MyTheme" parent="Theme.AppCompat">
<item name="android:buttonStyle">#style/MyButtonStyle</item>
</style>
<style name="MyButtonStyle" parent="...?">
<item name="android:minHeight">60dp</item>
</style>
However, there is no Widget.AppCompat.Button style to set as the parent for MyButtonStyle. If I use android:Widget.Button, then all my buttons look like the old crappy style. I tried various other AppCompat themes like TextAppearance.AppCompat.Button, but they do not work.
Leaving out a parent theme for the button style also causes the button not to be styled correctly.
How can I customize the default Theme.AppCompat buttonStyle?
You can have Base.MyButtonStyle extend android:Widget.Holo.Button on API 14+ (in res/values-v14/styles.xml) and android:Widget.Material.Button on API 21+ (in res/values-v21/styles.xml. This style will change according to the device system version. Put your platform specific modifications here.
Then have MyButtonStyle extend Base.MyButtonStyle and define the android:minHeight here (in res/values/styles.xml). This will apply to all platforms.
You buttons then can use style MyButtonStyle.
This example assumes your minimum SDK is 14.
And yes, there's no appcompat-v7 button style (well, at least not yet).
EDIT
This assumes you're OK with Holo button on platforms older than Lollipop. It feels unobtrusive and if you can do without ripples, it should be just fine. If you want ripples I suggest you google for a third party lollipop button library.
To answer my own question, it appears AppCompat does not in fact support the Button widget presently:
AppCompat provides similar behaviour on earlier versions of Android
for a subset of UI widgets:
Everything provided by AppCompat’s toolbar (action modes, etc)
EditText
Spinner
CheckBox
RadioButton
Switch (use the new android.support.v7.widget.SwitchCompat)
CheckedTextView
The only workaround I see would be to recreate the Material button style from the Android source code, a task which extends outside the scope of my desire.
Custom Button Style With AppCompat +22
in your styles.xml
<style name="Button.Tinted" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">YOUR_TINT_COLOR</item>
<item name="colorControlHighlight">#color/colorAccent</item>
<item name="android:textColor">#android:color/white</item> </style>
in your layout.xml
<Button
android:id="#+id/but_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/but_continue"
android:theme="#style/Button.Tinted" />
With new MaterialComponent it is convenient to use com.google.android.material.button.MaterialButton instead of regular Button.
But to style it, there is used the different attribute in the theme
- materialButtonStyle, with this parent theme Theme.MaterialComponents.DayNight.NoActionBar.
Then the theme should look like this:
<style name="NewAppTheme"
parent="Theme.MaterialComponents.DayNight.NoActionBar">
.......
<-- IMPORTANT if you are using com.google.android.material.button.MaterialButton to style them use this parameter
<item name="materialButtonStyle">#style/ButtonStyle</item>
</style>
And in ButtonStyle you can change button attributes like this:
<style name="ButtonStyle" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:minHeight">60dp</item>
</style>
Remove the android:
<style name="Theme.MyTheme" parent="Theme.AppCompat">
<item name="buttonStyle">#style/MyButtonStyle</item>
</style>
https://stackoverflow.com/a/32363833
I am using the AppCompat theme and I want set the minHeight attribute on my buttons:
<style name="Theme.MyTheme" parent="Theme.AppCompat">
<item name="android:buttonStyle">#style/MyButtonStyle</item>
</style>
<style name="MyButtonStyle" parent="...?">
<item name="android:minHeight">60dp</item>
</style>
However, there is no Widget.AppCompat.Button style to set as the parent for MyButtonStyle. If I use android:Widget.Button, then all my buttons look like the old crappy style. I tried various other AppCompat themes like TextAppearance.AppCompat.Button, but they do not work.
Leaving out a parent theme for the button style also causes the button not to be styled correctly.
How can I customize the default Theme.AppCompat buttonStyle?
You can have Base.MyButtonStyle extend android:Widget.Holo.Button on API 14+ (in res/values-v14/styles.xml) and android:Widget.Material.Button on API 21+ (in res/values-v21/styles.xml. This style will change according to the device system version. Put your platform specific modifications here.
Then have MyButtonStyle extend Base.MyButtonStyle and define the android:minHeight here (in res/values/styles.xml). This will apply to all platforms.
You buttons then can use style MyButtonStyle.
This example assumes your minimum SDK is 14.
And yes, there's no appcompat-v7 button style (well, at least not yet).
EDIT
This assumes you're OK with Holo button on platforms older than Lollipop. It feels unobtrusive and if you can do without ripples, it should be just fine. If you want ripples I suggest you google for a third party lollipop button library.
To answer my own question, it appears AppCompat does not in fact support the Button widget presently:
AppCompat provides similar behaviour on earlier versions of Android
for a subset of UI widgets:
Everything provided by AppCompat’s toolbar (action modes, etc)
EditText
Spinner
CheckBox
RadioButton
Switch (use the new android.support.v7.widget.SwitchCompat)
CheckedTextView
The only workaround I see would be to recreate the Material button style from the Android source code, a task which extends outside the scope of my desire.
Custom Button Style With AppCompat +22
in your styles.xml
<style name="Button.Tinted" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">YOUR_TINT_COLOR</item>
<item name="colorControlHighlight">#color/colorAccent</item>
<item name="android:textColor">#android:color/white</item> </style>
in your layout.xml
<Button
android:id="#+id/but_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/but_continue"
android:theme="#style/Button.Tinted" />
With new MaterialComponent it is convenient to use com.google.android.material.button.MaterialButton instead of regular Button.
But to style it, there is used the different attribute in the theme
- materialButtonStyle, with this parent theme Theme.MaterialComponents.DayNight.NoActionBar.
Then the theme should look like this:
<style name="NewAppTheme"
parent="Theme.MaterialComponents.DayNight.NoActionBar">
.......
<-- IMPORTANT if you are using com.google.android.material.button.MaterialButton to style them use this parameter
<item name="materialButtonStyle">#style/ButtonStyle</item>
</style>
And in ButtonStyle you can change button attributes like this:
<style name="ButtonStyle" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:minHeight">60dp</item>
</style>
Remove the android:
<style name="Theme.MyTheme" parent="Theme.AppCompat">
<item name="buttonStyle">#style/MyButtonStyle</item>
</style>
https://stackoverflow.com/a/32363833
I have finished making my app. Now, I want to reset all my editTexts to have the layout width as fill parent instead of wrap content.
android:layout_width="fill_parent"
while currently all my editTexts are
android:layout_width="wrap_content"
Is there any way i can do this in a style xml file, instead of individually in each layout?
I currently have this as my styles.xml
<style name="AppTheme" parent="android:Theme.Light">
<item name="android:fontFamily">Verdana</item>
<item name="android:editTextStyle">#style/EditTextStyle</item>
</style>
<style name="EditTextStyle" parent="#android:style/Widget.EditText">
<item name="android:layout_width">fill_parent</item>
<item name="android:textColor">#808080</item>
</style>
But i'm getting an exception saying that layout_width must be specified.
This is my exception:
07-15 11:13:34.872: E/AndroidRuntime(1195): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.passwordkeeper.ui/com.passwordkeeper.ui.ActivityLogin}: java.lang.RuntimeException: Binary XML file line #29: You must supply a layout_width attribute.
Any easy way out or do i have to change the attribute in all my editText's individually?
You can try this one.
Here is the part of the manifest file you need to change to call your custom theme (the custom theme called here is AppTheme:
<application android:name="YourApplication"
android:theme="#style/AppTheme" >
Then in your file styles.xml, create and customize this custom theme:
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:typeface">YourTypeFace</item>
</style>
You can add the parameters you need inside the style. This will apply the style to all your textviews.
One solution to your problem is to apply a custom theme to all of your activities. In order to do that, you can inherit properties from an existing theme and override the properties that you want to change.
In AndroidManifest.xml, locate the <application> element.
Add the attribute to it:
android:theme="#style/"
Locate styles.xml file in the values folder.
Use the following template:
<style name="ApplicationStyle" parent="android:Theme.Light">
<item name="#android:editTextStyle">#style/customEditText</item>"
</style>
Names used in the above are just examples, you may use your own. As to the parent theme, that is also up to you.
All that is left is the definition of editTextStyle (or whatever name you have chosen for the style). You should inherit properties from Widget.EditText and override the properties that you want to change, like the following:
<style name="customEditText" parent="#android:style/Widget.EditText">
<item name="android:textColor" >#ffffff</item>
</style>
To quote the official android guide:
The parent attribute in the element is optional and specifies
the resource ID of another style from which this style should inherit
properties. You can then override the inherited style properties if
you want to.
I tried to make it easy to understand and follow. I'm a junior dev so while the above solution works for me, it may not be the best one out there. As I said though, it solves the problem rather efficiently.
Unfortunately it is not possible to set layout attributes (layout_*) from a theme (see Layout Parameters documentation and this answer from an Android framework engineer). You must set them on each element or set the layout attributes in a style and let each element reference the style like this:
<Button style="#style/ButtonBig" android:text="my text" />
where ButtonBig is defined like this:
<style name="ButtonBig" parent="android:Widget.Holo.Light.Button">
<item name="android:textSize">20sp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">100dp</item>
</style>
I was having this exact issue. For some reason it helped me to drop the android: part in the AppTheme definition, and leave it only as editTextStyle (as mentioned in this answer):
<style name="AppTheme" parent="android:Theme.Light">
<item name="android:fontFamily">Verdana</item>
<item name="editTextStyle">#style/EditTextStyle</item>
</style>
You'll have to set the property style="#styles/EditTextStyle" to all of your EditText components in your application.
define the style attribute for all EditText like below:
<EditText
android:id="#+id/editText1"
android:layout_height="wrap_content"
style="#style/EditTextStyle">
You have to specify layout_width individually to each and every View. There is no way To escape. You can create a LayoutParams object and set its width and height in it and set in it every EditText like
textView1.setLayoutParams(lParams);
textView2.setLayoutParams(lParams);
...
I want to change color of the blue parts in the following image:
I know how to use custom styles, I just need to know what are called the properties of these blue parts (I mean something like <item name="android:background">#FF0000</item> ) thx
What I really want to achieve is demonstrated here:
You may inherit the theme used from Android code and change particulars within that.
http://developer.android.com/guide/topics/ui/themes.html
<style name="GreenText" parent="#android:style/TextAppearance">
<item name="android:textColor">#00FF00</item>
</style>
parent holds the value of the default theme. In your case it will be the Dialog theme for Holo.
I have a TextView and I want to apply a Style which I use for all TextView elements plus another style which I only use within a specific Activity. Is there any possibility to do that?
Just a little piece of information that might add to the overall value of the question - shamelessly copied from: http://developer.android.com/guide/topics/ui/themes.html#DefiningStyles
If you want to inherit from styles that you've defined yourself, you do not have to use the parent attribute. Instead, just prefix the name of the style you want to inherit to the name of your new style, separated by a period. For example, to create a new style that inherits the CodeFont style defined above, but make the color red, you can author the new style like this:
<style name="CodeFont.Red">
<item name="android:textColor">#FF0000</item>
</style>
Notice that there is no parent attribute in the tag, but because the name attribute begins with the CodeFont style name (which is a style that you have created), this style inherits all style properties from that style. This style then overrides the android:textColor property to make the text red. You can reference this new style as #style/CodeFont.Red.
You can continue inheriting like this as many times as you'd like, by chaining names with periods. For example, you can extend CodeFont.Red to be bigger, with:
<style name="CodeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style>
A style under Android can have a parent style.
So just have MyActivityTextView define GeneralTextView as parent style, and change/add style properties.
Then you can use MyActivityTextView for some views and GeneralTextView for the others.
It's described in Defining Styles.
You can extend one style with another in your style.xml:
<style name="current_weekday_white" parent="current_day_white">
<item name="android:textColor">#FFABAB</item>
</style>
Inherit one style from another and copy elements from third.
<style name="Button.Style1" parent="android:style/Widget.Button">
<item name="android:gravity">center</item>
<item name="android:textSize">12sp</item>
<item name="android:background">#drawable/shape_button</item>
<!-- Here are attributes from third style -->
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textColor">#112233</item>
</style>