Reputation: 145
I have two activities, First is simple Login page in which I disabled action bar as I don't need it there, second activity is the user feed section where I want to add action bar with text or search option and app drawer maybe. How do I add the action bar to second activity but not to first?
Upvotes: 1
Views: 3374
Reputation: 137
You can simply now use a Toolbar
<android.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:title="Hello World"
android:elevation="4dp"
android:id="@+id/my_toolbar"
/>
Then set it as an action bar in your Activity.java
file
Toolbar myToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
myToolbar = findViewById(R.id.my_toolbar);
setActionBar(myToolbar);
}
Note that the Toolbar
that is being used in the example above is android.widget.Toolbar
and not androidx.appcompat.widget.Toolbar
Upvotes: 0
Reputation: 2764
You should make app theme like this to have no actionbars by default.
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorPrimary</item>
</style>
In your AndroidManifest.xml file, in <application>
tag add this line:
android:theme="@style/AppTheme"
And if you need action bar on some activity just add this to your activity layout file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="owo.owocar.driver.rides_history_entity.ui.HistorySingleActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:background="@drawable/gradient"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:background="@drawable/gradient"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_history_single" />
</android.support.design.widget.CoordinatorLayout>
Upvotes: 1
Reputation: 449
I would use a NoActionBar style for the Apptheme:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Then in the layout of the activity where you want an action bar, you can include it:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.xxx.xxx.Activity">
<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" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/fragment" />
</android.support.design.widget.CoordinatorLayout>
This produces the bar on the top of your activity, without any buttons/text/menu. Create a toolbar inside your Activity.java:
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Hope this helps.
Upvotes: 0
Reputation: 467
You can define a different theme for the 2nd activity.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
</style>
<style name="LoginTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
</style>
Upvotes: 1