Android application style - android

I would like to set a specific style for the whole application.
I added the style in the androidmanifest.xml, and I defined the style as follow:
<style name="myStyle">
<item name="android:background">#000000</item>
<item name="android:textColor">#FFFFFF</item>
</style>
This style changes the background and the textcolor for the whole application, though I would like to change also the text color of the buttons: this sentence works when defining a button but I would like to add it to the style of the application
Thanks a lot

<style name="myStyle" parent="android:Theme">
<item name="android:buttonStyle">#style/Your button style</item>
</style>
source:How do I apply a style to all buttons of an Android application

Related

The night/theme.xml in android studio

The "colorprimary" changes in themes.xml then the button colour also changes "(image in lite mode)enter image description here (image in dark mode)enter image description hereedittext hint selection colour also changes . I want to edit text colour with out changing the theme and without changing button colour
I think, is important to mention first that a theme is different than a style. Basically, when you apply a style it only affects to the component( edit text, view, textview, etc). But when you apply a theme it affects in general terms to the app. More info about in this Link.
So, if you are applying a theme probably you are modifying some attributes that are being used in the app scope such as primaryColor, secondaryColor, etc. Probably, the editText is using primaryColor in order to set the color of the hint and when you change to another theme, primaryColor is affected, then the color hint of the editText is affected as well.
Based on the above description, we can apply multiple solutions. I would like to suggest the following.
default theme.xml
<style name="AppTheme" parent="AppTheme.Base" />
<style name="AppTheme.Base" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">#color/gray_medium_dark</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorPrimaryVariant">#color/gray_dark</item>
<item name="colorSecondary">#color/cyan</item>
<item name="editTextStyle">#style/CustomEditTextStyle</item>
</style>
nigh theme theme.xml
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowLightNavigationBar" tools:ignore="NewApi">false</item>
</style>
style.xml
<style name="CustomEditTextStyle" parent="Widget.AppCompat.EditText">
<item name="android:textSize">#dimen/text_size_16sp</item>
<item name="android:textColorHint">#color/gray_normal_light</item>
<item name="android:fontFamily">#font/roboto_condensed</item>
<item name="android:textColor">#color/colorPrimaryText</item>
<item name="android:inputType">textVisiblePassword</item>
</style>
Explanation: Another important point about style and themes is that we can apply something similar to inheritance. It means that we ca re-use some attributes or/and modify others.
So, <item name="editTextStyle">#style/CustomEditTextStyle</item> means that for all the edittext on my application I want to set up CustomEditTextStyle style. Also, in nigh theme we are applying parent as parent="AppTheme.Base" and this parent also has CustomEditTextStyle as style for edittext, so it will no change.

How to apply style to all Buttons but not to ImageButtons?

I need to apply a background color to all the buttons in my application, so I added this to styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- button styles -->
<item name="colorButtonNormal">#color/button_color</item> <!-- below api 21 -->
<item name="android:colorButtonNormal">#color/button_color</item> <!-- above api 21 -->
<item name="android:buttonStyle">#style/ButtonColor</item>
</style>
<style name="ButtonColor" parent="#style/Widget.AppCompat.Button">
<item name="android:textColor">#color/font</item>
</style>
It worked perfectly, when I add a button in XML, the button has automatically the colors I set in styles.xml
The problem is that when I add a ImageButton, the ImageButton is getting also those colors, and I don't want that. The problem is specifically with colorButtonNormal, which applyes the background color to the ImageButton also and I don't want that. How can I solve this problem?
The default style used by the ImageButton (in your case AppCompatImageButton) is defined in the app theme by the attr:
<item name="imageButtonStyle">#style/Widget.AppCompat.ImageButton</item>
In this style the background attribute <item name="background">#drawable/btn_default_material</item> is tinted with the android:tint="?attr/colorButtonNormal".
You can use a custom style starting from btn_default_material for the ImageButton or you can override the color in the layout.
Something like:
<ImageButton
android:theme="#style/MyImageButtonTheme"/>
with something like:
<style name="MyImageButtonTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="colorButtonNormal">#color/my_color</item>
</style>
Also evaluate to migrate to Material Components and the MaterialButton.
In this case it is very simple to override the color just using the styles:
<style name="MyButtonTheme" parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">#style/ButtonStylePrimaryColor</item>
</style>
<style name="ButtonStylePrimaryColor">
<item name="colorPrimary">#color/....</item>
</style>
You can do it by applying different style for defferent views
Let's say i have three style with defferent color combination and i wish to apply bigButtonTheme style for all big button
Then
i add this line on all big button android:theme="#style/bigButtonTheme">
Rest button or other vewis will remain unchanged.
You can also copy other properties of a style like global themes font background color by deffineing as parents theme
<style name="ButtonColor" parent="#style/GlobelStyleOfApp"/>

