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;
}
});
Related
I want to make in my app when I hold click it does something and when I release the button it does something else. A variable will change to true when I hold and false when I release. How do I do that
(Android Studio)
Thanks
You have to use TouchListener and MotionEvent class. The MotionEvent class has Fields ACTION_UP, ACTION_DOWN, and ACTION_MOVE. You can use the getAction() method of the MotionEvent class.
Something like this
findViewById(R.id.btn).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// button pressed
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// button relased.
}
return false;
}
});
You can use the onTouchListener for your use case:
boolean isClicking = false;
myView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// the view is pressed
isClicking = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// the view is released.
isClicking = true;
}
return false;
}
});
I have an Activity with one Fragment. This fragment has one button, and I want something to happen when I push the "enter" button on my hardware keyboard (I am testing it with adb keyevent).
I've read a lot of solutions here on stackoverflow, but none of them worked.
This is my fragment code:
btnPhoto = (Button) rootView.findViewById(R.id.taskBtnPhoto);
photoView = (ImageView) rootView.findViewById(R.id.taskPhotoView);
btnPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takePhoto();
}
});
btnPhoto.setFocusable(true);
btnPhoto.setFocusableInTouchMode(true);
btnPhoto.requestFocus();
btnPhoto.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_VOLUME_UP == keyCode) {
doFoo();
return true;
}
return false;
}
});
This code is placed in the onCreateView part of my fragment. Using debug, the event is never fired when I launch KeyEvent from ADB or use the hardware keys of my phone (volume keys).
public static class MyFragment extends Fragment {
#Override
public void onResume() {
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//YOUR CODE
return false;
}
});
}
}
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'm trying to use this code to set an "ontouchlistener". It says there are no errors in the code but when I try to run it I get a force quit... what is going wrong? any ideas?
final Handler handler = new Handler();
Runnable mLongPressed = new Runnable() {
public void run() {
}
};
#Override
public boolean onTouchEvent(MotionEvent event, View v){
if(event.getAction() == MotionEvent.ACTION_DOWN)
handler.postDelayed(mLongPressed, 1000);
if((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getAction() ==
MotionEvent.ACTION_UP))
handler.removeCallbacks(mLongPressed);
return false;
}
In OnTouchListener the event function is:
public abstract boolean onTouch (View v, MotionEvent event)
which is called when a touch event is dispatched to a view. But you are using this one?:
public boolean onTouchEvent(MotionEvent event, View v)
To implement the listener, we can easily do this:
myImgView.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
// put your code here
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
}
});