Froggo
Froggo

Reputation: 11

Android how to set ActionBar in just one Activity

I am new to Android and I'm facing this problem. I got MainActivity with items list, when one of them is clicked then the DetailActivity starts.

I disabled ActionBar with

<style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">

so the MainActivity no longer have actionBar. But I want this ActionBar in the DetailActivity ( i need the basic one, with the left arrow to get back to MainActivity ), so I created another style

<style name="DetailTheme" parent="android:Theme.Material.Light.DarkActionBar">

and used it in activity_detail.xml like this android:theme="@style/DetailTheme"

But it seems this is not the right way, because there is no ActionBar in that Activity .

I'm currently not sure if DetailActivity will be the only one with ActionBar, so I would like to know how to activate it on just this one Activity. What is the right solution for this ?

Upvotes: 0

Views: 2169

Answers (2)

Ben P.
Ben P.

Reputation: 54204

used it in activity_detail.xml like this android:theme="@style/DetailTheme"

My best guess is that you mean you added the android:theme attribute to the root view tag in your layout file. Maybe something like this:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:theme="@style/DetailTheme"
    ...
    />

This doesn't actually apply the theme to your Activity, it only applies the theme to your Activity's "content view". Content views don't have action bars, so those attributes of the theme will be ignored.

To apply a theme to an Activity, you have to specify it in AndroidManifest.xml

<activity
    android:name=".DetailActivity"
    android:theme="@style/DetailTheme"/>

Upvotes: 0

lasagnakid77
lasagnakid77

Reputation: 328

Not sure but maybe try assigning it to the activity in the manifest

<activity android:name=".DetailActivity"
            android:theme="@style/DetailTheme"/>

Upvotes: 1

Related Questions