Applying style globally Android with theme and Style inheritance - android

I am trying to create base theme for whole my app, I have created custom styles for base components like EditText, Button, TextView and wanna to set them globally in my app.
Here is my xml code.
<style name="AppThemeBase" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/base_app_color</item>
<item name="colorPrimaryDark">#color/base_app_color</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="windowActionBarOverlay">true</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:editTextStyle">#style/EditTextBaseStyle</item>
<item name="android:textViewStyle">#style/TextViewBaseStyle</item>
</style>
My custom edit text style
<style name="EditTextBaseStyle" parent="Widget.AppCompat.EditText">
<item name="android:textColor">#color/base_text_color</item>
<item name="android:textSize">#dimen/text_size_edit_text</item>
<item name="android:textColorHint">#color/base_hint_color</item>
<item name="android:layout_centerHorizontal">true</item>
<item name="android:background">#drawable/edit_text_rounded_corners_bg</item>
</style>
I also tried to use #android:style/Widget.EditText as a parent.
<style name="TextViewBaseStyle" parent="#android:style/Widget.TextView">
<item name="android:textColor">#color/base_text_color</item>
<item name="android:textSize">#dimen/size_text_base</item>
<item name="android:layout_margin">#dimen/margin_text_view_base</item>
</style>
And of course in AndroidManifest.xml file
<application
android:name=".application.QrApplication"
android:allowBackup="true"
android:icon="#drawable/ic_logo_green"
android:label="#string/app_name"
android:theme="#style/AppThemeBase">
<activity
android:name=".ui.activities.LoginActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name"
android:noHistory="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I have several questions
Why it doesn't apply style for each view (EditText and TextView), if set style manually in xml layout it works like a charm, but I cannot get it working globally
About style inheritance. There are two types of inheritance explicit or implicit or for user custom styles using . (dot) and using keyword parent for default themes, but what will be if combine this types of inheritance like this <style name="SomeStyle.ParentStyle" parent="Widget.AppCompat.Button">?
Are styles and theme applied to fragments inside activity, theoretically it should because for inflating fragment context from activity is used which is ContextWrapper ?
Please if you have any ideas what can cause such problem, help to solve it or at least share your thoughts.
Thanks.

The problem was solved
Instead of overriding
<item name="android:editTextStyle">#style/EditTextBaseStyle</item>
I have to override
<item name="editTextStyle">#style/EditTextBaseStyle</item>
To figure out what style item you need to override, follow the inheritance tree of base style/theme and you will find what is the name of style item you need to use.
BUT NOTE
Text view style should be still
<item name="android:textViewStyle">#style/TextViewBaseStyle</item>
It is weird but as is.

Related

duplicate android:theme androidmanifest

i embedding a code that contains one android:theme with my original code that also have one android:theme, is it possible to have 2 android:theme
I dont now any other way to have this 2 styles.
Androidmanifest.xml
<application
android:theme="#style/AppTheme"
android:theme="#style/AppTheme_1" />
<activity android:name="xxxxxxxxx.RegisterActivity"></activity>
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/color_primary</item>
<item name="colorPrimaryDark">#color/color_primary_dark</item>
<item name="colorAccent">#color/accent_color</item>
</style>
<style name="AppTheme_1" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
You can’t do it in this way (having 2 android:theme).
Depending on your requirements, there can be other ways to achieves.
For example, for most of your screen you want to use your own theme and for certain Activity you want this AppTheme_1, you can try this in that Activity:
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme);
}
No you can't have many themes for same app
but, I suggest do it programmatically using setTheme() method.
with that method, you can replace your themes as you need depending on your needs
you can learn more about themes and styles from here

Conflict with AppTheme my app cant use built-in actionBar but one of the library require actionBar in AppTheme

