Change default TextInputLayout style? - android

I am using Material Design Components and want to change the default style of a TextInputLayout, meaning my changes shall apply to all instances of the TextInputLayout without me having to explicitely define a style="#style/my_style" on each of them.
If and how is that possible?

You can use the textInputStyle attribute in your app theme.
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="textInputStyle">#style/myCustomStyle</item>
</style>

Related

MaterialComponents typography opacity

In MaterialComponents theming we can use attributes to either use the default styles or extend them to reflect your design guidelines, e.g.:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
...
<item name="textAppearanceCaption">#style/TextCaption</item>
...
</style>
<style name="TextCaption" parent="TextAppearance.MaterialComponents.Caption">
<item name="fontFamily">#font/inter_regular</item>
</style>
Then the framework automatically applies opacity to the text colors based on the theme, which is kinda cool, because you don't need to handle it manually. For example as far as I know the normal texts in light theme are getting 87%, the caption is getting somewhere around 40%.
But what if I'd like e.g. make the textAppearanceCaption have the same opacity as body1 app-wide without setting the color directly? Is something like this possible? How and when is this opacity applied by the framework? Is it the part of styles?

Create custom button class to set style in Android

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>

Material design components button global theme overriding broken

I want to simply apply styling to all buttons on a theme level like this
<style name="BaseTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
<item name="buttonStyle">#style/DefaultButton</item>
</style>
<style name="DefaultButton" parent="Widget.MaterialComponents.Button">
<item name="android:textColor">#color/whatever</item>
</style>
<Button
android:id="#+id/addChannelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_margin="16dp"
android:text="Add room" />
Why doesnt this work? It would in appcompat
// If I use Theme.MaterialComponents.Light.NoActionBar.Bridge, then it works
You should be setting materialButtonStyle instead of buttonStyle in your theme.
Use a Theme.MaterialComponents theme defining the materialButtonStyle attribute.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
....
<item name="materialButtonStyle">#style/MyButtonTheme</item>
</style>
In this way you can customize the theme of all the buttons in your app.
Also starting from version 1.1.0 of the material library you can override the theme attributes from the default style using the materialThemeOverlay attribute.
Something like:
<style name="MyButtonTheme" parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">#style/ButtonStyleTextColor</item>
</style>
<style name="ButtonStyleTextColor">
<!-- For filled buttons, your theme's colorPrimary provides the default background color of the component, and -->
<!--the text color is colorOnPrimary -->
<item name="colorOnPrimary">#color/my_color</item>
</style>
Currently it requires version 1.1.0 of material components for android library.
Can you try with v1.0.0-alpha06? There has been some progress since v1.0.0 which may have addressed what you're seeing.
1.1.0-alpha02
Shape Theming: FloatingActionButton, MaterialButton, Chip, MaterialCardView, BottomSheet, & TextInputLayout updated to use the new Material shape system
1.1.0-alpha06
Implement Shapeable interface in MaterialButton
Source: https://github.com/material-components/material-components-android/releases
Use materialButtonStyle and use MaterialButton instead of Button

Overriding the theme attribute inside of a style?

I have a project where I defined all the styles in the theme (Button style, Checkbox Style, EditText style and so on) This way I don't need to apply any style or theme in the layouts where I use those views, because they are applied automatically by my AppTheme.
Now I encountered a problem. I wanted to define the Switch style inside the theme but it should use another color for the colorControlActivated and colorControlHighlight. By default it uses the colorPrimary which I defined in the theme, but what if I want to change that.
The problem can be fixed easy with a theme overlay or a style where I override the needed attributes that I mentioned above and apply that style/theme everywhere where I use the Swtch view. But I want to know if I can avoid that and define a default style for my Switch views inside the same theme where the colorControlActivated and colorControlHighlight are already defined.
I tried several things but this looked like the one that actually might work but it does not:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorControlActivated">#color/colorPrimary</item>
<item name="colorControlHighlight">#color/colorPrimary</item>
<item name="colorSwitchThumbNormal">#color/white</item>
<item name="switchStyle">#style/SwitchStyle</item>
</style>
<style name="AppTheme.GreenControlOverlay">
<item name="colorControlActivated">#color/green</item>
<item name="colorControlHighlight">#color/green</item>
</style>
the SwitchStyle looks like this
<style name="SwitchStyle" parent="Widget.AppCompat.CompoundButton.Switch">
<item name="android:theme">#style/AppTheme.GreenControlOverlay</item>
</style>
I dont know why this is not working because if I set the android:theme inside my AppTheme directly it does override the colorControl attributes, but if you override it from a style it does not work. If I apply this GreenControlOverlay on the Switch view inside of my layout it also works.
Is it even possible to do this?
If you have any questions please don't hesitate to ask. I hope I explained my problem well.

Can i change accent color (AppCompat) programmatically?

I have:
<style name="AppTheme" parent="Theme.AppCompat">
<item name="colorAccent">#color/myColor</item>
</style>
But I want to allow the user to change the accent color. Can I do it with AppCompat?
No you can't, because the accent color is defined in the theme and themes are read-only in Android.
The only thing you can do is switch themes or set the color of each component manually.
Note: you can apply a theme to a portion of UI instead of the whole Activity in order to change the accent color (or other things) locally. To do so, you can use the android:theme attribute in your XML layout with the AppCompat library, or you can inflate a layout by providing a ContextThemeWrapper as context to your LayoutInflater.

Categories

Resources