I can't remove the action bar from my application - android

I am trying to remove the Action Bar from my app, but it just won't leave. Here is what I have done:
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- 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>
styles.xml v21:
<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>
</resources>
And the app theme is set to No Action Bar.
I also am doing this in the MainActivity.java class:
getActionBar().hide();
But that only removes it when I run the app. I would like it to be gone in the design panel.
How is that done?

But that only removes it when I run the app. I would like it to be
gone in the design panel.
Simply, use a theme without ActionBar on Android Studio's Preview:
I said that because you said it's working after compiling the app and your question was about:
I would like it to be gone in the design panel
Design panel!
But, this will only show you how to remove that ActionBar on Andorid Studio's Preview.
The thing is, it should be like this:
In Manifest:
<android:theme="#android:style/Theme.NoTitleBar">
And if it is still there, You should check if there is a Toolbar in your Layouts

You have missed parent attribute:
<style name="AppTheme.NoActionBar" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Similarly:
<style name="AppTheme.NoActionBar" parent="AppTheme">
<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>
And don't forget to add theme declaration in manifest using:
android:theme="#style/AppTheme.NoActionBar"
Hope this would help!!

Go to your manifest.xml on this line:
android:theme="#style/AppTheme"
delete Apptheme, than typing Theme. with a dot (.) it will show you different options of theme layouts.
follow the pictures:
This will affect only the emulator. In order to see also the effect in the activity_main.xml, just go on that tab activity and press, form the preview window, the button no the top right corner "NoActionBar" as I show in follow picture, choose form the dialog box "manifest theme" and hit OK.
this will change the preview too!
hope this will help ;)

Related

Understanding styles

