setDisplayHomeAsUpEnabled using a style attribute - android

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.

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?

Change App Bar Background Color

I'm struggling to override the default themes app bar color properly.
When I use the android:actionBarStyle it changes the color but then the title text is missing.
<resources>
<style name="ToolbarThemeStyle" parent="android:Theme.Material.Light">
<item name="android:actionBarStyle">#style/ActionBarTitle.Background</item>
</style>
<style name="ActionBarTitle.Background" parent="">
<item name="android:background">#8DC44E</item>
<item name="android:titleTextStyle">#style/ActionBarTitle.Text</item>
</style>
<style name="ActionBarTitle.Text" parent="">
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>
I've tried Replacing the Action Bar but I'm wondering if there's not a shorter and easier way.
Manifest:
<application android:label="Home Automation" android:icon="#mipmap/ic_launcher" android:theme="#android:style/Theme.Material.Light"
</application>
Activity:
[Activity(Label = "Label", MainLauncher = false, Theme = "#style/ToolbarThemeStyle", NoHistory =true)]
Thanks all for your replies. I thought I'd answer this myself whereas I've spent hours trying to figure this out.
The obvious solution was in this page under Using Custom Themes but I was getting the error "No resource found that matches the given name (android:color Primary)". Changing android:minSDKVersion=23 also did not work.
I went to Application Properties and set the "Compile using Android version" to the current targeting framework (i.e. API 23) instead of the maximum which was not installed yet set to API 25.
make sure are you setting theme to activity in menifest or xml. Here I am sharing my theme code you can refer.
<style name="MaterialAppTheme" parent="Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#color/white</item>
<item name="android:textColorSecondary">#color/red</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
This code should be in you style.xml
<resources>
<!-- 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>
And make sure you should have declared it in manifest.xml
<application
android:name=".app.AppController"
android:allowBackup="true"
android:icon="#drawable/images"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">

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"

Android Lollipop, how to change color for different UI elements (ProgressBar, Switch, TextInput)

I have a ProgressBar, Switch, and TextInput, and I need to be able to specify a different color for each one of them.
When I do this, it changes the color of all 3 items.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorAccent">#e8c4c7</item>
</style>
I'm looking for a command that only changes the color of the UI Element I want. something like:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="progressBar">#aaaaaa</item>
<item name="switch">#bbbbbb</item>
<item name="textInput">#cccccc</item>
</style>
Unfortunately, I am unable to change the style for each element directly (cause im using ReactNative). I need a solution that uses those global settings.
style.xml:
<resources>
<!-- inherit from the material theme -->
<style name="AppTheme" parent="android:Theme.Material">
<!-- Main theme colors -->
<!-- your app branding color for the app bar, also for button color-->
<item name="android:colorPrimary">#color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">#color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="android:colorAccent">#color/accent</item>
<!--progress bar theme-->
<item name="android:progressBarStyle">#style/myprogress</item>
<!--switch button theme-->
<item name="android:switchStyle">#style/myswith</item>
</style>
</resources>
<!--progress bar style,-->
<style name="myprogress" parent="Widget.AppCompat.ProgressBar.Horizontal">
<item name="android:background">#color/white_color</item>
</style>
<style name="myswitch" parent="Widget.AppCompat.CompoundButton.Switch"></style>
Manifest.xml:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".activity.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>
Sample:
Some code like this:
<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>
<item name="android:progressBarStyle">#style/progress</item>
<item name="android:switchStyle">#style/swith</item>
<item name="android:textAppearance">#style/text</item>
</style>
<style name="progress" parent="Widget.AppCompat.ProgressBar.Horizontal">
<item name="android:background"></item>
</style>
<style name="swith" parent="Widget.AppCompat.CompoundButton.Switch"></style>
<style name="text" parent="TextAppearance.AppCompat"></style>

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