If i use AppTheme without ActionBar than i get crash when i call WeAccept Payment SDK because they require Actionbar to be available in AppTheme.
My code :
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="colorPrimary">#color/DefaultPrimary</item>
<item name="colorPrimaryDark">#color/DefaultPrimaryDark</item>
<item name="colorAccent">#color/DefaultAccent</item>
<item name="android:textColorHint">#color/DefaultPrimary</item>
<item name="android:actionButtonStyle">#style/MyActionButtonStyle</item>
<item name="android:typeface">serif</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
And in Manifest
i use android:theme="#style/AppTheme.NoActionBar"
Note : i cant display the default ActionBar in my application because if the custom design
With himanshu help i managed to get it working after creating new theme with different name in styles and adding that sdk activity in my main manifest (it seems that it will override their manifest).
<activity
android:name="com.paymob.acceptsdk.PayActivity"
android:theme="#style/AppThemePayment" />

Custom PopupMenu styles are being ignored

I've been struggling with PopupMenu styling, and after a lot of research, the process of creating a custom PopupMenu style for my theme seemed to be fairly straightfoward. For example the answer on this question seems like an easy way to change the background color of a PopupMenu. However, none of these solutions have worked for my project. So I started digging into the style sources to find out what I should actually be inheriting to style the PopupMenu in my own project.
I'm running my tests on a handset running API 19, and my base theme looks like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- ... -->
<item name="android:popupMenuStyle">#style/CustomPopupMenu</item>
</style>
<style name="CustomPopupMenu" parent="#style/Widget.AppCompat.Light.PopupMenu">
<item name="android:popupBackground">#color/retina_burning_hot_pink</item>
</style>
This is the solution that most of the accepted answers here show. But this does not style my PopupMenu. So I dug into the sources to find the geneology of my theme and see what default settings where being used. This is what I found:
<style name="Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light.DarkActionBar"/>
<style name="Base.Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light">
<style name="Base.Theme.AppCompat.Light" parent="Base.V7.Theme.AppCompat.Light"/>
And then in Base.V7.Theme.AppCompat.Light I found these default settings:
<!-- Popup Menu styles -->
<item name="popupMenuStyle">#style/Widget.AppCompat.Light.PopupMenu</item>
<item name="textAppearanceLargePopupMenu">#style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large</item>
<item name="textAppearanceSmallPopupMenu">#style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small</item>
<item name="listPopupWindowStyle">#style/Widget.AppCompat.ListPopupWindow</item>
<item name="dropDownListViewStyle">?android:attr/dropDownListViewStyle</item>
So as far as I can tell, my theme inherits from Theme.AppCompat.Light.DarkActionBar which uses a default PopupMenu style of #style/Widget.AppCompat.Light.PopupMenu. I then inherit that style to set my own PopupMenu settings. I made sure my Activity and my Application are all using the correct Theme. I'm not sure why else this just won't work. I've also tried these settings:
<item name="android:textAppearanceLargePopupMenu">#style/CustomPopupMenuLarge</item>
<item name="android:textAppearanceSmallPopupMenu">#style/CustomPopupMenuSmall</item>
<item name="android:popupWindowStyle">#style/CustomPopupMenuListWindow</item>
I provided custom styles for each one, and none of them worked. What could be overriding all my custom styles? Any help would be appreciated, because I'm stumped.
EDITS
Here's my manifest...
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blah.blah">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".Activities.MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is AppTheme.NoActionBar
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Do not specify android: in
<item name="android:popupMenuStyle">#style/CustomPopupMenu</item>.
Instend:
<item name="popupMenuStyle">#style/CustomPopupMenu</item>
I think you need to define a parent for AppCompat.NoActionBar. Try parent="Theme.AppCompat.Light.NoActionBar" and also add in <item name="android:popupMenuStyle">#style/CustomPopupMenu</item> to it.

Can I change to dark theme only for one single SearchView in the Activity?