I'm trying to set a custom colour for my buttons in Android application but value set for accentColor is always used as button background colour. I am trying to set a global colour for all buttons without having to specify styles or theme for each button individually.
Can you explain please how styling works in Android? I understand the basics, but overriding existing themes is never working for me :(
styles.xml
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:titleTextColor">#ffffff</item>
<item name="android:buttonStyle">#style/AppTheme.Button</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.Button" >
<item name="android:backgroundTint">#color/colorPrimaryDark</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
Short answer: Try going to the source code of how the
Theme.MaterialComponents.Light.NoActionBar is defining the style for
the buttons and override those attributes.
Android styling works by merging all the defined attributes in a theme. For example, you have an AppTheme that includes theming for every built-in view. If you create a new theme for your button and your define just a couple of attributes, what android studio does is to merge all the attributes defined in the AppTheme with your new attributes. So if you want to override the buttons, you need to go deep and find out what are exactly the attributes defined in the general AppTheme you set in your application, they may not be the same.
For example, this is how I changed the color of all the buttons in my app since they were using a global attribute for it:
In the general styles.xml file, I put:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="colorButtonNormal">?myCustomColor</item>
...
</style>
But if I wanted to change the backgroundTint attribute of my buttons, I had to do this:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="colorButtonNormal">?myCustomColor</item>
<item name="android:buttonStyle">#style/AppButtonStyle</item>
...
</style>
<style name="AppButtonStyle" parent="Widget.AppCompat.Button">
<item name="android:backgroundTint">?attr/colorAccent</item>
</style>
Also, check out this cool Medium post for another developer experience in this topic.
I managed to solve the issue. The main point to notice here is that I am using Theme.MaterialComponents and not AppCompact.
I have defined a new style and I set it to buttonStyle, which refers to style for AppCompact button. In order to set a style for the material button, I had to use "materialButtonStyle" instead of "buttonStyle".
All relevant properties that can be overridden for MaterialComponents can be found in material design documentation: https://www.material.io/develop/android/components/material-button/
So the final code looks like this:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorSecondary">#color/colorAccent</item>
<item name="floatingActionButtonStyle">#style/AppTheme.Test</item>
<item name="materialButtonStyle">#style/AppTheme.Button</item>
<!--<item name="buttonStyle">#style/Widget.MaterialComponents.Button.Icon</item>-->
</style>
<style name="AppTheme.Button" parent="#style/Widget.MaterialComponents.Button">
<item name="android:backgroundTint">#D400EB</item>
</style>
<style name="AppTheme.Test" parent="#style/Widget.MaterialComponents.FloatingActionButton">
<item name="backgroundTint">#A305B1</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>

Change background colour of Activity

I have created an Android project and included Navigation Drawer Activity.
But the backgrounds of the activities are black.
I know I can change the background colour to white using XML or Java. But every time I do that I need to change other views as well. I checked the styles file. But it doesn't contain any tag about the background colour.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<!-- 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>
Is there any solution for that? Thank you!
Extend your app Theme from Theme.AppCompat.Light
you can Extend your app Theme from Theme.AppCompat.Light or you can pass background color to the root view of you activity layout.

How to change the theme of contextual action bar

I have use toolbar in my app with style
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/holo_blue</
<item name="actionModeStyle">#style/Search.ActionMode</item>
<item name="windowActionModeOverlay">true</item>
</style>
Then I used a contextual action bar and able to change the background color.
<style name="Search.ActionMode" parent="#style/Widget.AppCompat.ActionMode">
<item name="background">#color/actionModeColor</item>
</style>
But I am unable to change the background color of popup menu.
Here is the image
And I wanted the background color of popup menu to be white. I also tried to change the theme by Widget.AppCompat.Light.ActionBar in Search.ActionMode but it does nothing.
There is an easy way to change the colors in Actionbar Use ActionBar Generator:
http://jgilfelt.github.io/android-actionbarstylegenerator/
and copy paste all file in your res folder and change your theme in Android.manifest file.
I have faced the same problem as yours. What I did is
I changed from
<!-- Base application theme. -->
<style name="Theme.Noteplus.Base.Brown" parent="Theme.AppCompat.Light.NoActionBar">
<item name="actionModeStyle">#style/Widget.ActionMode</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
to
<!-- Base application theme. -->
<style name="Theme.Noteplus.Base.Brown" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="actionModeStyle">#style/Widget.ActionMode</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
solve the problem. (Not exactly sure the reason behind)
(https://stackoverflow.com/a/50114013/72437)

Android Status Bar transparent when using AppTheme.NoActionBar

When I apply the auto-generated AppTheme.NoActionBar to my activity via android:theme like so:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mypackage">
<application
...
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme.NoActionBar" />
</application>
</manifest>
My MainActivity is rendered with a transparent status bar at the top, which eventually has a white background, and white text over it. If the device is charging, that is the only symbol to be seen.
Transparent ActionBar from AppTheme.NoActionBar
Here is my values/styles.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>
In the second style, you can see AppTheme.NoActionBar, which by default inherits AppTheme, however nowhere in either style specifies that the status bar should be transparent.
Chances are, if you've come across this issue, you have a folder called values-v21 and in it a file called styles.xml. This file defines styling for devices using API 21+ (Android 5.0+). This file will also most likely contain the following style named AppTheme.NoActionBar. Sound familiar?
values-v21/styles.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>
</resources>
AppTheme.NoActionBar was defined in the values/styles.xml provided in the question, which is still valid, however that file is only used for devices under API 21. If you look at the code, you can immediately identify the issue just by seeing the word transparent towards the very end. A little to the left and we see statusBarColor and voila. That's the issue. If you do not which to provide this styling for more up-to-date users you can simply remove that line of styling.
values-v21/styles.xml:
<resources>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>
</resources>
And here's the result of that style removal:
Good ol' opaque status bar

Android: on creating a new custom theme with styles.xml

Here's the styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<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>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
Using the style above, the home screen shows as follows in Galaxy Note II (The theme of the home activity is AppTheme here.
And for the survey making activity, the theme is set as android:theme="#style/AppTheme.AppBarOverlay" at AndroidManifest.xml. And here's the screenshot of the survey making activity.
I managed to make the menu always shown on the bottom of the screen, but I keep struggling with applying the theme colour (tones) here. How should I modify the style so that the theme of the survey making activity has the same tone as the home activity, as well as keeping the menu always shown on the bottom of the screen?

Categories

Resources