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
Related
I am designing an application, where I have fragment with text to selection and button. And I need to transmit selected text to next fragment, when I click on Button. Also, text must be always selectable ( with two cursors for selection). I try use TextView to do it: I make my TV selectable:
tvMain.setTextIsSelectable(true);
And now I can call cursors by long pressing on TV. But these is not, what I'm expecting. Cursors must be always visible and never hiding from text. Any ideas, how to do it?
You can use EditText with null background so that it look like TextView and do something like this:
tvMain.selectAll();
You can use
EditText text = (EditText) findViewById(R.id.EditText1);
int selectionStart = text.getSelectionStart();
int selectionEnd = text.getSelectionEnd();
And then get a substring from the start to the beginning
String selectedText = text.getText().toString().substring(selectionStart, selectionEnd);
You CAN use it with TextView too.
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.
Let's say in a LinearLayout we have two EditText. Android system is somehow able to tell whether an EditText is the last EditText, because the "Enter" button of the soft keyboard for the first EditText is "Next", while the "Enter" button of the soft keyboard for the second EditText is "Done". Here is the code I use to test EditText:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout editContainer = new LinearLayout(this);
editContainer.setOrientation(LinearLayout.VERTICAL);
EditText firstEdit = new EditText(this);
firstEdit.setSingleLine(true);
EditText secondEdit = new EditText(this);
secondEdit.setSingleLine(true);
editContainer.addView(firstEdit);
editContainer.addView(secondEdit);
setContentView(editContainer);
}
Can someone tell me how I can programmatically know whether an EditText is the last one or not? Of course I can traverse the whole LinearLayout but I want to keep this approach as the last resort.
Thanks!!
The Next/Done labels are set with setImeOptions. If you want to query this value you could try getImeOptions.
Quick and easy way would be to give your view a tag of 'last'.
EditText secondEdit = new EditText(this);
secondEdit.setSingleLine(true);
secondEdit.setTag('last');
You would get this value with view.getTag()
You can give unique id's to EditTexts like:
mEditText1.setId(1);
mEditText2.setId(2);
Then you can check wheter is the focused EditText is the last one or not by comparing its id (mEditText.getId()) with total number of EditTexts.
Similarly you can use setTag() and getTag() method.
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 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.