Hide toolbar (inside a AppBarLayout) in a specific fragment - android

I newly started learning android. For the sake of developing a compatible version with Android Lollipop (API 21) and also android pre-lollipop devices (API15), I define two different styles. The first style supports ActionBar and the other one extends the property .NoActionBar which is needed for API21 and above. I think that I may misunderstood the appbar with toolbar and actionbar.
Additionaly, I use two fragments in my main activity. I wonder if there is a way that toolbar (which is inside the AppBarLayout in xml file) be hidden in specific fragment and shown in the other fragment.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
...
...
</application>
</manifest>
app_bar_main.xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<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">
<com.example.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:layout_gravity="center"
android:text="#string/app_name"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
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>
<item name="windowNoTitle">true</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>

The first style supports ActionBar and the other one extends the property .NoActionBar which is needed for API21 and above.
ActionBar is no longer recommended, so you can use Toolbar everywhere. Make your activity extend AppCompatActivity and set your toolbar as Actionbar using setSupportActionBar (Toolbar toolbar)

Related

How to delete the default bar in android studio?

I am currently developing my first app in android studio and i am facing a problem that i tried to fix for a while now.
I am trying to remove the first action bar.
I tried changing the theme to a .noActionBar one but to result
Please check out the image for a visual explanation
this is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.survay_1.SecondActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_red_light"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="#+id/toolbar2"
android:background="#00a4f9"
android:visibility="visible">
</android.support.v7.widget.RecyclerView>
change your theme NoActionBar at styles.xml
<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>
also add
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
write below code in Style.xml:
<style name="LoginTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
</style>
Now u call this theme in manifest.xml with adding this line android:theme="#style/LoginTheme"
See manifest.xml look like :
<activity
android:name=".MainActivity"
android:theme="#style/LoginTheme"
android:windowSoftInputMode="stateHidden" />
getSupportActionBar().hide();
In your Activity just use this :- only one line
You can do it in two ways.
First way is to disable it in styles.xml file:
styles.xml
<style name="CustomTheme.NoActionBar" parent="CustomTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
And in your AndroidManifest.xml you can use it for all activities:
<application android:theme="#style/CustomTheme.NoActionBar">
Or for one activity only:
<activity android:theme="#style/CustomTheme.NoActionBar">
The second way is to hide action bar from your activity:
Activity.class
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
declare custom theme in your style.xml file like this..
<style name="AppThemeNoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
</style>
then in your manifest give your activity theme like this..
<activity android:name=".ActivityName"
android:theme="#style/AppThemeNoActionBar">
</activity>

colorPrimaryDark is not applying for particular activity in Status Bar

For information, I am using Android Studio 2.0 Preview 7, API 23.
Activity that is having the issue
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="in.ezyride.blovia.OfferRide"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
android:id="#+id/view">
<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" />
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/view">
<include
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/basicLayoutAddl"
layout="#layout/content_offer_ride"
/>
</ScrollView>
</RelativeLayout>
But other activities are regular. This happened when I was trying to fix the keyboard covering EditText issue. However I almost solved that, new one raised.
For fixing it, I added scroll view in the above relative layout.
The manifest part of the activity is as follows
<activity
android:name=".OfferRide"
android:label="#string/title_activity_offer_ride"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateAlwaysVisible"
android:theme="#style/AppTheme.NoActionBar" />
Other activity where things are normal.
File: 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>
<item name="md_positive_color">#color/colorPrimary</item>
<item name="md_neutral_color">#color/colorPrimary</item>
<item name="md_title_color">#color/colorPrimary</item>
<item name="md_negative_color">#color/colorPrimary</item>
<item name="md_widget_color">#color/colorPrimary</item>
</style>
<style name="FullscreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">#null</item>
<item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
<item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
<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 case, the problem was because I had two files styles.xml in my project, in both of them had the same name of theme for the activities.
Let me describe it.
This is my activity in the AndroidManifest.xml:
This is my first styles.xml (default):
This is my second styles.xml (V21):
The app in running mode used the second style, it had the problem, because we couldn't see the colorPrimaryDark of the app.
The solution was, comment the last item in the second style.xml:
http://developer.android.com/reference/android/support/design/widget/AppBarLayout.html
AppBarLayout is a vertical LinearLayout which implements many of the
features of material designs app bar concept, namely scrolling
gestures.
Children should provide their desired scrolling behavior through
setScrollFlags(int) and the associated layout xml attribute:
app:layout_scrollFlags.
This view depends heavily on being used as a direct child within a
CoordinatorLayout
Use CoordinatorLayout instead of RelativeLayout and instead of ScrollView, Use NestedScrollView.
And change this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
to:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Since you already have Toolbar, let's imagine you've already added this in your OnCreate:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
For me, the issue was windowTranslucentStatus:
Before:
<style name="MyTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:colorPrimary">#color/bright_purple</item>
<item name="android:colorPrimaryDark">#android:color/background_dark</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
After:
<style name="MyTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:colorPrimary">#color/bright_purple</item>
<item name="android:colorPrimaryDark">#android:color/background_dark</item>
</style>
Use this in your styles.xml:
<item name="android:statusBarColor">#color/colorPrimaryDarkBlue</item>

Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead

