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.
Related
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
}
i am new in android testing, I try to calculate two values using Edit Text. i tried in two ways. One way is solo.enterText(EditText edittext, String text); and another one is solo,enterText(int index, String text);
This one is working for me
//Access First value (editfiled) and putting firstNumber value in it
EditText FirsteditText = (EditText) solo.getView(R.id.EditText01);
solo.enterText(FirsteditText, String.valueOf("10"));
This one is not working for me
solo.enterText(0, "10");
whats the problem here?
From the documentation of Solo.enterText
index: the index of the EditText. 0 if only one is available
You'll need to use 1 for the fist EditText if there is more than one available in your current layout.
You can also try without creating first the EditText object :
solo.enterText((EditText) solo.getView(ID),value);
EG :
solo.enterText((EditText) solo.getView(R.id.number_one_value),"13");
You have two way to input Edit Text:
By EditText id.
EditText FirsteditText = (EditText) solo.getView(R.id.EditText01);
By Index. This index is the index of editText that displays in current activity. If there is only one editText, use 0. If there are more than 1 editText, use 1, 2...
use this statement:
String.valueOf(): in -- solo.enterText(0,String.valueOf("10"));
It worked for me.
I am doing some math on the values of two EditText fields, I want to have the following validations on them:
They are not empty.
They are valid to each other(if the first field was integer the second should be the same, if the first was decimal the second should be the same).
I cannot figure out how to validate the decimal values or specifically the decimal point.
I have tried this out, but it didn't work. My app just crashes.
if (editText1.getText().toString().equals(".") || editText2.getText().toString().equals("."))
return;
For decimal value you can use this:
EditText et = (EditText) findViewById(R.id.myTextView) ;
et.setInputType(0x00002002);
It will accept only decimal values.
Another solution is here.
You can use contains() method to check if your string contains the argument you provided
if (textView1.getText().toString().contains(".") || textView2.getText().toString().contains(".")) {
Toast.makeText(getApplicationContext(), "Wrong Values", Toast.LENGTH_LONG).show();
First of all, you want to set your input type to accept decimals and/or numbers.
You can set it up in the xml layout:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal|number"
/>
Then you can just convert your EditText text to double:
double result = new Double(editText1.getText().toString());
or
double result = Double.valueOf(editText1.getText().toString());
This rocket science that you wroted is not needed anymore after that.
You can test whether something is a valid value or not by trying to parse it with Float.parseFloat and seeing if it throws a NumberFormatException (via try/catch). If it does, then you know the input is invalid.
Btw, it's nice if you provide a visual cue to the user to inform them that the input is invalid. As opposed to waiting until the user presses the "calculate" button and only tell them then.
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 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