Different colorControlActivated styles in Android - android

This is my app theme:
<style name="BaseTheme" parent="Theme.AppCompat.Light">
...
<item name="colorControlActivated">#color/default_orange</item>
...
</style>
...
<style name="Switch" parent="Material.Widget.Switch">
<item name="colorControlActivated">#color/default_green</item>
</style>
And if I use the Switch style:
<com.rey.material.widget.Switch
style="#style/Switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"/>
The colorControlActivated used it's the one inside the BaseTheme (orange) instead of the Switch one (green).
Why is this happening? Can't I have different colorControlActivated for different Views?
Thanks.

The main issue is that the attribute colorControlActivated in the theme of the activity has preference to that attribute in any custom style that you define and apply to specific views.
The solution is (and this solution overrides the attribute for all elements in the same activity in the same way) to create a new theme and apply that theme to your activity in the manifest. This theme could inherit from your main theme and override only the attributes you need.
The themes:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- items-->
<item name="colorControlActivated">#android:color/white</item>
<!-- items-->
</style>
<style name="lightAppTheme" parent="AppTheme" >
<item name="colorControlActivated">#color/colorPrimary</item>
</style>
The manifest:
<application
android:name=".application.appname"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:theme="#style/lightAppTheme"
android:screenOrientation="portrait"></activity>
</application>
I hope this helps anyone that comes into this since it took me some hours to get it working.
In order to make different elements in the same activity to use different colorControlActivated atributes, go to this answer.

Related

Attributes are ignored in my app theme (style)

I have base theme
<style name="BaseAppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">#color/color_primary</item>
...
</style>
and AppTheme that inherits it
<style name="AppTheme" parent="BaseAppTheme" >
<item name="colorPrimary">#FFFF0000</item>
<item name="android:statusBarColor">#color/transparent</item>
</style>
and is used as the main theme of the app, i.e.
<application
...
android:theme="#style/AppTheme"
...
>
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme"/>
</application>
The problem is that attributes (eg <item name="colorPrimary">#FFFF0000</item> or <item name="android:statusBarColor">#color/transparent</item>, all of them) in AppTheme are ignored. However, if I add them to BaseAppTheme then they will work. Why are attributes ignored in AppTheme and how to fix it?
Do you have AppTheme only in values/styles.xml? Maybe do you have values-v21/styles.xml that contains AppTheme also so the problem is there or something like this?

setDisplayHomeAsUpEnabled using a style attribute

Is it possible when usingAppCompatActivityto specify getSupportActionBar().setDisplayHomeAsUpEnabled(true)using styles, rather than doing so programmatically?
I can specify the drawable itself, but how do I enable it using styles?
<style name="MyActivityTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarTheme">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="homeAsUpIndicator">#drawable/ic_clear</item>
<!-- Enable the above using a style attribute -->
</style>
In styles:
<style name="MyActionBarStyle" parent="Widget.AppCompat.Light.ActionBar.Solid">
<item name="displayOptions">showTitle|homeAsUp</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="actionBarStyle">#style/MyActionBarStyle</item>
</style>
Will launch activity with arrow:
Its not possible to enable it via xml. You can only set the parent activity in the manifest:
<activity
android:name=".CurrentActivity"
android:label="#string/app_name"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivityy" />
</activity>
But you can't enable it via xml. Anyways its just one line of code.

remove bar title of application from Android Studio

i would like to remove title bar on applications blue bar in android studio,i've tried using a different theme in manifest.xml
( android:theme="#style/AppTheme.NoTitleBar") but this didn't work, then, how i do to hide this title bar from this activity?
my manifest.xml is this:
and my styles.xml is this:
On the left side above the phone( layout preview ) there AppTheme text click on that and choose the theme that you want. I've highlighted it with red
Go to App>Res>Values>styles xml.
The first thing that you have after the <resources> tag is another <style> tag with the name AppTheme probably? Or something similar. If you're using a Material theme, you'd have the colorPrimary, colorPrimaryDark and accentColor declared there.
After the end of the </style> tag for this theme, create a new one with the same name but add .NoActionBar to the name, like that:
<style name="YourThemeName.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBarOverlay">true</item>
</style>
Now go to your Manifest.xml and in each <activity> tag there probably is a theme attribute. Change it so that the activity you want to not have a title bar uses the new theme we created:
android:theme="#style/YourThemeName.NoActionBar"
How your theme should look like:
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/teal</item>
<item name="colorPrimaryDark">#color/tealDark</item>
<item name="colorAccent">#color/purple700</item>
<item name="android:navigationBarColor" tools:targetApi="21">#color/teal</item>
</style>
<style name="MyTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBarOverlay">true</item>
</style>
Just remember to change the color names to the ones you have declared in your colors.xml . Now in your Manifest, the Theme for your app should be MyTheme and the theme for each Activity not using a Title bar should be MyTheme.NoActionBar
this is my Stiles.xml
<resources>>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
<style name="AppTheme.NoTitleBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBarOverlay">true</item>
</style>
and for my activity manifest.xml i do this:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main2Activity"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar" />
</application>
I am assuming you are using the latest version of Android for your app. If not please mention the version of Android that is being used. Following is the way in which you can remove the blue bar on the top of the activity:-
Go to the activity xml file and remove the following code from the xml:-
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
Now if you switch to the design mode then you will be able to see the screen without the blue bar on the top.
Hope this helps.

Hide ActionBar title just for one activity

If I apply theme for whole application it hides ActionBar title successfully:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
</style>
<style name="ActionBarStyle" parent="#android:style/Widget.Holo.Light.ActionBar.Solid">
<item name="android:displayOptions">showHome|useLogo</item>
</style>
</resources>
assign in manifest:
<application
android:name="myapp"
android:allowBackup="true"
android:icon="#drawable/action_bar_icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
I need ActionBar title hidden just in one activity.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
...other things...
</style>
<style name="MainActivityTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
</style>
<style name="ActionBarStyle" parent="#android:style/Widget.Holo.Light.ActionBar.Solid">
<item name="android:displayOptions">showHome|useLogo</item>
</style>
</resources>
setting this theme explicitly for one activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/mainLayout"
android:orientation="vertical"
android:theme="#style/MainActivityTheme">
and it still shows title in MainActivity ActionBar. I know how to hide the title in java, I need to know what I do wrong here in XML.
Set the theme on your activity in the manifest.
<activity
android:name=".MyActivity"
android:theme="#style/MainActivityTheme" />
You probably want to create a sub-theme which hides the action bar:
<style name="AppTheme.NoActionBar">
<item name="android:windowActionBar">false</item>
</style>
and then apply it to your activity in the manifest like so:
<activity android:name="NonActionBarActivity"
android:theme="#style/AppTheme.NoActionBar" />
Using the dot notation (AppTheme.NoActionBar) makes this NoActionBar theme inherit everything from AppTheme and override the setting on android:windowActionBar. If you prefer you can explicitly note the parent:
<style name="AppThemeWithoutActionBar" parent="AppTheme">
<item name="android:windowActionBar">false</item>
</style>
Also, if you're using the AppCompat libraries, you probably want to add a namespace-less version to the sub-theme.
<item name="windowActionBar">false</item>
If you want to remove actionbar title then just remove label attribute from <activity> tag in your manifest file.
Such as,
<activity
android:name=".YourActivityName"
android:label="#string/title" />
And after removing,
<activity
android:name=".YourActivityName" />
Done!

Android. My custom theme is not applied

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>`

Categories

Resources