1. Are you ready for the Galaxy S20? Here is everything we know so far!

Creating multi page preferences

Discussion in 'Android Development' started by Nabeel Rajabali, Jul 14, 2021.

  1. Nabeel Rajabali

    Thread Starter

    Hello. I'm currently following this tutorial for trying to create a settings page using the preference library that will lead to another preference screen when a preference is selected. Basically I want my settings page to lead to another advanced settings page when a preference is selected.

    What I currently have done is my settings fragment, my settings activity, the layout for my preferences and the layout for my settings activity.

    activity_settings.xml
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     xmlns:app="http://schemas.android.com/apk/res-auto"
    4.     xmlns:tools="http://schemas.android.com/tools"
    5.     android:layout_width="match_parent"
    6.     android:layout_height="match_parent"
    7.     tools:context=".SettingsActivity">
    8.  
    9.  
    10.     <!--frame layout for displaying
    11.         our preference fragment-->
    12.     <FrameLayout
    13.         android:id="@+id/idFrameLayout"
    14.         android:layout_width="match_parent"
    15.         android:layout_height="match_parent" />
    16.  
    17. </androidx.constraintlayout.widget.ConstraintLayout>
    SettingsActivity.java
    Code (Java):
    1. package com.example.navapp;
    2.  
    3. import androidx.appcompat.app.AppCompatActivity;
    4. import androidx.fragment.app.Fragment;
    5. import androidx.preference.PreferenceManager;
    6. import android.os.Bundle;
    7.  
    8.  
    9. import android.os.Bundle;
    10. import androidx.preference.Preference;
    11. import android.preference.PreferenceFragment;
    12. import android.util.Log;
    13.  
    14. import androidx.appcompat.app.AppCompatActivity;
    15. import androidx.preference.PreferenceFragmentCompat;
    16.  
    17. public class SettingsActivity extends AppCompatActivity implements
    18.         PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
    19.  
    20.  
    21.     @Override
    22.     protected void onCreate(Bundle savedInstanceState) {
    23.         super.onCreate(savedInstanceState);
    24.         setContentView(R.layout.activity_settings);
    25.  
    26.         // below line is to change
    27.         // the title of our action bar.
    28.         getSupportActionBar().setTitle("Settings");
    29.         // below line is used to check if
    30.         // frame layout is empty or not.
    31.         if (findViewById(R.id.idFrameLayout) != null) {
    32.             if (savedInstanceState != null) {
    33.                 return;
    34.             }
    35.             // below line is to inflate our fragment.
    36.             getFragmentManager().beginTransaction().add(R.id.idFrameLayout, new SettingsFragment()).commit();
    37.             PreferenceManager.
    38.  
    39.         }
    40.     }
    41.  
    42.     @Override
    43.     public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
    44.         final Bundle args = pref.getExtras();
    45.         final Fragment fragment = this.getSupportFragmentManager().getFragmentFactory().instantiate(
    46.                 this.getClassLoader(),
    47.         fragment.setArguments(args);
    48.         fragment.setTargetFragment(caller, 0);
    49.  
    50.         int setupSettingsFragment = getFragmentManager().findFragmentByTag("FeedbackFragment").getId();
    51.  
    52.         this.getSupportFragmentManager().beginTransaction()
    53.                 .replace(setupSettingsFragment, fragment)
    54.                 .addToBackStack(null)
    55.                 .commit();
    56.  
    57.         return true;
    58.     }
    59.  
    60.  
    61. }
    preferences.xml
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <PreferenceScreen
    3.     xmlns:android="http://schemas.android.com/apk/res/android">
    4.  
    5.  
    6.     <SwitchPreference
    7.         android:key="switch"
    8.         android:summaryOff="Switch off"
    9.         android:summaryOn="Switch on"
    10.         android:title="Switch Preference"
    11.         android:fragment="com.example.navapp.FeedbackFragment"/>
    12.  
    13.     <Preference
    14.         android:key="feedback"
    15.         android:title="Send feedback"
    16.         android:summary="Report technical issues or suggest new features"/>
    17.         android:fragment="com.example.navapp.FeedbackFragment"/>
    18.  
    19.  
    20. </PreferenceScreen>
    21.  
    SettingsFragment.java
    Code (Java):
    1. package com.example.navapp;
    2. import android.os.Bundle;
    3. import android.preference.PreferenceFragment;
    4.  
    5. import androidx.annotation.Nullable;
    6.  
    7. public class SettingsFragment extends PreferenceFragment {
    8.  
    9.     @Override
    10.     public void onCreate(@Nullable Bundle savedInstanceState) {
    11.         super.onCreate(savedInstanceState);
    12.  
    13.         // below line is used to add preference
    14.         // fragment from our xml folder.
    15.         addPreferencesFromResource(R.xml.preferences);
    16.     }
    17. }
    18.  
    feedback.xml


    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <PreferenceScreen
    3.     xmlns:android="http://schemas.android.com/apk/res/android">
    4.  
    5.     <Preference
    6.         android:key="feedback"
    7.         android:title="Send feedback to devs"
    8.         android:summary="Report technical issues or suggest new features"/>
    9.  
    10. </PreferenceScreen>
    11.  
    FeedbackFragment.java

    Code (Java):
    1. package com.example.navapp;
    2. import android.os.Bundle;
    3. import android.preference.PreferenceFragment;
    4.  
    5. import androidx.annotation.Nullable;
    6.  
    7. public class FeedbackFragment extends PreferenceFragment {
    8.  
    9.     @Override
    10.     public void onCreate(@Nullable Bundle savedInstanceState) {
    11.         super.onCreate(savedInstanceState);
    12.  
    13.         // below line is used to add preference
    14.         // fragment from our xml folder.
    15.         addPreferencesFromResource(R.xml.feedback);
    16.     }
    17. }



    I'm trying to use the onPreferenceStartFragment method to replace the layout with my new fragment. However, I'm currently struggling with figuring out how to attach a preference to this listener and how this code works.

    This is my first post in this forum so any feedback on how I can improve my posts would also be great.

     



    1. Download the Forums for Android™ app!


      Download

       
  2. werdersaz

    werdersaz Newbie

    i think it can work well. I am into creating and planning own IT project. Apps can be developed as an extension of your existing business or used to create new business from scratch.So I decided to get help with insurance software development from experienced web developers.
     
    #2 werdersaz, Jul 19, 2021
    Last edited: Jul 23, 2021
Loading...
Similar Threads - Creating multi page
  1. mysorian
    Replies:
    0
    Views:
    212
  2. mysorian
    Replies:
    0
    Views:
    288
  3. Kuldip Somwanshi
    Replies:
    0
    Views:
    320
  4. Davide Vergnani
    Replies:
    0
    Views:
    315
  5. xpressive
    Replies:
    0
    Views:
    505
  6. androinewbi
    Replies:
    6
    Views:
    969
  7. MackOS
    Replies:
    0
    Views:
    437
  8. Youssef Hammad
    Replies:
    0
    Views:
    1,164
  9. Murphler
    Replies:
    0
    Views:
    754
  10. bin_per
    Replies:
    0
    Views:
    1,333

Share This Page

Loading...