July 8, 2021
Settings Activity Preferences.xml
AndroidManifest.xml
<activity android:name=".SettingsActivity" android:parentActivityName=".MainActivity2" />
SettingsFragment.java
package com.zakasoft.newspaperbd; import android.os.Bundle; import androidx.preference.PreferenceFragmentCompat; public class SettingsFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String s) { addPreferencesFromResource(R.xml.preferences); // implement your settings here } }
SettingsActivity.java
package com.zakasoft.newspaperbd; import android.os.Bundle; import androidx.preference.PreferenceFragmentCompat; public class SettingsFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String s) { addPreferencesFromResource(R.xml.preferences); // implement your settings here } }
Get Value
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); Boolean x = prefs.getBoolean("switch_settings_key",false); Log.d("i/key", x.toString());
activity_preferences.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.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"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <TextView android:id="@+id/toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Settings" android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" android:textColor="@android:color/white" android:textSize="16sp" /> </androidx.appcompat.widget.Toolbar> </com.google.android.material.appbar.AppBarLayout> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>
preferences.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <androidx.preference.PreferenceCategory android:title="General Settings"> <androidx.preference.Preference android:key="general_settings_key" android:title="Preference Test" /> <androidx.preference.SwitchPreferenceCompat android:defaultValue="true" android:key="switch_settings_key" android:title="Switch" /> </androidx.preference.PreferenceCategory> <androidx.preference.PreferenceCategory android:title="Other"> <androidx.preference.Preference android:key="about_settings_key" android:title="About Me" /> </androidx.preference.PreferenceCategory> </androidx.preference.PreferenceScreen>
Load the fragment from main
package com.zakasoft.newspaperbd; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; import androidx.preference.PreferenceManager; import android.app.FragmentManager; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.MenuItem; import com.google.android.material.bottomnavigation.BottomNavigationMenu; import com.google.android.material.bottomnavigation.BottomNavigationView; public class MainActivity2 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); Fragment fragmenthome, fragmentsearch, fragmentsettings, fragmentmore, fragmentset; fragmenthome = new BlankFragment1(); fragmentsettings = new BlankFragment3(); fragmentmore = new BlankFragment1(); fragmentsearch = new BlankFragment2(); fragmentset = new SettingsFragment(); loadFragment(fragmenthome); BottomNavigationView bottomNavigationView; bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomNavigationView); bottomNavigationView.setOnNavigationItemSelectedListener( new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.mnuhome: loadFragment(fragmenthome);break; case R.id.mnusearch: loadFragment(fragmentsearch);break; case R.id.mnusettings: //Intent i = new Intent(getApplicationContext(), SettingsActivity.class); //startActivity(i); loadFragment(fragmentset);break; case R.id.mnumore: } return true; } }); } private void loadFragment(Fragment fragment) { // load fragment FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); //frame_container is your layout name in xml file transaction.replace(R.id.flFragment, fragment); transaction.addToBackStack(null); transaction.commit(); } }