Trying to use on action listner on a edittext using the following
editText = (EditText) findViewById(R.id.editTextnum);
editText.setOnEditorActionListener(new OnEditorActionListener()) ;
keep getting error "cannot instantiate the type textview.OnEditorActionListner
any ideas?
mark
UPDATE WORKING CODE HAS ERROR ON TOAST
editText = (EditText) findViewById(R.id.editTextnum);
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
EditText mEdit = (EditText)findViewById(R.id.editTextnum);
if(mEdit.length()==0){
Toast.makeText(this, "No Asset Number Entered", Toast.LENGTH_LONG).show();
}
else{
db.open();
Cursor c = db.getContact(mEdit.getText());
if (c.moveToFirst())
DisplayContact(c);
else
//Toast.makeText(this, "No contact found", Toast.LENGTH_LONG).show();
db.close();
}
}
return false;
}});
you haven't implemented the listener.
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
// do things
}
});
Related
This question already has answers here:
Implementing Text Watcher for EditText
(4 answers)
Closed 7 years ago.
I am designing a chat application using android studio and i am using fire-base as cloud service provider and i got stock on how to make a listener like if the user is typing on the edit-text field The value of the user on fire-base will changed into true and when the user is not typing the value will become false.? I am looking for this solution for about a week, and didn't find any answer regarding on my researches.
How to put TextWatcher?
EDIT
final Firebase firebase = new Firebase("https://my-first-sample-is.firebaseio.com/");
editText = (EditText) findViewById(R.id.editText);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new MessageAdapter(this, Message.class, R.layout.fragment_message, firebase.child("chat")));
editText.setAdapter(new RoomTypeAdapter(this, RoomType.class, R.layout.fragment_message1, firebase.child("Room-Typing")));
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Message message = new Message();
message.setMessage(editText.getText().toString());
editText.setText("");
message.setAuthor("Name");
message.setNumMessages(numMessages);
firebase.child("chat").push().setValue(message);
}
});
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
return true;
}
return false;
}
});
}
Implement the TextWatcher on your edittext like this
EditText myTextBox = (EditText) findViewById(R.id.myTextBox);
myTextBox.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// Here you can trigger your another code/function about which you are asking
}
});
Hope it will help!
you can use OnFocusChangeListener, if the textview has focus then the user is typing. for more information you can check here
http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html
I have an editText and I want to take the user to another Activity when the enter action of the keyboard is pressed. I tried adding this to the editText's xml:
android:imeOptions="actionSend"
And this is my code:
final EditText searchField = (EditText)findViewById(R.id.searchRecipe);
searchField.setImeActionLabel("Cerca", KeyEvent.KEYCODE_ENTER);
searchField.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
Intent goDetails = new Intent(RecipesList.this, RecipeDetails.class);
goDetails.putExtra("Keyword",searchField.getText());
startActivity(goDetails);
return true;
}
return false;
}
});
The enter button has its name changed but the action isn't fired. Can you help me understand where's the problem with the code?
Try this
final EditText searchField = (EditText)findViewById(R.id.searchRecipe);
searchField.setImeActionLabel("Cerca", KeyEvent.KEYCODE_ENTER);
searchField.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == KeyEvent.KEYCODE_ENTER) {
// do stuff here
}
return false;
}
});
Edited
KeyEvents can also be listened this way.
searchField.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.KEYCODE_ENTER)
// do stuff here
return false;
}
});
I have two android activities with ime_action_done.
Activity A has it on EditText view.
There - the soft keyboard "done" closes the keyboard.
private void initLayout(){
mInputText.setImeOptions(EditorInfo.IME_ACTION_DONE);
}
no onEditorAction overriding
Activity B has it on a custom view that extends TokenCompleteTextView
(com.tokenautocomplete.TokenCompleteTextView extends android.widget.MultiAutoCompleteTextView implements android.widget.TextView$OnEditorActionListener )
There the done action does nothing.
completionView.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (completionView.enoughToFilter() && (adapter != null)) {
//some logic. doesn't separate action_done from the other actions.
}
return true;
}
});
How can i make the second one close the soft keyboard as well?
mInputText.setImeOptions(EditorInfo.IME_ACTION_DONE);
sets the action to be the default for action_done (close keyboard)
in Activity B i used setOnEditorActionListener which catches the ime_action and consumes it.
so i have had to add this in Activity B:
completionView.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager imm =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(completionView.getWindowToken(), 0);
}
or like this:
completionView.setImeOptions(EditorInfo.IME_ACTION_DONE);
completionView.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//logic
}
return false;
}
});
I have a EditText. I wamt tp do something, when the user presses the Enter key while changing EditText. How can I do that?
The most simplest method:
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
Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
sample code for text watcher
your_edittext.addTextChangedListener(new InputValidator());
private class InputValidator implements TextWatcher {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
}
}
First, create an OnEditorActionListener (as a private instance variable, for example):
private TextView.OnEditorActionListener mEnterListener =
new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_DOWN) {
/* If the action is a key-up event on the return key, do something */
}
return true;
});
Then, set the listener (i.e. in your onCreate method):
EditText mEditText = (EditText) findViewById(...);
mEditText.setOnEditorActionListener(mEnterListener);
Another alternative is:
your_edittext.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
You need to use TextWatcher for this purpose. Here is an example on how to work with TextWatcher and Android textwatcher API.
your_edittext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
I search on many websites and many tutorials how can I get the data from editText and radioGroup and spinner DIRECTLY When the user input the data
I tried with this:
MyEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
{case1.setName(CaseName.getText().toString());
}
return true;
}
return false;
}
});
but it didn't take the data, I just use button to make that as shown below:
Test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.cancel1:
try
{
// 1-Name
case1.setName(CaseName.getText().toString());
}catch(Exception e){
}
break;
}
}});
Can anyone explain what shall I use to save the data in Object(case1) directly when the user input it ?
editext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// s is your text .
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
})
for Radio button
checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks, depending on whether it's now checked
if (((CheckBox) v).isChecked()) {
Toast.makeText(HelloFormStuff.this, "Selected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(HelloFormStuff.this, "Not selected", Toast.LENGTH_SHORT).show();
}
}
});