2

So I'm in the process of creating an app which utilises a Navigation Drawer which is split into separate fragments to access each of the tabs. Now, the thing I would like to find out how to do, is to equally have a Settings/Preferences Fragment or Activity (unsure which one would be appropriate in this context) to open once the user selects the "Settings" tab within the Navigation Drawer. I've looked all over the internet for a CLEAR explanation on how to do this, as I'm not an expert on app development.

Below is a picture of the Navigation Drawer: Navigation Drawer

If any layout or class code is required, I am happy to provide it. Any help is much appreciated!

Jaydeep Devda
  • 727
  • 4
  • 18
AndroidDevBro
  • 151
  • 1
  • 3
  • 13
  • You should create a Setting activity then add/replace `PreferenceFragment` – tahsinRupam Jul 06 '17 at 09:37
  • Do you want the PreferenceFragment to open literally within the drawer? Or just from the drawer into a new Activity? Or do you want the Home fragment etc to be replaced by the Settings page? If so, why? – MSpeed Jul 06 '17 at 09:38
  • @tahsinRupam Do you mean the Settings Activity template in the Gallery when creating a new activity? – AndroidDevBro Jul 06 '17 at 10:57
  • @billynomates Yes, just like when "Home" or "Account" are selected, new fragments with unique layouts open, I want the PreferenceFragment to open once the user clicks upon Settings in the Navigation Drawer (pretty much like 90% of apps in the Google Play store are setup). Any ideas? – AndroidDevBro Jul 06 '17 at 10:59
  • Can you show us your Adapter? It should be the same really – MSpeed Jul 06 '17 at 11:05
  • @Programmer101 Yeah, you can use that too. – tahsinRupam Jul 06 '17 at 11:08
  • @billynomates What do you mean by an Adapter? Apologies in advance for my ignorance towards terminology... – AndroidDevBro Jul 06 '17 at 11:23
  • @tahsinRupam Ok, but how would I link the fragment within the navigation drawer to that activity? – AndroidDevBro Jul 06 '17 at 11:25

1 Answers1

2

You can use the PreferenceFragmentCompat, for example:

First you create a Fragment who extends PreferenceFragmentCompat and the layout associated under the folder res/xml

settings.xml

<android.support.v7.preference.PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.preference.PreferenceCategory
        android:key="the_key_to_retrieve_the_preference_in_code"
        android:title="the_preference_title">

        <android.support.v7.preference.Preference
            android:key="key"
            android:summary="subtitle"
            android:title="title" />

        <android.support.v7.preference.Preference
            android:key="key2"
            android:summary="subtitle2"
            android:title="title2" />

        <android.support.v7.preference.CheckBoxPreference
             android:key="key_for_check_box"
             android:summary="subtitle"
             android:title="title" />

    </android.support.v7.preference.PreferenceCategory>
</android.support.v7.preference.PreferenceScreen>

SettingsFragment.java

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.settings);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
       // To get a preference
       PreferenceScreen preferenceScreen = getPreferenceScreen();
       Preference preference = preferenceScreen.findPreference("preference_ key_defined_in_the_xml");

     //You can set a listener
     preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                return false;
            }
        });

     //change title
     preference.setTitle("my_title");

     // etc
    }
}

Then you create a Activity who will hold the fragment:

SettingsActivity.java

public class SettingsActivity extends AppCompatActivity {
    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);
        setActionBarTitle("Settings");
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }
} 

settings_activity.xml

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.AppBarLayout>

    <fragment
        android:id="@+id/settings_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"        
        android:name="package.name.SettingsFragment"/>

</android.support.design.widget.CoordinatorLayout>

Be sure to add the new Settings activity to your manifest

AndroidManifest.xml

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

    <application
            android:name=".MyApplication"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
        <activity
                android:name=".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>

        <!-- add the below line to your AndroidManifest.xml -->
        <activity android:name=".SettingsActivity"></activity> 

    </application>

</manifest>

That it , then when you click on the item in your NavigationDrawer you set a new Intent and call it:

Intent intent = new Intent(this /*or the actual context*/, SettingsActivity.class);
startActivity(intent);

Hope this helps. Sorry for my english.

lasec0203
  • 2,422
  • 1
  • 21
  • 36
Cochi
  • 2,199
  • 2
  • 12
  • 15
  • You probably have to add something like this to make the setDisplayHomeAsUpEnabled work correctly: @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: onBackPressed(); return true; } return super.onOptionsItemSelected(item); } – clic Aug 27 '18 at 15:27
  • answer works, but still curious to know the reason why Settings can't be created just like any other fragment, in this case as OP stated earlier like "Home" or "Account". Why must it need it's own activity? – lasec0203 Sep 13 '19 at 05:07
  • It's simply a choice , you can just use the fragment but your setting will be dependent of your main activity. If you want to open your setting in another activity, you will open your main activity and handle your fragment stack. With the SettingActivity you just start it anywhere. It's as you want / need. – Cochi Sep 13 '19 at 10:13
  • You can simply wrap your current SettingsFragment with a simple Fragment. – Walid Feb 04 '20 at 12:57