Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number Preferences in Preference Activity in Android

What I want to do is I am working on a game of life program. I want to take the time delay and make it a preference, but I want to make it available for people to type in a specific time. The number can be in miliseconds or seconds.

However I'm a little stuck on how to proceed, I haven't been able to find a simple preference that already handles this, but there might be one. Is there an easy way to make this preference and confirm that the entered data is an integer or afloat?

like image 423
Kinglink Avatar asked Jul 08 '10 18:07

Kinglink


People also ask

What is preference activity Android?

PreferencesActivity is a way to easily create preference screens such as those in Android itself, just look under Settings . These can be used inside applications to easily save preferences to SharedPreferences and then easily access these from within your app. See this page for more information on PreferenceActivity.

What are preference settings?

Preference - the basic building block that represents an individual setting. If a Preference is set to persist, it has a corresponding key-value pair that holds the user's choice for the setting that can be accessed elsewhere in the application.

How do I use PreferenceFragment?

Use PreferenceFragmentCompat to programatically handle preferences. To load the settings into the fragment, load the preferences in the onCreatePreferences() method. To get an instance of a preference, search for a preference using its key through the PreferenceManager within onCreateView() .


1 Answers

Use an EditTextPreference and set the input type to TYPE_CLASS_NUMBER. This will force the user to enter numbers and not letters.

EditTextPreference pref = (EditTextPreference)findPreference("preference_name"); pref.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER); 
like image 170
Kevin Westwood Avatar answered Sep 22 '22 14:09

Kevin Westwood