I want to write a input filter
such that if i say the edit text should not have a number higher than 40
you can input 1-40 , but anything else will not work
I do not want to do that with a text watcher, but an InputFilter
how can I do that ?
There's a property of EditText (in xml) called:
android:inputType="number"
You might be having a button click or something during which you would validate the EditText, there you can do something like this:
int num = Integer.parseInt(editText.getText().toString());
if(num<1 || num>40)
{
// display a message that you have to write something to EditText first
}
Related
Folks,
I need to capitalize first letter of every sentence. I followed the solution posted here
First letter capitalization for EditText
It works if I use the keyboard. However, if I use setText() to programatically add text to my EditText, first letter of sentences are not capitalized.
What am I missing? Is there a easy way to fix or do I need to write code to capitalize first letters in my string before setting it to EditText.
The only thing the inputType flag does is suggest to the input method (e.g. keyboard) what the user is attempting to enter. It has nothing to do with the internals of text editing in the EditText view itself, and input methods are not required to support this flag.
If you need to enforce sentence case, you'll need to write a method which does this for you, and run your text through this method before applying it.
You can use substring to make this
private String capSentences( final String text ) {
return text.substring( 0, 1 ).toUpperCase() + text.substring( 1 ).toLowerCase();
}
Setting inputType doesn't affect anything put into the field programmatically. Thankfully, programmatically capitalizing the first letter is pretty easy anyway.
public static String capFirstLetter(String input) {
return input.substring(0,1).toUpperCase() + input.substring(1,input.length());
}
Is there a way to have an edittext show the hint but to have a 0 value and if you click to input your value,the 0 disappears ? I'd like this as a solution to having empty editTexts that make my app crash.
How can i name my EditText with a variable ? For example : EditText cant1=(EditText) findViewById(1);
Instead of cant1 to have something like EditText cant+i=(EditText) findViewById(i);i++; I want to loop and register the EditTexts automatically but i need it to change the names.I mean,to name it cant1 then cant2 then cant3 and so on.
Thanks in advance !
In your XML add android:hint="Username" and what ever is in the brackets will show up in the edit text but won't equal anything.
And in your java you can do a null check or surround it in a try/catch to prevent app crashing:
EditText et = (EditText) findViewById(R.id.yourEditText);
if(et != null) {
//Do stuff
}else{
//Toast an error or a warning
}
or
try{
String value = et.getText();
}catch(NullPointerException e){
//Toast an error or a warning
}
Then,add 0 as hint.But,if you want both hint text and '0' at a time i think it is not possible. Try edittext.setText("0");in coding place and check it out...i am not sure it may work.
I want to have an edittext that only accepts numbers as input and when a button is clicked I want to check that the edittext has got a number inside and is not empty. Thanks.
try this in layout.xml
<EditText android:numeric="integer" ..../>
in Code
EditText mNumber = (EditText)findViewById(R.id....);
onButtonClick
if(mNumber.getText().toString().length()>0)
//logic
else
//empty Editext
Call EditText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
This will make sure that numeric virtual keypad will appear, and special filtering will be applied to only allow only numbers to be entered.
To check empty text, call
EditText.getText().length() != 0
Instead of validating you can add a (inpuType = "number") attribute in your xml file (Under the editText) after which you'll be able to add only numbers in your editText.
I have an XML layout file that contains an EditText and a Button. I would like to display the following validation message to provide feedback to the user:
You must enter 4 numbers
What's the best way to go about accomplishing this?
From my experience, this is the best way:
EditText yourEditText;
// when you detect an error:
yourEditText.setError("Input must be 4 digits and numeric");
The result is:
Also, if the input must be numeric, use android:inputType="numberSigned" in the EditText definition. That way the device won't allow the user to put non-numeric values; even better, it will show a special keyboard to do so:
On the EditText definition in the xml use android:numeric to bring up the numeric IME and use android:maxLength = "4" to limit the input to 4 digits. Use android:onClick on the button to trigger a click handler.
public void onClick(View v) {
if(mEditText.length() != 4) { // check if 4 digits
Toast.makeText(this, "Input must be 4 digits and numeric", Toast.LENGTH_SHORT).show();
}
}
I am writing a simple calculator application(using absolute layout with 3 edit boxes and some buttons), which has two inputtext boxes and a output box.
input1 = (EditText) findViewById(R.id.input1);
input2 = (EditText) findViewById(R.id.input2);
now once user enters some numerics into input1 and presses '+', now i want to shift the focus from input1 to input2. How can i do that?
I tried the below code on keypress of '+'
onClick(View arg0){
operator.setText("+");
//Move focus from input1 to input2
input1.clearFocus();
input2.setNextFocusDownId(input2.getId());
}
but this is not working.. can you please help me on this?
Well, I found the answer: we can simply call input2.requestFocus(); to change the focus.
Try this:
input1.setNextFocusDownId(input2.getId());
you are using input2, and I guess that you want to go from input1 to input2.
[By the way, you want to avoid using AbsoluteLayout - it's deprecated, and may be removed.]
Instead of using onClick, you want the action to happen in a KeyListener's onKeyDown method.
See: setKeyListener
Then you can examine the KeyEvent's key with
KeyEvent.getAction() == KeyEvent.KEYCODE_PLUS