I'm trying to detect when "Go" is pressed on the android Keyboard (the last key on the bottom right).
My code works in the emulator and on my Nexus 5, but doesn't on some other Android devices.
What am I doing wrong?
editTextMain.setImeActionLabel("Go", KeyEvent.KEYCODE_ENTER);
editTextMain.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if(i==KeyEvent.KEYCODE_ENTER)
{
Intent intent = new Intent(thisActivity, ActivityRisultati.class);
startActivity(intent);
return true;
}
}
}
Try this
editText = (EditText) findViewById(R.id.edit_text);
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// do your stuff here
}
return false;
}
});
I have some code in my app which detects when any key which sends the KEYCODE_ENTER value is pressed. It detects both the Enter and Done keys on a Motorola Droid 3 slide-out keyboard. Here's my code:
private OnKeyListener enterKeyListener = new OnKeyListener()
{
public boolean onKey(View v, int k, KeyEvent e)
{
/*
* "Enter" or "Done" key was pressed
*/
if( (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (e.getAction() == KeyEvent.ACTION_UP) )
{
//
// Do some stuff ...
//
return true;
}
return false;
}
};
Note the additional check for KeyEvent.ACTION_UP. You might need that on some devices in order to prevent the listener from firing as soon as the button is pressed, and repeatedly if the button is held down. In my case, I only wanted it to take an action if the key had been pressed and then released.
Hope this helps.
Related
I want to detect when the power button has been pressed (not long press) in my app.
I'm using the following code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
Log.d("mytag", "keycode_power");
return true;
}
return super.onKeyDown(keyCode, event);
}
However, nothing prints to log. I've also tried:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
Log.d("mytag", "power off");
}
return super.dispatchKeyEvent(event);
}
but that doesn't work either.
Any suggestions?
I'm No Expert, But I Don't Think That The Power Button Counts As A Keypress
I'm working on a project to make calculations. So, I have my EditText box working, and I want to hide the softkeyboard if the user clicks the DONE (or any other kind of button, GO, NEXT, etc.) and the EditText box is empty
Like so:
EditText is empty -> user clicks button -> softkeyboard hides
I have this piece of code that I manage to write using tutorials and guides over the internet
I do know it is to manage the listener of the button in the softkeyboard
TextView.OnEditorActionListener mEditor = new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_DONE)
{
//Calculations method
}
return false;
}
};
So, my question is: How can I manage the listener when the EditText is empty and not get errors?
you can use, for example:
TextView.OnEditorActionListener mEditor = new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_DONE)
{
if (!TextUtils.isEmpty(v.getText().toString())){
// you calculations method
} else {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
return false;
}
};
Using onKeyDown with KEYCODE_MENU do nothing but it work with KEYCODE_SEARCH
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_MENU:
Toast.makeText(this, "Menu key pressed", Toast.LENGTH_SHORT).show();
return false;
case KeyEvent.KEYCODE_SEARCH:
Toast.makeText(this, "Search key pressed", Toast.LENGTH_SHORT).show();
return false;
}
return super.onKeyDown(keyCode, event);
}
I think there is something handling the menu key so it won't listen to my code
i have tried disbling onCreateOptionsMenu like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
But still won't work..
So, any ideas to make the menu button listen to onKeyDown??
This may be a bug in appcompat v22. See https://code.google.com/p/android/issues/detail?id=159795
The workaround as posted in that thread is to override dispatchKeyEvent :
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int keyCode = event.getKeyCode();
int action = event.getAction();
boolean isDown = action == 0;
if (keyCode == KeyEvent.KEYCODE_MENU) {
return isDown ? this.onKeyDown(keyCode, event) : this.onKeyUp(keyCode, event);
}
return super.dispatchKeyEvent(event);
}
Edit: Please see Upgraded to AppCompat v22.1.0 and now onKeyDown and onKeyUp are not triggered when menu key is pressed
Even in use of Setup Box and Android TV
you can use dispatchKeyEvent in Activity and after that detect any key you want
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int code = event.getKeyCode();
int action = event.getAction();
boolean isDown = action == 0;
if (code == KeyEvent.KEYCODE_MENU) {
Toast.makeText(this, "Menu Is Selected", Toast.LENGTH_SHORT).show();
}
return super.dispatchKeyEvent(event);
}
What device and OS are you on ?
You may want to update your compat library to the latest ..
23.0.0
I'm coding a music app with buttons. I want to make music sound while a button is pressed but stop it when it's released. Also I want to play the music in a constant loop without separation between loop times.
Now when I press the button music starts to play but when I release the button it stills playing untill the end of the file.
This is the code:
#Override
public boolean onTouch(View v, MotionEvent event) {
MediaPlayer do2n = MediaPlayer.create(this, R.raw.do_leg);
if(event.getAction() == MotionEvent.ACTION_DOWN) {
if(v.getId()==R.id.dor){
do2n.start();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if(v.getId()==R.id.dor){
if (do2n != null)
do2n.release();
}
}
return true;
}
Well, you have actually created 2 instances of MediaPlayer since onTouch is called twice: once for key down and once for key up. So the first time you're creating the player and then you lose the reference. The second time onTouch is called, for ACTION_UP you're creating a new MediaPlayer object, but that object is a different one than the previous created with ACTION_DOWN so calling stop on this newly created object has no effect.
So you could instantiate the player as a class variable. Something like below:
private MediaPlayer do2n;
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (v.getId() == R.id.dor) {
do2n = MediaPlayer.create(this, R.raw.do_leg);
do2n.start();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (v.getId() == R.id.dor) {
if (do2n != null) {
do2n.stop();
do2n.release();
do2n = null;
}
}
}
return true;
}
#Override
protected void onDestroy() {
super.onDestroy();
/**
* The activity may be destroyed if you receive a long phone call while
* keeping the button pressed so it's safe to do this
*/
if (do2n != null) {
do2n.stop();
do2n.release();
}
}
I have 3 EditTexts in a row but on 3 different layouts. I set them up so when i click enter,it will jump to the next EditText from that row,on the next layout.The problem is that when i click enter from the first EditText on the row,it goes to the third.From the third it goes to the second and from the second to the first.It's like i press enter on the first EditText,it goes to the second and then to the third on the same enter.
How can i stop it from jumping for example from the first EditText to the second and then the third on the same press of the enter key?
Here's some of my code :
et.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v1, int keyCode, KeyEvent KEYCODE_ENTER) {
l2.getChildAt(localizarer).requestFocus();
return true;
}
});
et2.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v2, int keyCode, KeyEvent KEYCODE_ENTER) {
l3.getChildAt(localizarer).requestFocus();
return true;
}
});
et3.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v3, int keyCode, KeyEvent KEYCODE_ENTER) {
l1.getChildAt(localizarer).requestFocus();
return true;
}
});
It's like i need some kind of break function,lol.
How can i fix this ?
Thanks and have a nice day/night !
Fixed by doing this :
et.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
l2.getChildAt(localizarer).requestFocus();
return true;
}
return false;
}
});
But now an older issue is back,it jumps to my second row of edittexts instead of going to the first child in the layout it goes to the second..
Are your edit texts defined in your xml do you have them in the corrent order there?
if that doesn't work you can use
android:nextFocusDown
to set the order
Why don't you use:
et1.requestFocus();
Instead of
l1.getChildAt(localizarer).requestFocus();