There is my app_style.xml
<style name="EditText.LargePassword" parent="Widget.AppCompat.EditText">
<item name="android:gravity">center_horizontal</item>
<item name="android:imeOptions">flagNoExtractUi</item>
<item name="android:inputType">numberPassword</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="android:textColor">#color/white</item>
<item name="android:textColorHint">#color/white</item>
<item name="android:textSize">56sp</item>
<item name="colorControlNormal">#color/white</item>
<item name="colorControlActivated">#color/white</item>
<item name="colorControlHighlight">#color/white</item>
</style>
And part of layout:
<EditText
android:id="#+id/password_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/giant_margin"
android:layout_marginRight="#dimen/giant_margin"
android:theme="#style/EditText.LargePassword"
tools:text="12345"
tools:ignore="TextFields"
/>
As you can see, I apply EditText.LargePassword style via android:theme attribute:
There is a problem: every attribute applied, except android:gravity.
Okay, let's try with style attribute:
style="#style/EditText.LargePassword"
Boom! Indicator and cursor are gone:
But now android:gravity="center_horizontal" works.
As I read, style == theme, but style don't apply to children of View. Are there any differences? Is it a bug?
Styles and Themes whilst similar are not exactly the same. A style applies only to one specific view whereas a theme will apply to a View/ViewGroup/Activity and all of its children.
In your case you are actually mixing attributes from both styles and themes into one setting which is why it's not working properly. To get both you will need to take the three colorControl attributes and define them as a theme:
<style name="LargePasswordTheme" >
<item name="colorControlNormal">#color/white</item>
<item name="colorControlActivated">#color/white</item>
<item name="colorControlHighlight">#color/white</item>
</style>
The rest can stay as a style. Then you can add both this and your style to the EditText:
<EditText
android:id="#+id/password_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/giant_margin"
android:layout_marginRight="#dimen/giant_margin"
android:theme="#style/LargePasswordTheme"
android:style="#style/EditText.LargePassword"
tools:text="12345"
tools:ignore="TextFields"
/>
Further reading with good explanations can be found here and here.
Related
I have one button and letterspacing is not working with it. Please check my following snippet code
<style name="ButtonText_v2" parent="TextAppearance.AppCompat">
<item name="fontFamily">#font/silka_regular</item>
<item name="android:letterSpacing">0.1</item>
<item name="android:textSize">#dimen/dimen_14dp</item>
</style>
<style name="PrimaryButton_v2" parent="Widget.AppCompat.Button">
<item name="android:background">#drawable/button_selector_primary</item>
<item name="android:textColor">#color/white</item>
<item name="textAllCaps">true</item>
<item name="textAppearance">#style/ButtonText_v2</item>
</style>
layout.xml File
<Button
android:text="Button"
android:layout_height="56dp"
android:layout_width="200dp"
style="#style/PrimaryButton_v2"
android:layout_margin="15dp"
/>
You can't apply letter spacing in button/custom button because their is some limitation in button component in which you can't make text appearance and background customization and more limitation
the solution for this is instead of button you can use text view and make custom text view it will also work similar to button and customize it according to requirement.
for example :-
https://stackoverflow.com/questions/9477336/how-to-make-a-custom-textview#:~:text=Create%20a%20Custom%20View%20for%20Textview.%20Make%20the,values%20Make%20entries%20of%20all%20fonts%20in%20strings.xml
Add android:letterSpacing in PrimaryButton_v2 it will work perfectly.
<style name="ButtonText_v2" parent="TextAppearance.AppCompat">
<item name="fontFamily">#font/silka_regular</item>
<item name="android:textSize">#dimen/dimen_14dp</item>
</style>
<style name="PrimaryButton_v2" parent="Widget.AppCompat.Button">
<item name="android:background">#drawable/button_selector_primary</item>
<item name="android:textColor">#color/white</item>
<item name="textAllCaps">true</item>
<item name="textAppearance">#style/ButtonText_v2</item>
<item name="android:letterSpacing">0.1</item>
</style>
layout.xml File
<Button
android:text="Button"
android:layout_height="56dp"
android:layout_width="200dp"
style="#style/PrimaryButton_v2"
android:layout_margin="15dp"
/>
I use one of the predefined styles for MaterialButton.
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve" />
And this is the style predefined style which i used.
</style>
<style name="Widget.MaterialComponents.Button.OutlinedButton" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:paddingLeft">#dimen/mtrl_btn_padding_left</item>
<item name="android:paddingRight">#dimen/mtrl_btn_padding_right</item>
<item name="strokeColor">#color/mtrl_btn_stroke_color_selector</item>
<item name="strokeWidth">#dimen/mtrl_btn_stroke_size</item>
</style>
And this is parent style of Widget.MaterialComponents.Button.OutlinedButton
<style name="Widget.MaterialComponents.Button.TextButton" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:textColor">#color/mtrl_text_btn_text_color_selector</item>
<item name="android:paddingLeft">#dimen/mtrl_btn_text_btn_padding_left</item>
<item name="android:paddingRight">#dimen/mtrl_btn_text_btn_padding_right</item>
<item name="iconTint">#color/mtrl_text_btn_text_color_selector</item>
<item name="iconPadding">#dimen/mtrl_btn_text_btn_icon_padding</item>
<item name="backgroundTint">#color/mtrl_btn_text_btn_bg_color_selector</item>
<item name="rippleColor">#color/mtrl_btn_text_btn_ripple_color</item>
</style>
Now i tried to override one of the attributes of Widget.MaterialComponents.Button.TextButton in my styles.xml
<style name="Widget.MaterialComponents.Button.TextButton">
<item name="android:textColor">#color/colorAccent</item>
</style>
I wonder whether this is the right approach or not to override a predefined style and will style Widget.MaterialComponents.Button.OutlinedButton use #color/colorAccent as text color. I think its working but i can not be sure is there any side effect of this approach or is there a better way ?
You can use one of the following:
<style name="MyCustomStyle" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="android:textColor">#color/colorAccent</item>
</style>
<style name="Widget.MaterialComponents.Button.OutlinedButton.MyCustomStyle">
<item name="android:textColor">#color/colorAccent</item>
</style>
With these approach, you would inherit all the properties of your parent style and you would be declare only those properties that you like to change/override.
And use this style in your layout file as follow:
<com.google.android.material.button.MaterialButton
style="#style/MyCustomStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve" />
or
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton.MyCustomStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve" />
I use this in my theme now I am wondering if possible without using theme either programmatically changing colors of textinputlayout
or I can change in xml still without using theme, as I need to fetch colors dynamically and I write code for that or do databinding in xml.
<item name="colorControlActivated">#color/primaryTextColor</item>
<item name="android:textColorHint">#color/primaryTextColor</item>
<item name="android:textColor">#color/primaryTextColor</item>
<item name="android:editTextColor">#color/primaryTextColor</item>```
Yes, we can use setBackgroundColor().
But in your case I think you need to build your own style, here is an example:
Creating TextInputLayout Theme
Let's define a couple of text Styles that will be used as part of the theme. These styles correspond to the different texts used, such as the Hint and Error texts.
<style name="ErrorText" parent="TextAppearance.AppCompat">
<item name="android:textColor">#android:color/red</item>
<item name="android:textSize">16sp</item>
</style>
<style name="HintText" parent="TextAppearance.AppCompat">
<item name="android:textColor">#android:color/white</item>
<item name="android:textSize">14sp</item>
</style>
Then, we create our TextInputLayout theme, referencing the above:
<style name="TextInputLayoutAppearance" parent="Widget.Design.TextInputLayout">
<!-- reference our hint & error styles -->
<item name="hintTextAppearance">#style/HintText</item>
<item name="errorTextAppearance">#style/ErrorText</item>
<item name="android:textColor">#color/user_input_color</item>
<item name="android:textColorHint">#color/unfocused_color</item>
<item name="colorControlNormal">#color/white</item>
<item name="colorControlActivated">#color/blue</item>
<item name="colorControlHighlight">#color/green</item>
</style>
And for the last step, we build our layout.xml using the android:theme attribute with the style we defined above:
<android.support.design.widget.TextInputLayout
android:theme="#style/TextInputLayoutAppearance"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_email"/>
</android.support.design.widget.TextInputLayout>
styles.xml
<style name="Red" parent="android:TextAppearance.DeviceDefault.Medium.Inverse">
<item name="android:textColor">#f00</item>
</style>
<style name="Red.LARGE">
<item name="android:textSize">30sp</item>
</style>
When i use this;
<Button
android:id="#+id/save"
android:text="#string/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/save_marginTop"
android:gravity="center"
style="#style/Red.LARGE"/>
This button attribute only Large and doest painting to Red. Why?
Your Red.LARGE style needs to use the parent attribute. Red.LARGE will inherit Red if it is changed to:
<style name="Red.LARGE" parent="Red">
<item name="android:textSize">30sp</item>
</style>
I used to use the following style to set my preferenceCategory.
But as it looks, the background color isn't applied when running on Lollipop.
<style name="Theme.Preference.Category" parent="#android:attr/listSeparatorTextViewStyle">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#color/primary_light</item>
<item name="android:background">#color/primary_dark</item>
<item name="android:paddingTop">4dp</item>
<item name="android:paddingBottom">4dp</item>
<item name="android:paddingLeft">4dp</item>
</style>
And I set it : <item name="android:listSeparatorTextViewStyle">#style/Theme.Preference.Category</item> in my theme.
is the background attribute changed in Lollipop ?
Because of the way Preference is structured, you will need to create a layout with these properties and set the android:layout attribute in the style referenced by your themes's android:preferenceCategoryStyle.
The example code below is suitable for Material. You will need to find appropriate substitutes if you're targeting Holo or AppCompat.
res/values/themes.xml:
<style name="MyAppTheme" parent="#android:style/Theme.Material.Light.DarkActionBar">
...
<item name="android:preferenceCategoryStyle">#style/MyCategoryPreferenceStyle</item>
</style>
res/values/styles.xml
<style name="MyCategoryPreferenceStyle" parent="#android:style/Preference.Material.Category">
...
<item name="android:layout">#layout/my_category_preference</item>
</style>
You will apply whatever attributes you need in the following layout. You could also continue to use a style and specify a style attribute on the TextView element, but just make sure you're using a valid parent style.
res/layout/my_category_preference.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dip"
android:textAppearance="#android:style/TextAppearance.Material.Body2"
android:textColor="?android:attr/colorAccent"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingTop="16dip" />