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);
...
Related
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>
I have set the default button style in my application theme, so that it is applied to every button in the app.
There are few buttons that need to have their text capitalized in addition to default style. So what I'm trying to do is something like this
(the following code snippet doesn't work as I cannot set parent="?attr/materialButtonStyle"):
<style name="capitalizedButton" parent="?attr/materialButtonStyle">
<item name="android:textAllCaps">true</item>
</style>
Is it possible to extend a style defined in theme.xml? I don't want to refer to the local style that I'm setting in theme as that can change later, instead I intend to access it using theme attribute.
If the capitalization happens in addition to your custom default button style, there is no need to make the capitalization style a child of ?attr/materialButtonStyle (This cannot be done anyhow, as ?attr references an attribute in the currently applied theme). You can read more about it here.
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="buttonStyle">#style/DefaultButton</item>
</style>
<style name="DefaultButton" parent="Widget.AppCompat.Button">
<item name="android:textColor">#color/white</item>
</style>
<style name="AllCapsButton">
<item name="android:textAllCaps">true</item>
</style>
layout.xml
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:theme="#style/AllCapsButton"/>
In the code above, button gets the DefaultButton style (white text) applied by the theme, and the AllCapsButton style (capitalized text) applied in the layout file.
You can directly pass style to Button widget in you XML like:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="capitalizedButton"/>
Hope you get the answer. If not please explain your question.
I'm trying to put a style in all my app, so i created a theme with my style inside :
<resources>
<style name="MyTheme" parent="android:Theme.Holo.Light">
<item name="android:textAppearance">#style/subtitle</item>
</style>
<style name="subtitle parent="#android:style/TextAppearance">
<item name="android:textColor">#color/purple</item>
<item name="android:textSize">40sp</item>
</style>
</resources>
But textAppearance doesn't work it stay the same, but when i put something like textColor in my theme, it works
This is a quite old question, but the answer may help someone.
The key to solve this is in the "precedence order of styling techniques" here:
on the top is the highest precedence, at the bottom is the lowest precedence.
As we can see theme has the lowest precedence, in your example, your android:textAppearance property is being overridden by the default style of every view that accepts this attribute, the default style property is defined in every them for every specific view that accepts this attribute, in this case android:Theme.Holo.Light provides the default style for textView as android:textViewStyle... for buttons is android:buttonStyle (which inherits its textAppearance from TextView), and so on.
So if you are trying to apply that android:textAppearance property to a TextVew you should use <item name="android:textViewStyle">#style/subtitle</item> instead of <item name="android:textAppearance">#style/subtitle</item> inside MyTheme. Away to veryfy this is setting android:textViewStyle to null, that way your current code will work fine with textViews <item name="android:textViewStyle">null</item>
This post explains this precedence a bit deeper:
https://medium.com/androiddevelopers/whats-your-text-s-appearance-f3a1729192d
What I can see is, you have not declared the color in your xml for theme. Please add the following line within the <resources> and try. Your xml will look like:
<resources>
<style name="MyTheme" parent="android:Theme.Holo.Light">
<item name="android:textAppearance">#style/subtitle</item>
</style>
<color name="purple">code for your color</color>
<style name="subtitle parent="#android:style/TextAppearance">
<item name="android:textColor">#color/purple</item>
<item name="android:textSize">40sp</item>
</style>
I think this will do.
Depends on your target API you need to put your customization code in different /res/values-vxx/style.xml files.
For TextView, try android:textAppearanceSmall inside your theme instead.
is there a way to define a style that affects all Views in an Activity of a given type, without editing all of the xml View tags to define the style. is there a way to define something like "all TextViews in this Activity have a minHeight of 70sp"?
A theme is what you want. Each android widget has a default style that you could override to make it as you want. For example:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="TextViewSpecial" parent="#android:style/Theme">
<item name="android:textViewStyle">#style/SpecialTextView</item>
</style>
</resources>
a theme that overrides the style for a TextView. SpecialTextView is a style like this:
<style name="SpecialTextView" parent="#android:style/Widget.TextView">
<item name="android:minHeight">70sp</item> <!-- use dp instead of sp --
</style>
Then simply set the TextViewSpecial theme in the manifest to the desired activity. You can use this method to ovreride the style of what widget you want.
Just define a theme for the layout.
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>