I'm having this issue when I applied the toolbar into my app and it crashed when I try to run the app.I used all previous post but no luck.
Here is my code :
Style.xml
<style name="ToolBarStyle" parent="#style/Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
I tried parent="#style/Theme.AppCompat.Light.NoActionBar" but not worked.
Toolbar.xmal
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ToolBarStyle" />
Manifest.axml
<application android:label="Capzure" android:theme="#style/ToolBarStyle" android:icon="#drawable/Icon"></application>
Thank you.
Your theme should look like this, and remove #style/:
<style name="MyMaterialTheme" parent="Theme.AppCompat.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
...
</style>
Then, do not define the theme in <application/> tag. Define it only with activity you want to use the material design theme, i.e. in <activity> tag.
If your activity extends PreferenceActivity, AppCompatActivity, or ActionBarActivity, which you want to begin the transaction with PreferenceFragment, change your theme to:
<style name="MyMaterialTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">true</item>
...
</style>
And remove this line from your Toolbar:
android:theme="#style/ToolBarStyle"

Problems with AppCompat 21 Toolbar

So I changed from using Material theme to AppCompat to support non Lollipop devices, and I encountered a couple of problems using the AppCompat toolbar.
First of all, the toolbar is not using the primary color:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"/>
Primary color is set to:
<style name="AppTheme" parent="Theme.Base"/>
<style name="Theme.Base" parent="Theme.AppCompat.Light">
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorPrimaryDark">#color/primary_dark</item>
<item name="android:colorAccent">#color/accent</item>
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
I am using this theme for my activity:
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme"
</activity>
I am adding the toolbar to the Activity like this:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
The Activity is extending ActionBarActivity
Also I noticed all the content is shifted up a bit when using the AppCompat library. Is this intentional? The menu button is displaying black, even though I chose the light theme. And the toolbar is not showing any shadow below itself, like a vanilla material one would. How can I fix those errors?
1.create theme
<style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>
<style name="AppTheme2" parent="AppTheme.Base">
<item name="colorPrimary">#color/ColorPrimary</item>
<item name="colorPrimaryDark">#color/ColorPrimaryDark</item>
</style>
2.set it to activity
<activity
android:name="OtherApp"
android:theme="#style/AppTheme2" >
</activity>
create toolbar
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ColorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:elevation="4dp">
</android.support.v7.widget.Toolbar>
and finally
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
here is my result
While using appcompact library you should not use like this android:colorPrimary, it should be colorPrimary
It seems you have problem in your style
<style name="AppTheme" parent="Theme.Base"/>
<style name="Theme.Base" parent="Theme.AppCompat.Light">
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorPrimaryDark">#color/primary_dark</item>
<item name="android:colorAccent">#color/accent</item>
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
Change it to
<style name="AppTheme" parent="Theme.Base"/>
<style name="Theme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

Show ImageView partly behind transparent ActionBar

The Google Maps application has a transparent ActionBar, through which the map is visible.
I am able to set the transparency of the ActionBar using this:
<style name="Theme.MyTheme" parent="android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
<style name="ActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#64000000</item>
</style>
But how can I show my ImageView behind the ActionBar?
You can enable overlay mode of the ActionBar. To do it you have to set (android:)windowActionBarOverlay item in the theme to true.
<style name="MyTheme" parent="Theme.Sherlock">
...
<item name="windowActionBarOverlay">true</item> <!-- for ActionBarSherlock -->
<item name="android:windowActionBarOverlay">true</item>
</style>
You can also set it at run-time:
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
This will the ActionBar a semi-transparent floating bar.
Like any requestWindowFeature..., this should be called before adding content.
After the setContentView, you can then set a background from your Drawable with this:
getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg));
Change getActionBar with getSupportActionBar for ActionBarSherlock
actionbar_bg.xml with the root element of shape:
<solid android:color="#64000000" />
Although I find Tomik's solution great, this will be useful for those one-off cases for a few activities rather than an across the board style.
If someone needs the transparent bar but only for certain activities while using a solid one in the rest of them, it might be worth creating two different styles and using the manifest to get control over it:
<style name="MyThemeOverlay" parent="Theme.Sherlock">
...
<item name="windowActionBarOverlay">true</item> <!-- for ActionBarSherlock -->
<item name="android:windowActionBarOverlay">true</item>
<!-- any stuff common here, colours, etc -->
<!-- define the style for native ActionBar for Android 4 and higher -->
<item name="android:actionBarStyle">#style/myActionbarTransparent</item>
<!-- define the style for ActionBarSherlock -->
<item name="actionBarStyle">#style/myActionbarTransparent</item>
</style>
<style name="MyThemeNoOverlay" parent="MyTheme">
<item name="windowActionBarOverlay">false</item> <!-- for ActionBarSherlock -->
<item name="android:windowActionBarOverlay">false</item>
<!-- any stuff specific for no overlay activity action bars -->
<!-- define the style for native ActionBar for Android 4 and higher -->
<item name="android:actionBarStyle">#style/myActionbar</item>
<!-- define the style for ActionBarSherlock -->
<item name="actionBarStyle">#style/myActionbar</item>
</style>
<style name="myActionbar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#color/white</item>
</style>
<style name="myActionbarTransparent" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#color/transparent</item>
</style>
and then in your AndroidManifest.xml you can either use one of them as default and the other one for some specific activities by doing something like:
<application
...
android:theme="#style/MyThemeOverlay">
...
<activity
android:name=".Activity1"
/>
<activity
android:name=".Activity2"
android:theme="#style/MyThemeNoOverlay"
/>
<activity
android:name=".Activity3"
/>
<activity
android:name=".Activity4"
android:theme="#style/MyThemeNoOverlay"
/>
...
in styles.xml:
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="colorPrimary">#color/semiTransparent5</item>
</style>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" >
<item name="windowActionBarOverlay">true</item>
</style>
in activity_main.xml:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/content_main" />

Categories

Resources