<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PreferenceFragmentCompat &#8211; MaXEster Technologies  | Technical Blog</title>
	<atom:link href="https://www.maxester.com/blog/tag/preferencefragmentcompat/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.maxester.com/blog</link>
	<description>Tutorials, Examples and Implementation code for Developers Help</description>
	<lastBuildDate>Mon, 17 Feb 2025 09:17:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.22</generator>
	<item>
		<title>Setting Preference Activity In Android Q.</title>
		<link>https://www.maxester.com/blog/2019/12/18/setting-preference-activity-in-android-q/</link>
		<comments>https://www.maxester.com/blog/2019/12/18/setting-preference-activity-in-android-q/#comments</comments>
		<pubDate>Wed, 18 Dec 2019 08:13:40 +0000</pubDate>
		<dc:creator><![CDATA[Birjesh Gupta]]></dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[#Preferences activity deprecated]]></category>
		<category><![CDATA[PreferenceFragmentCompat]]></category>

		<guid isPermaLink="false">https://www.maxester.com/blog/?p=783</guid>
		<description><![CDATA[<p>PreferenceFragmentCompat</p>
<p>The post <a rel="nofollow" href="https://www.maxester.com/blog/2019/12/18/setting-preference-activity-in-android-q/">Setting Preference Activity In Android Q.</a> appeared first on <a rel="nofollow" href="https://www.maxester.com/blog">MaXEster Technologies  | Technical Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[
<p>In this article, we are going to learn how we can use setting preference activity in Android. When we need certain settings in our app.<br>  </p>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<h2>What is Setting Preference Activity?</h2>



<p>

A&nbsp;<strong>Setting Activity</strong>&nbsp;is an activity in the android studio which makes it easy to integrate the functionality and user interface in the application.

</p>



<h2>Use of Setting Preference Activity in Android.</h2>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<p> Many times we need certain settings in our app, like setting default language, theme(light or dark), customizing notifications, etc.</p>



<p> In this tutorial, we will be creating a simple app with settings to change Image visibility. </p>



<p>But, For this, we will be using&nbsp;<strong>Shared Preferences</strong>. </p>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<p> In this article, we will be creating the first setting that is making the image visible or invisible by&nbsp;<strong>CheckBoxPreference.</strong></p>



<h2>Example For Setting Preference Activity in Android.</h2>



<h3><strong> 1:-&nbsp;Create&nbsp;a&nbsp;new&nbsp;android&nbsp;project.</strong></h3>



<p>File &#8211;&gt; New &#8211;&gt; Activity &#8211;&gt; EmptyActivity &#8211;&gt; name the project(SettingPrefrence) &#8211;&gt;finish.</p>



<h3><strong> 2:-</strong> In<strong> res/menu</strong> create a .xml file.</h3>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<p>Right-click on the menu &#8211;&gt; New &#8211;&gt; menu resource file &#8211;&gt; name the file(menu_item.xml) &#8211;&gt;ok.</p>



<h3> 3:- In <strong>menu_item.xml </strong>copy the below code.</h3>



<pre class="wp-block-code"><code>//menu_item.xml

&lt;?xml version="1.0" encoding="utf-8"?>
&lt;menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    &lt;item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="setting"
        app:showAsAction="never" />
&lt;/menu></code></pre>



<h3><strong> 4:-</strong> In <strong>MainActivity.java</strong> write the below code for creating and selection of menu items.</h3>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<p>when we click on settings then the following methods will be called.</p>



<pre class="wp-block-code"><code>@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_item, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            // launch settings activity
            startActivity(new Intent(MainActivity.this, SettingsActivity.class));
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

</code></pre>



<h3><strong> 5:-&nbsp;&nbsp;</strong>Now&nbsp;in&nbsp;<strong>activity_main.xml</strong>&nbsp;write&nbsp;below&nbsp;code&nbsp;for&nbsp;text view&nbsp;and&nbsp;image view.</h3>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<pre class="wp-block-code"><code>&lt;?xml version="1.0" encoding="utf-8"?>
&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="10dp"
    tools:context=".MainActivity">

    &lt;TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Setting Prefrence"
        android:textColor="#000000"
        android:textSize="25dp"
        android:gravity="center_horizontal"/>

    &lt;ImageView
        android:id="@+id/image"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:src="@drawable/image"
        android:layout_centerInParent="true"/>



&lt;/RelativeLayout></code></pre>



<h3><strong> 6:-</strong> Now for <strong>Setting activity.</strong></h3>



<p>Right-click on the app &#8211;&gt; new &#8211;&gt; activity &#8211;&gt; setting activity &#8211;&gt;finish.</p>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<pre class="wp-block-code"><code>public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.settings, new SettingsFragment())
                .commit();
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
        }
    }
}</code></pre>



<p>And, you will get the above code in setting activity by default.</p>



<h3><strong> </strong>7:- Now, In rex/xml/root_preferences.xml copy the below code in your file for SwitchPreferenceCompat.</h3>



<pre class="wp-block-code"><code>&lt;PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

    &lt;SwitchPreferenceCompat
        app:key="display_image"
        app:defaultValue="true"
        app:summary="Visible/Invisible Image"
        app:title="Display Image" />

&lt;/PreferenceScreen></code></pre>



<p><strong>Attributes define:-</strong></p>



<p>key = used for the id of the view.</p>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<p>defaultValue =  true/false for making switch initially on/off. By default it is true.</p>



<p>summary = to provide a short description of the switch button.</p>



<p>title = to provide title/name of the switch button.</p>



<h3><strong> 8:- </strong> In MainActivity file.</h3>



<p> Now, <strong>implements</strong> your <strong>MainActivity</strong> file with <strong>SharedPreferences.OnSharedPreferenceChangeListener</strong> and copy the below code in your file.</p>



<pre class="wp-block-code"><code>public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

    ImageView image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = findViewById(R.id.image);
        ActionBar actionBar = this.getSupportActionBar();

        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        setupSharedPreferences();
    }

    private void setImageVisible(boolean display_image) {
        if (display_image == true) {
            image.setVisibility(View.VISIBLE);
        } else {
            image.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_item, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            // launch settings activity
            startActivity(new Intent(MainActivity.this, SettingsActivity.class));
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {


        if (key.equals("display_image")) {
            setImageVisible(sharedPreferences.getBoolean("display_image",true));
        }

    }
    private void setupSharedPreferences() {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        sharedPreferences.registerOnSharedPreferenceChangeListener(this);
    }
</code></pre>



<p>Here, the description of the above methods uses in the <strong>MainActivity</strong> file.</p>



<pre class="wp-block-code"><code>private void setupSharedPreferences() {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        sharedPreferences.registerOnSharedPreferenceChangeListener(this);
    }
</code></pre>



<p>Here, the Above method is used to set the switch button preference. It set true when on and false when off.</p>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<pre class="wp-block-code"><code>@Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {


        if (key.equals("display_image")) {
            setTextVisible(sharedPreferences.getBoolean("display_image",true));
        }

    }</code></pre>



<p>Now, The above method is used to getting the preference data on switch button status changed.  <br>It gets true when on and false when off. </p>



<p>and now,</p>



<pre class="wp-block-code"><code>private void setImageVisible(boolean display_image) {
        if (display_image) {
            image.setVisibility(View.VISIBLE);
        } else {
            image.setVisibility(View.INVISIBLE);
        }
    }</code></pre>



<p>This method is used to set the visibility of the image. The image will visible when the switch button in on.</p>



<p>But,</p>



<p> The image will invisible when the switch button is off.</p>



<p>Now, you can run your project and you will get the following output.</p>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<h2>Output:-</h2>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2019/12/first-473x1024.png" alt="" class="wp-image-784" srcset="https://www.maxester.com/blog/wp-content/uploads/2019/12/first-473x1024.png 473w, https://www.maxester.com/blog/wp-content/uploads/2019/12/first-138x300.png 138w, https://www.maxester.com/blog/wp-content/uploads/2019/12/first-768x1664.png 768w, https://www.maxester.com/blog/wp-content/uploads/2019/12/first.png 1080w" sizes="(max-width: 473px) 100vw, 473px" /></figure>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2019/12/second-473x1024.png" alt="" class="wp-image-785" srcset="https://www.maxester.com/blog/wp-content/uploads/2019/12/second-473x1024.png 473w, https://www.maxester.com/blog/wp-content/uploads/2019/12/second-138x300.png 138w, https://www.maxester.com/blog/wp-content/uploads/2019/12/second-768x1664.png 768w, https://www.maxester.com/blog/wp-content/uploads/2019/12/second.png 1080w" sizes="(max-width: 473px) 100vw, 473px" /></figure>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2019/12/third-473x1024.png" alt="" class="wp-image-786" srcset="https://www.maxester.com/blog/wp-content/uploads/2019/12/third-473x1024.png 473w, https://www.maxester.com/blog/wp-content/uploads/2019/12/third-138x300.png 138w, https://www.maxester.com/blog/wp-content/uploads/2019/12/third-768x1664.png 768w, https://www.maxester.com/blog/wp-content/uploads/2019/12/third.png 1080w" sizes="(max-width: 473px) 100vw, 473px" /></figure>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2019/12/fourth-473x1024.png" alt="" class="wp-image-787" srcset="https://www.maxester.com/blog/wp-content/uploads/2019/12/fourth-473x1024.png 473w, https://www.maxester.com/blog/wp-content/uploads/2019/12/fourth-138x300.png 138w, https://www.maxester.com/blog/wp-content/uploads/2019/12/fourth-768x1664.png 768w, https://www.maxester.com/blog/wp-content/uploads/2019/12/fourth.png 1080w" sizes="(max-width: 473px) 100vw, 473px" /></figure>



<figure class="wp-block-image"><img src="https://www.maxester.com/blog/wp-content/uploads/2019/12/fifth-473x1024.png" alt="" class="wp-image-788" srcset="https://www.maxester.com/blog/wp-content/uploads/2019/12/fifth-473x1024.png 473w, https://www.maxester.com/blog/wp-content/uploads/2019/12/fifth-138x300.png 138w, https://www.maxester.com/blog/wp-content/uploads/2019/12/fifth-768x1664.png 768w, https://www.maxester.com/blog/wp-content/uploads/2019/12/fifth.png 1080w" sizes="(max-width: 473px) 100vw, 473px" /></figure>



<p>Now, when you turn on the switch button image will be visible. </p>



<center>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Fads -->
<ins class="adsbygoogle fads"
     style="display:inline-block;"
     data-ad-client="ca-pub-3804472713147276"
     data-ad-slot="1267368188"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>




<p>And, I hope this article will helpful for you.</p>



<p>Thank you.</p>
<p>The post <a rel="nofollow" href="https://www.maxester.com/blog/2019/12/18/setting-preference-activity-in-android-q/">Setting Preference Activity In Android Q.</a> appeared first on <a rel="nofollow" href="https://www.maxester.com/blog">MaXEster Technologies  | Technical Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.maxester.com/blog/2019/12/18/setting-preference-activity-in-android-q/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
