Android. My custom theme is not applied - android

I have following style definitions in my res/values/style.xml file:
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:textColor">#fff</item>
<item name="android:textSize">12sp</item>
</style>
<style name="MainActivity" parent="AppTheme">
<item name="android:textColor">#f0f</item>
<item name="android:background">#color/black</item>
</style>
And following code in manifest file:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:theme="#style/MainActivity"
android:name="com.example.mygateway.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I expect that textColor #f0f will be applied to MainActivity activity, but it isn't.
Neither textColor no other items i tried are applied.
Actually the only AppTheme is applied to the activity and the whole application.
I'am a beginner in android development. What I do wrong?
Please help.
Thanks!

As official docs state:
"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="AppTheme" parent="AppBaseTheme">
<item name="android:textColor">#fff</item>
<item name="android:textSize">12sp</item>
</style>
<style name="AppTheme.MainActivity">
<item name="android:textColor">#f0f</item>
<item name="android:background">#color/black</item>
</style>
and
<activity android:theme="#style/AppTheme.MainActivity"
...>
...
</activity>`

Related

How to apply Material Theme to all buttons in application (activity)?

I'm trying to customize my Android theme, but nothing seems to work.
Activity:
public class MainActivity extends AppCompatActivity {
...
}
themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Delete" parent="Theme.MaterialComponents.Light">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryVariant">#color/primary</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/primary</item>
<item name="colorSecondaryVariant">#color/primary</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="materialButtonStyle">#style/Widget.App.Button</item>
</style>
<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">#style/ThemeOverlay.Button</item>
<item name="colorPrimary">#color/primary</item>
</style>
<style name="ThemeOverlay.Button" parent="" >
<!-- For filled buttons, your theme's colorPrimary provides the default background color of the component-->
<item name="colorPrimary">#color/primary</item>
</style>
</resources>
In layout I'm using Material Design Components for example
<com.google.android.material.button.MaterialButton
android:id="#+id/button_home_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/home_all"
app:layout_constraintStart_toStartOf="parent"/>
but the default purple_200 background color is applied to the button. Can you help me a bit? I read that there I should use Activity instead of AppCompatActivity but I can't change it as my activity is a bottom navigation activity I can't find support for onSupportNavigateUp or other realated stuff
EDIT
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="licenta.allbank">
<application
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AllBank"
android:usesCleartextTraffic="true">
<activity
android:name=".activity.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="callback"
android:scheme="https" />
</intent-filter>
</activity>
</application>
</manifest>
To use a MaterialButton you have to use a Theme.MaterialComponents.* theme in your app.
Then you can define the materialButtonStyle attribute.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
....
<item name="materialButtonStyle">#style/Widget.App.Button</item>
</style>
In this way you can customize the theme of all the buttons in your app.
To override the default background color you can use the materialThemeOverlay attribute:
<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">#style/ThemeOverlay.Button</item>
</style>
<style name="ThemeOverlay.Button" parent="" >
<!-- For filled buttons, your theme's colorPrimary provides the default background color of the component-->
<item name="colorPrimary">#color/my_color</item>
</style>
Otherwise you can use the backgroundTint attribute in your custom style:
<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
<!-- Background color -->
<item name="backgroundTint">#color/buttonBackgroundSelector</item>
</style>
Figured it out. If you're testing on your phisical device and it is set on dark mode, you should make the changes in your values-night/themes.xml file, otherwise, working on your values/themes.xml should be enough

Set ToolBar label in Manifest or activity.xml

I keep seeing my domain name in the ToolBar in the past I would just use the Manifest file with this line android:label="#string/app_name" I can accomplish this in the ActivityMain.java with title. but would prefer to use the xml and strings concept. The only thing I have done different is create a custom style in the styles.xml for some TextView widgets tools v 24.0.2 V7 24.2.0 code posted
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<!--<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Light"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>-->
<style name="MyStyle" parent="AppTheme">
<item name="android:textColor">#f10909</item>
<item name="android:textSize">20sp</item>
<item name="android:width">190dp</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.XXX.newstyletest">
<application>
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:title="what the Hell"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity" android:theme="#style/MyStyle">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Is this an issue because of the custom style of has something changed ?
or what the heck am I doing wrong
You're going to scream when you SEE what is wrong if you look real close at you AndroidManifest.xml file you will see you have a situation where the code below the is UNREACHABLE because you have a closing brace behind the word application So just remove that > and life will be good

Android Material Design- ActionBar and Title visible even after setting windowActionBar as false and windowNoTitle as true

I am trying out the material design. I created a theme MyMaterialTheme in the styles.
Styles:
<resources>
<!-- Theme for the material design of the app -->
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base"></style>
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimaryApp</item>
<item name="colorPrimaryDark">#color/colorPrimaryDarkApp</item>
<item name="colorAccent">#color/colorAccentApp</item>
</style>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
In my AndroidManifest.xml, I included this theme (MyMaterialTheme). I can confirm that my theme is applied as it has taken the correct colors from it. I had assigned windowActionBar as false and windowNoTitle as true, but still I am seeing the ActionBar and the title when I run the app. Could anyone point out where I am going wrong?
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/MyMaterialTheme">
<activity
android:name=".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>
Here is the screenshot of the emulator:
Check your activity_main.xml file. Android Studio's default layouts will typically have a toolbar manually added to each layout, even if your app theme says there is none.
your base style that your material style extend it should be noTitlebar like this code
parent="Theme.AppCompat.Light.NoActionBar"

support actionbar menu styling

How does one style the Android ActionBar overflow menu (background color, text color, custom drawables etc.)?
I've been trying to do this with the following styles, but nothing seems to be taking any effect. Any help would be greatly appreciated.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:actionBarWidgetTheme">#style/MyActionBar</item>
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:textColor">#android:color/holo_blue_light</item>
<item name="android:actionBarItemBackground">#android:color/holo_green_light</item>
<item name="android:actionMenuTextColor">#android:color/holo_green_dark</item>
</style>
</resources>
TIA!
BTW minSdk set to 14
AndroidManifest is
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app1.app1">
<application android:allowBackup="true"
android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Do you call it on AndroidManifest.xml file?
Example in my style.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/green_dark</item>
<item name="android:titleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="MyTheme.ActionBar.TitleTextStyle" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/white</item>
<item name="android:textStyle">bold</item>
</style>
and in menifestfile
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher1"
android:label="#string/app_name"
android:theme="#style/CustomActionBarTheme" >
<activity.....
For implement click listener, you need create option menu for this case.

Tabs interfering with application- and activity themes

We're developing an android application that has several different tabs. We've been trying to apply a theme to entire application, and we've also tried applying a theme to specific activities. Neither has any effect.
I have this style-file:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="recapmain">
<item name="android:buttonStyle">#style/recapButton</item>
<item name="android:textViewStyle">#style/recapText</item>
</style>
<style name="recapButton" parent="android:style/Widget.Button">
<item name="android:textSize">30sp</item>
<item name="android:background">#FF0000</item>
</style>
<style name="recapText">
<item name="android:textSize">30dip</item>
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>
The application tag in the manifest file looks like this:
<application android:icon="#drawable/icon" android:label="#string/app_name" android:theme="#style/recapmain">
<activity android:name="appname"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This doesn't work. The application simply does not react to the theme. If I set a specific view to use one of the styles, it's works perfectly. So, I can do this in the xml file for an activity:
<Button
android:id="#+id/OverviewButtonYears"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/years"
android:onClick="years"
style="#style/recapButton"
/>
That will style the button appropriately.
we've also tried applying the theme to a specific activity tag, with no result.
Now, what does work, is removing the intent-filter-tag from the application tag, and putting it in an activity tag, like this:
<activity android:name=".PolicyTab" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
That starts the specific activity only, and it does have the appropriate theme.
My question then, is, why? We've gussed that it has something to do with the tabs, since starting only one activity enables the theme. What do we have to do to get it to work with tabs?
If you wany your main application theme to be android:theme="#android:style/Theme.NoTitleBar" with customized control styles, then you should extend that theme with your preferences:
<style name="recapmain" parent="#android:style/Theme.NoTitleBar">
This line will adopt the styles of the parent theme into recapmain, and inside you can write your own preferences (as you already have for buttonStyle and textViewStyle).
Inside your androidManifest.xml you set the application theme to your custom style:
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:theme="#style/recapmain">
Update
Your styles.xml should contain:
<style name="recapmain" parent="#android:style/Theme.NoTitleBar">
<item name="android:buttonStyle">#style/recapButton</item>
<item name="android:textViewStyle">#style/recapText</item>
</style>
<style name="recapButton" parent="android:style/Widget.Button">
<item name="android:textSize">30sp</item>
<item name="android:background">#FF0000</item>
</style>
<style name="recapText" parent="android:style/Widget.TextView">
<item name="android:textSize">30dip</item>
<item name="android:textColor">#FFFFFF</item>
</style>

Categories

Resources