Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8591445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:31:48+00:00 2026-06-11T23:31:48+00:00

Does anyone used SwitchPreference class from Android and knows how to set the default

  • 0

Does anyone used SwitchPreference class from Android and knows how to set the default value? I have implemented it programmatically:

SwitchPreference switch = new SwitchPreference(this);
switch.setKey("preference_my_key");
switch.setTitle(R.string.preference_title_my_title);
switch.setSummary(R.string.preference_summary_my_summary);
Boolean isChecked = Manager.myMethodIsChecked(MyActivity.this);
switch.setChecked( isChecked ); 

switch.setOnPreferenceChangeListener(new OnPreferenceChangeListener()  {                
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
    try {
            boolean selected =   Boolean.parseBoolean(newValue.toString());      
        if ( !selected ) {
            //do something
        }
    } catch (Throwable e) {
       e.printStackTrace();
    }               
   return true;
   }
});         
category.addPreference(switch);

Preferences saves all values into its XML file: app_package_name_preferences.xml. First time when app is loaded, switch has default “false ” values. But I need sometimes to make default value “true“. I tried few methods,but nothing works.

switch.setChecked( true );  
switch.setDefaultValue(true);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:31:50+00:00Added an answer on June 11, 2026 at 11:31 pm

    As I told, I write preferences programmatically:

    PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
    PreferenceCategory catView = new PreferenceCategory(this);
    catView.setTitle(R.string.preference_category_view);
    root.addPreference(catView);
    
    final SwitchPreference switchSplash= new SwitchPreference(this);
    switchSplash.setKey(PreferenceKeys.SPLASH); 
    
    //-----the above code----
    switchSplash.setChecked(false);       // LINE 1
    catView.addPreference(switchSplash);  // LINE 2
    

    While debugging I found that true value is set in LINE 1, but when I add switchSplash into catView, the values of switchSplash is reset to false, because catView sets values from preferences.xml.
    That’s why I changed all needed values into the XML

    SharedPreferences.Editor editor = root.getPreferenceManager().getSharedPreferences().edit();
    editor.putBoolean(PreferenceKeys.SPLASH, true);  
    editor.commit();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have a list of statuses corresponding to the the icons used in
Alt+Tab is used to switch a windows app to foreground, does anyone know how
Does anyone have a ballpark percentage breakdown of Windows versions currently being used out
Does anyone know of a base class that can be used to serve up
Greetings I may have imagined this but does anyone know if Last.fm previously used
Does anyone know what component is used to view HTML emails in the default
Does anyone know which resource is used for the show dialog icon in Android
Does anyone know how salt is used in the authentication process? although i have
Does anyone have a working example where protobuf-net can be used as a drop-in
Does anyone know where I can find the default Silverlight Project Template used in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.