EditText that can regain focus - android

I want to set my editText to always focus and when I inputted something and press key enter it will clear and regain focus again and can be inputted again.

you can capture the enter key event of your EditText, pull the text from it, and then set the text to "".
Here it is;
EditText et;
ArrayList<String> input = new ArrayList<String>(); // I am not sure how
// you want to save the strings, but this is a good way.
...
#Override
public void onCreate(Bundle sIS){
et = (EditText)findViewById(R.id.et); // fill in your id here
et.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView arg0,
int actionId, KeyEvent event) {
actionId = (actionId & EditorInfo.IME_MASK_ACTION);
switch (actionId){
case EditorInfo.IME_ACTION_DONE:
case EditorInfo.IME_ACTION_GO:
case EditorInfo.IME_ACTION_NEXT:
if (etAddLike.getText().length() > 0){
input.add(et.getText().toString());
etAddLike.setText("");
}
return true;
default:
return false;
}
}
});
...
don't forget to accept answer if it works.

call edittext.requestFocus(); when you want focus on it

Related

What's the appropriate way of handling IME_ACTION_SEND in EditText?

Looking at the Android documentation here:
https://developer.android.com/training/keyboard-input/style.html,
specifically:
EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
});
This does not close the keyboard after clicking the send button. It also does not close the keyboard if handled is set to false.
What is the appropriate way to hide the keyboard in this case?
Looking at :-
how to hide keyboard after typing in EditText in android?, there seems to be a multitude of ways. Is this really the way to go in Android?

Android default button and DONE on keyboard

I have an activity with the following structure
- LinearLayout
- EditText (Username)
- EditText (Password)
- Button (Login)
I want that when the user is focused on the second EditText, instead of displaying NEXT in the keyboard, it displays "DONE" or "GO" and by pressing it, the click event of my button is raised.
Is this possible?
Yse, is possible, just use following code:
mUserID.setImeOptions(mUserID.getImeOptions()|EditorInfo.IME_ACTION_NEXT);
mUserPass.setImeOptions(mUserPass.getImeOptions()|EditorInfo.IME_ACTION_DONE);
mUserPass.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView pView, int pActionId, KeyEvent pEvent) {
if (pActionId == EditorInfo.IME_ACTION_DONE) {
// Do something
return true;
}
return false;
}
});

How do you change an EditText based on another EditText?

I have two EditTexts. Is there a way to code the program in a way that changing one EditText would change the other?
For example, if chicken is 200 calories per 100 grams, then changing the EditText of chicken to 100 will result in an auto update of 100 grams to 50 grams, without clicking anything.
You could install an OnEditorActionListener on your EditText and perform the action when the user stops typing:
myEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
myEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// call some other function to test the value and
// set the result on the other EditText
return true;
}
return false;
}
});

Android EditText : getting entered text

I want to get the after entered text.I have done using TextWatcher.
There are some issues:
For example I want to enter 32.5. In that method I want to add to SET<Product>.
Here each & every number its saving object.That means after enter 3 its add Product object into SET, then add 2 then also adding...
I want to avoid. Once I finish enter EditText,Then want to take it:
final EditText txtQty = new EditText(this);
txtQty.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Log.v("TAG", "afterTextChanged" + s.toString());
String enterdPrice = txtPrice.getText().toString();
double remainQty =0.00;
Product enterdProduct = new Product();
try{
String enteredQty = s.toString();
enterdProduct.setProductCode(txtCode.getText().toString());
enterdProduct.setPrice(Double.parseDouble(enterdPrice));
//enterdProduct.setQty(Double.parseDouble(enteredQty));
// TO-DO
if (productSet.contains(enterdProduct)) {
productSet.remove(enterdProduct);
}
productSet.add(enterdProduct);
System.out.println("SIZE --" + productSet.size());
}
catch (Exception e) {
e.printStackTrace();
}
});
Please give me idea, How we can get the EditText once enter I enetred text?
You can get entered text on pressing "Enter" button on keyboard.
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
return true;
}
return false;
}
});
Code from Android tutorial
Can't you use a separate button to add an object using the value in EditText?
Extend the EditText class and override onEndBatchEdit to implement the saving functionality after it has been edited in a 'batch' (could implement some sort of listener interface).

add functionality to normal backspace

New to programming, new to Android. have figured out dynamic view creation,deletion and id assignment, but.....
i need to know how to have the backspace work normally unless there is no text in the current edittext, in which case the current view (edittext) would be deleted and the cursor and focus would jump to the edittext before it.
Any help would be very appreciated.
thanks,
Chris
You need to use an onKeyListener for your EditText field:
EditText et = new EditText();
et.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case 82:
case 84:
return false; // menu and search buttons
case 66:
return true; // enter button
case 4:
case 67:
return false; // back and delete buttons
default:
return true;
}
};
});
Hope you were looking for something along these lines.
To put focus to another edit text use:
EditText et = new EditText();
et.requestFocus();

Categories

Resources