0

First, I am using Jetpack Navigation Component - navigation drawer.

And "No Action Bar" is my app's main style.

    <style name="NoActionBar" parent="AppTheme">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="preferenceTheme">@style/PrefsTheme</item>
    </style>

Here is the manifest.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxx.xxx">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:name=".MainApplication"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".ui.MainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Because I use Jetpack Navigation Component, it is a single activity pattern.

In general, my UIs have a custom toolbar. It's ok.

But in case of the Settings UI, I faced a problem...

I am using Jetpack Preferences. https://developer.android.com/guide/topics/ui/settigs

My settings UI extends "PreferenceFragmentCompat" and it is defined in "nav_graph.xml", too.

So it can be accessed by below code:

    findNavController().navigate(R.id.settingsFragment)

Because my app uses "NoActionBar", the preference has no action bar too. And the preference is made by Jetpack Preferences, I cannot add a custom toolbar.

Is there any good idea/solution, please?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
yoonhok
  • 2,575
  • 2
  • 30
  • 58

2 Answers2

0

There can be a number of solutions. Create another style the enables actionbar to true.

<style name="AppTheme.WithActionBar" parent="AppTheme">
    <item name="windowActionBar">true</item>
    <item name="windowNoTitle">false</item>
    <item name="preferenceTheme">@style/PrefsTheme</item>
</style>

Then apply that theme specifically in your desired activity only inside Manifest.

<activity
        android:name="YourActivity"
        android:theme="@style/AppTheme.WithActionBar" />
touhid udoy
  • 4,005
  • 2
  • 18
  • 31
  • My preference is in the same activity... According to your opinion, I should move my preference out to another nav graph. But it doesn't look like a solution that I want. – yoonhok Oct 19 '19 at 15:42
  • 1
    @yoonhok please add some of your your activity code and how you defined your activity in manifest to better understand the problem – touhid udoy Oct 19 '19 at 15:50
  • 1
    I added my full manifest code. It has just single activity because I use Jetpack Navigation Component. – yoonhok Oct 19 '19 at 16:08
0

I'm using jetpack navigation drawer the MainActivity doesn't have Actionbar, also I have Preference with Actionbar, just i'm using AppTheme

<activity
        android:name="YourPreferenceActivity"
        android:theme="@style/AppTheme" />

Note: you can remove the theme in PreferenceActivity if your default theme is AppTheme

MainActivity with No action bar theme

<activity
            android:name="com.jca.dastuurkajsl.ui.main.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>

here is how my AppTheme looks like

<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
</style>

AppTheme.NoActionBar

<style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
Jimale Abdi
  • 2,574
  • 5
  • 26
  • 33
  • Yes, I am using Jetpack navigation component with Navigation drawer. But my preference is in the nav_graph. – yoonhok Oct 19 '19 at 16:10
  • So I think you didn't use Activity to hold the fragment of the preference – Jimale Abdi Oct 19 '19 at 16:40
  • I don't want separate activity for preference instead i want to use PreferenceFragmentCompat directly with toolbar. is it possible ? – KamDroid Aug 28 '22 at 09:39