<manifest ...> | |
... | |
<!-- Make sure your app (or individual activity) uses the | |
theme with the custom attribute defined. --> | |
<application android:theme="@style/AppTheme" ...> | |
... | |
</application> | |
</manifest> |
public class MyActivity extends Activity { | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
... | |
// Pass in the theme attribute that'll resolve to the | |
// desired button style resource. The current theme | |
// must have a value set for this attribute. | |
Button myButton = new Button(this, null, R.attr.myButtonStyle); | |
myButton.setText("Hello world"); | |
ViewGroup containerView = (ViewGroup) findViewById(R.id.container); | |
containerView.addView(myButton); | |
} | |
} |
<!-- The convention is to put the <style> elements below | |
in res/values/styles.xml and <declare-styleable> in | |
res/values/attrs.xml. --> | |
<resources> | |
<!-- First declare a custom theme attribute that'll | |
reference a style resource. --> | |
<declare-styleable name="AppTheme"> | |
<attr name="myButtonStyle" format="reference" /> | |
</declare-styleable> | |
<!-- Provide a style resource as the value for the | |
theme attribute. As a side note, if you want to | |
change the default button style, you can instead | |
override the android:buttonStyle attribute. --> | |
<style name="AppTheme" parent="android:Theme.Holo"> | |
<item name="myButtonStyle">@style/MyButton</item> | |
</style> | |
<!-- Define the custom button style itself. Make sure | |
to inherit from an existing button style. --> | |
<style name="MyButton" parent="android:Widget.Holo.Button"> | |
<item name="android:textColor">#f00</item> | |
</style> | |
</resources> |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
cesards
commented
May 9, 2014
@romannurik This is not working for me. Maybe there is an android bug? If I use:
where
And TextView initialization does:
But it doesn't recognize fontFamily attribute :S Is it a normal behaviour? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
jrub
Jun 24, 2015
Very useful indeed, thanks Roman. This should definitely be included into the official documentation for Themes & Styles (http://developer.android.com/guide/topics/ui/themes.html)
jrub
commented
Jun 24, 2015
Very useful indeed, thanks Roman. This should definitely be included into the official documentation for Themes & Styles (http://developer.android.com/guide/topics/ui/themes.html) |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
ursusursus
Feb 19, 2016
For me this does not apply layout params like layout_width, layout_height when I have them in my style. Only default wrap_content LayoutParams gets created in both dimensions
ursusursus
commented
Feb 19, 2016
For me this does not apply layout params like layout_width, layout_height when I have them in my style. Only default wrap_content LayoutParams gets created in both dimensions |
alexfu
commented
Aug 18, 2016
•
edited
edited
@cesards: Note that you're using a direct style (i.e. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
saretta93
commented
Feb 6, 2017
Very useful! :) |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
sosite
Jun 1, 2017
@ursusursus: Setting any global style in theme never set layout_*
parameters from style like layout_width
or layout_margins
sosite
commented
Jun 1, 2017
@ursusursus: Setting any global style in theme never set |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
rodolfo-tenfen
Aug 29, 2017
Thank you so much!!! I wasn't being able to get a custom button to be displayed properly and your example was REALLY useful, thanks!!
rodolfo-tenfen
commented
Aug 29, 2017
Thank you so much!!! I wasn't being able to get a custom button to be displayed properly and your example was REALLY useful, thanks!! |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
shgalym
Oct 17, 2017
Button myButton = new Button(this, null, R.attr.myButtonStyle);
@cesards The code above does not inject the current context if not used in Activity. Instead use:
Button myButton = new Button(new ContextThemeWrapper(context, R.attr.myButtonStyle), null, 0);
or if you use AppCompat:
Button myButton = new AppCompatButton(new ContextThemeWrapper(context, R.attr.myButtonStyle), null, 0);
shgalym
commented
Oct 17, 2017
•
edited
edited
@cesards The code above does not inject the current context if not used in Activity. Instead use:
or if you use AppCompat:
|
@romannurik This is not working for me. Maybe there is an android bug?
If I use:
where
And TextView initialization does:
But it doesn't recognize fontFamily attribute :S Is it a normal behaviour?