How to set range for an editText preference? [duplicate] - android

This question already has answers here:
Is there a way to define a min and max value for EditText in Android?
(29 answers)
Closed 5 years ago.
I have an editText preference in my Settings Activity.
I want to get a number from 0-99 from the user.
How can i do that?
<EditTextPreference
android:defaultValue="90"
android:key="key_a"
android:inputType="number"
android:title="#string/pref_title"/>
Using this code i have restricted the input to numbers,but i want to further restrict the user from entering more than 2 digits.
If restriction is not possible,any validation method to check if the entered input is <= 99 and if the entered input is invalid, i would like to change the value to its previously saved value.

Adding android:maxLength="2" solved it. Thanks #Suraj Nair

Related

how to Check Float value Is Null Or not? [duplicate]

This question already has answers here:
How do I check if my EditText fields are empty? [closed]
(30 answers)
Does EditText.getText() ever returns null?
(7 answers)
Closed 4 years ago.
I have some EditText which is of "numberDecimal", and One Button. When user clicks on button, data goes to Database. So how to check EditText is empty or not. I am getting error when I try to parse the EditText.
To check an edittext is empty:
EditText ed = (EditText) findViewById(R.id.ed);
age = ed.getText().toString();
if (age.matches("")) {
Toast.makeText(this, "Empty", Toast.LENGTH_SHORT).show();
return;
}
You can also use equals() instead of matches()

how to change EditText hint color in android [duplicate]

This question already has answers here:
Design Android EditText to show error message as described by google
(7 answers)
Closed 5 years ago.
I have a login
If user don't enter username or password and press "login" button, the EditText hint will warn you, like thislogin(turn red). How can I implement this?
I think you should set error instead
editText.setError(getString(R.string.error_required));
but if you want that
editText.setHint()
editText.setHintTextColor
Based on the image you have posted , it is very simple to show that hint when there is no data entered in username/password fields.
if(username.isEmpty())
{
usernameEditTextView.setHint("please enter username");
usernameEditTextView.setHintTextColor(R.color.Red);
}

Android: how to parameterize which resource to use at runtime? [duplicate]

This question already has answers here:
Access resource string by string name in array
(2 answers)
Closed 7 years ago.
Think about a list of parameters and their respective intervals of discrete integer values:
a[1-N], b[1-M], c[1-K], d[1-J]
a, b, c, d are the variables while between square brackets there are intervals of their possible values.
At runtime if they are
a=1, b=2, c=3, d=5
then I'd like to get the resource with
name = R.string.string_1_2_3_5
Is it possible?
I wouldn't want to make a series of cascade switches for each variable to finally pick a resource. I know this could work but is there another way?
You can use Java reflection like in here.
If you need to retrieve strings like that more than once, in order to get fast access, you should first construct a hashtable with the field names of R.string as keys (maybe when you launch the app).

displaying float in android without adding trailing zero [duplicate]

This question already has answers here:
How to nicely format floating numbers to string without unnecessary decimal 0's
(29 answers)
Closed 9 years ago.
I've got a series of float numbers; I want to display them as they are;
but while I use String.getValueOf .0 will be added to my numbers
ie 10 is shows as 10.0
what should i do to prevent this mess ?
Try
if(stringVariable.endsWith(".0")) stringVariable = stringVariable.replace(".0" , "");
This way it will remove .0 only if the string ends with .0
Hope this helps.
You can change from float to int using Math.round()
using something like sprintf would do it
String.format("%.0f", value);

How can I check an EditTextPreference value before the User validate it?

I have currently a Preference in my application where the user is prompted to enter between 2 and 10 numerical values.
As this feature is only available for power users and beta testers, not for public release, I decided to let them enter a CSV value.
So, in my EditTextPreference, some users will enter: "1;20;30", some other will enter "1;10;10;10;10;10;10;10" etc...
After, in my code, I am just splitting these values to build an array and execute my code.
I have to admit that's not very professional, but that just works!
String[] patternString = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("vibPattern", "0;500;500;500;500;500;500;500").split(";");
The main issue is that I would like to check the validity of the String while the user is writing it!
Does some of you have an idea how to achieve that?
you need to use "addTextChangedListener" and act according to the text that was , well, changed...
if you wish, you can use regular expressions in order to check for validity .
why not just programmatically add the amount of wished boxes, (for an example) have a spinner(drop down menu), from where you can choose the amount, and then add that amount of edittext with numerical only?
In the EditText entry in your layout add the following:
android:inputType="number"
android:digits="0123456789,"
Then on the addTextChangeListener implementations you only have to check if a user just entered a bunch of commas.

Categories

Resources