How to apply themes for an exact view?

For example when I define a style tag in XML, all views of all types get that theme. Is there any solution to difference between styles for view types? here is some code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="buttonTheme" parent="android:Theme.Holo">
<item name="android:drawableLeft">#drawable/ic_launcher</item>
</style>
<style name="textTheme" parent="android:Theme.Holo">
<item name="android:textColor">#ff0000</item>
</style>
</resources>
I think there's a small confusion in terms between a style and a theme. You are talking about defining custom styles for a widget in your application. A Theme is a collection of these styles applied globally to an Activity or Application. The custom styles you have created for a button and text view can be applied to a new custom theme so all buttons and text items share the same attributes.
I think what you are looking for is something more like this.
<style name="ApplicationTheme" parent="android:Theme.Holo">
<item name="buttonStyle">#style/MyButtonStyle</item>
<item name="textAppearance">#style/MyTextAppearance</item>
</style>
<style name="MyButtonStyle" parent="android:Widget.Button">
<item name="android:drawableLeft">#drawable/ic_launcher</item>
</style>
<style name="MyTextAppearance" parent="android:TextAppearance">
<item name="android:textColor">#ff0000</item>
</style>
Here, we have created two new styles (one for the button appearance and one for default text appearance), and then applied those as attributes in a new custom theme. You can now apply this theme in your manifest or in View constructors by referencing #style/ApplicationTheme or R.style.ApplicationTheme
You can do this by adding the attribute android:theme="" to the specific activity in your AndroidManifest.xml file
Example:
<activity android:theme="#style/CustomTheme">

Want to overload Button style

I want to overload how an android Button looks, except I want to overload only one attribute i.e. the android:background. I know I could write something like:
<style name="App_TextButtonStyle" parent="???">
<item name="android:background">#drawable/filled_roundededges_nostroke</item>
</style>
Where parent="???" specifies which style I inherit from. My question is which style should I inherit from do that I get everything from the android default style for buttons and just define a new background.
I have used style for buttons without specifying "parent" attribute
<style name="BigButtonStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:background">#drawable/backbutton</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">8pt</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_margin">10pt</item>
</style>
I think it could be enough for you to define your style without "parent".
This is mostly for future visitors, I found a solution to the problem:
<style name="App_TextButtonStyle" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/filled_roundededges_nostroke</item>
</style>
#android:style/Widget.Button references the default style of the button in the currently selected theme. You could also use the holo widget button style #android:style/Widget.Holo.Button (available since api 11).
You might want to look in <android-sdk-install-folder>/platforms/android-XX/data/res/values for the files styles.xml and themes.xml to check out all styles android uses for a given platform version.

textColor not applied when used as them

I have a style that includes textColor, textSize, textStyle and typeface. When applied directly to an EditText widget, the color of the text is as specified (as well as the other attributes), but when applied as a theme to the activity or the entire application, the size is fine but the color is not applied. What I am missing?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="fap" parent="android:Theme.Holo">
<item name="android:textSize">24sp</item>
<item name="android:textColor">#FF0000</item>
<item name="android:textStyle">normal</item>
<item name="android:typeface">normal</item>
</style>
</resources>
This is quite simple : you are not overriding android default style, your just creating a new one which extends android:Widget.EditText. Thus, the style is not applied.
To correct this, into your theme definition, just add :
<item name="android:editTextStyle">#style/fap</item>
Now, each time Android instanciate an EditText, when it load default style values, it will find your fap style.
Edit:
searching through android's source code is very usefull. Check https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/attrs.xml
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/themes.xml
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/styles.xml
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/EditText.java
for example.
EditText widget just can't get these parameters from an activity theme. It gets its default style from the android:editTextStyle parameter of the activity theme. So you have to create your own style:
<style name="MyEditText" parent="android:Widget.EditText">
<item name="android:textSize">24sp</item>
<item name="android:textColor">#FF0000</item>
<item name="android:textStyle">normal</item>
<item name="android:typeface">normal</item>
</style>
And then set it as EditText style in the activity theme:
<style name="fap" parent="android:Theme.Holo">
<item name="android:editTextStyle">#style/MyEditText</item>
</style>
Hope this will work because I haven't tried this code.

Categories

Resources