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 1092573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:42:11+00:00 2026-05-16T23:42:11+00:00

Using a simple EditTextPreference in my preferences activity: <EditTextPreference android:key=SomeKey android:title=@string/some_title android:summary=… android:numeric=integer android:maxLength=2

  • 0

Using a simple EditTextPreference in my preferences activity:

<EditTextPreference
    android:key="SomeKey"
    android:title="@string/some_title"
    android:summary="..."
    android:numeric="integer"
    android:maxLength="2"
/>

Is there a way that this configuration value would be saved as integer? Seems now it just allows to enter numbers, but the value is still saved as string:

Calling:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int value = preferences.getInt("SomeKey", -1);

throws me java.lang.ClassCastException: java.lang.String, and:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String value = preferences.getString("SomeKey", "-1");

retrieves the value successfully.

How to make PreferenceActivity to save value as integer by default?

  • 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-05-16T23:42:11+00:00Added an answer on May 16, 2026 at 11:42 pm

    You could extend EditTextPreference:

    public class IntEditTextPreference extends EditTextPreference {
    
        public IntEditTextPreference(Context context) {
            super(context);
        }
    
        public IntEditTextPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public IntEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected String getPersistedString(String defaultReturnValue) {
            return String.valueOf(getPersistedInt(-1));
        }
    
        @Override
        protected boolean persistString(String value) {
            return persistInt(Integer.valueOf(value));
        }
    }
    

    It would be better to overwrite onSetInitialValue() and setText() methods, but then you would have to copy some code from a base class. Above solution is simplier, but it’s quite tricky – “string” methods do something with ints. Try to not extend this class further 😉

    You could use it from XML by:

    <package.name.IntEditTextPreference
        android:key="SomeKey"
        android:title="@string/some_title"
        android:summary="..."
        android:numeric="integer"
        android:maxLength="2"
    />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using simple field width specifier, the columns are 'thinner' when the string includes 'European'
I'm using Simple 2.4.1 library in my Android project. I got this OutOfMemory error
I am using simple predicate to determine if the key should be removed. For
I'm using simple-html-dom to scrape the title off of a specified site. <?php include('simple_html_dom.php');
I am using Simple XML for XML serialization in my Android project. I have
Using simple type like class A { public int X, Y; } with object
im using simple vector push_back to object of Type A and getting this error
When using simple request to post to Twitter the messages are signed with sent
I'm using simple-html-dom to try and pull some code from another site. Here's some
I am using Simple Injector to manage the lifetime of my injected dependencies (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.