My app has a Light Material Theme, but one section of my activity is dark, and I've placed a SearhView in it. The problem is then that the search icon hint is black-on-blue. Not pretty. So can I override the theme for this single View only?
The intuitive thing to do would be to change the style proerty. So I tried this, to no avail:
<SearchView
style="#android:style/Widget.Material.SearchView"
(...)
/>
(the normal style would be Widget.Material.Light.SearchView, I suppose)
Suggestions?
PS: I am not going down this rabbit hole
You can set it in your manifest. You can globally define a theme for the whole application but you can apply other themes for activities too.
For Application:
<application
android:name=".MyApp"
android:theme="#style/Theme.global">
For Activity:
<activity
android:name=".SomeActivity"
android:theme="#style/Theme.search">
UPDATE on comment:
Then set this in your styles.xml where you define your theme attributes like this:
<item name="android:searchViewStyle">#stye/MySearchViewStyle</item>
This is the default for base Searchview:
<style name="Base.Widget.AppCompat.SearchView" parent="android:Widget">
<item name="layout">#layout/abc_search_view</item>
<item name="queryBackground">#drawable/abc_textfield_search_material</item>
<item name="submitBackground">#drawable/abc_textfield_search_material</item>
<item name="closeIcon">#drawable/abc_ic_clear_mtrl_alpha</item>
<item name="searchIcon">#drawable/abc_ic_search_api_mtrl_alpha</item>
<item name="searchHintIcon">#drawable/abc_ic_search_api_mtrl_alpha</item>
<item name="goIcon">#drawable/abc_ic_go_search_api_mtrl_alpha</item>
<item name="voiceIcon">#drawable/abc_ic_voice_search_api_mtrl_alpha</item>
<item name="commitIcon">#drawable/abc_ic_commit_search_api_mtrl_alpha</item>
<item name="suggestionRowLayout">#layout/abc_search_dropdown_item_icons_2line</item>
</style>
You can define your style like this:
<style name="MySearchViewStyle" parent="Base.Widget.AppCompat.SearchView">
<item name="searchIcon">#drawable/my_incredable_search_icon</item>
</style>

Set custom theme in Android List Preference

In my Android app, I am using various custom themes in different activities, all of which work fine. All these themes use the same custom styles for buttons, edit controls etc.
My preferences use standard Preference activities, which I define in XML and style via the manifest.
However, when I have a preference that causes an alert dialog to get launched, for example a ListPreference, the dialog uses the standard buttons. It does seem to use my custom EditText style in the EditTextPreference... but not my background or text colour.
Anyone have any idea why this is happening ?
Some of the code:
Styles:
<style name="Theme.Prefs" parent="#android:style/Theme.Holo.Light">
<item name="android:windowBackground">#drawable/background</item>
<item name="android:buttonStyle">#style/CustomButtonStyle</item>
<item name="android:editTextStyle">#style/CustomEditTextStyle</item>
</style>
<style name="CustomButtonStyle" parent="#android:style/Widget.Holo.Button">
<item name="android:background">#drawable/custom_button</item>
<item name="android:textSize">#dimen/dialog_text_size_normal</item>
<item name="android:textColor">#color/dialog_control_colour</item>
</style>
<style name="CustomEditTextStyle" parent="#android:style/Widget.Holo.EditText">
<item name="android:background">#drawable/custom_textfield</item>
<item name="android:textColor">#color/dialog_control_colour</item>
<item name="android:textSize">#dimen/dialog_text_size_normal</item>
</style>
And in the manifest:
<activity
android:name="com.breadbun.prefs.MainPreferencesActivity"
android:theme="#style/Theme.Prefs"
android:screenOrientation="portrait">
</activity>
To customize the alertDialog you need to add the second row to your main theme, and then customize the DialogStyle the way you want it.
<style name="AppTheme" parent="android:Theme.Material">
<item name="android:alertDialogTheme">#style/DialogStyle</item>
</style>
<style name="DialogStyle" parent="android:Theme.Material.Dialog.Alert">
</style>

Categories

Resources