Check whether this is the last EditText in a LinearLayout? - android

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.

Related

Is it possible to do TextView/EditView with permanent copy/paste cursors

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.

Finding Id when using array for EditText

I would like to create an array of EditText for android but it seems that finding the id has been a very challenging task.Can someone reach out?
My code for the array:
EdiTText[] mEditText = new EditText[20];
for(int i =0;i<mEditText.length;i++){
mEditText[i] = (EditText) findViewById(i);
}
You cannot set id to view in runtime. Read more about android id here.
Just use constructor to initialize EditText:
mEditText[i] = new EditText(this);
There's two approaches to UI elements like EditText:
a. Implement them in your layout xml like this:
<EditText android:id="#+id/myedittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Here the EditText would have the id "myedittext". You can then do stuff with that EditText by referencing it from your code with
EditText et = (EditText)findViewById(R.id.myedittext);
b. Programmatically create them like
EditText et = new EditText(context)
With this approach you will also have to manually add this EditText to your UI / layout.
You mixed those two approaches in a way that won't work.
If you don't know beforehand how many UI elements (in your case EditTexts) you need, it's cool to create them programmatically, but you should definitely read up on how to do that properly.
I'm not going to ask why you're trying to do this. However...
The resource IDs needed by findViewById will not start at zero as implied by your code snippet which uses the loop index for id.
If the EditText are created in XML, then you need the resource IDs of the form R.id.xxxx. You could create an int[] of the IDs and pass the corresponding id to findViewById.
If you create the EditText in code, then you won't have IDs, so will have to store the EditText objects at creation time.

enterText() in Robotium

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.

How to validate edittext with integers in android?

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.

Move focus from one Edit text box to another

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

Categories

Resources