I've implemented dispatchKeyEvent in my activity to listen to the Enter key being pressed.
The problem is that when i click enter,it calls my method twice ? How can i fix this ?
Thanks,have a nice day !
#Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
enter();
return true;
}
return super.dispatchKeyEvent(e);
};
Fixed it,done this :
At first i was doing ACTION_DOWN but that was triggering an older problem of mine.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_UP){
enter();
return true;
}}
return super.dispatchKeyEvent(event);
};
Related
The issue is im not getting the Button release Toast.
i've a simple view in xml on which im performing onTouch.
hidenBtn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
firstTime=System.currentTimeMillis();
Toast.makeText(MainActivity.this, "Pressed", Toast.LENGTH_SHORT).show();
} else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
Toast.makeText(MainActivity.this, "Released", Toast.LENGTH_SHORT).show();
secondTime=System.currentTimeMillis();
if(secondTime-firstTime>=5000){
//do your actions here,prev,curr are fields in a class
ShowDialog();
}
else{
firstTime=0;
secondTime=0;
}
}
// TODO Auto-generated method stub
return false;
}
});
Change your OnTouchListener like this:
hidenBtn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
firstTime=System.currentTimeMillis();
Toast.makeText(MainActivity.this, "Pressed", Toast.LENGTH_SHORT).show();
return true;
} else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
Toast.makeText(MainActivity.this, "Released", Toast.LENGTH_SHORT).show();
secondTime=System.currentTimeMillis();
if(secondTime-firstTime>=5000){
//do your actions here,prev,curr are fields in a class
ShowDialog();
}
else{
firstTime=0;
secondTime=0;
}
}
// TODO Auto-generated method stub
return false;
}
});
You are returning false at ACTION_DOWN which means you are not consuming your touch event, so no other steps of the same event are triggered. Therefore you need to return true in the ACTION_DOWN so you can intercept ACTION_UP.
So, In my code the user must answer a quick math question and press a button to answer but for the next question it does not check for the second answer only the first. How could you make it where it then checks for the next answer and not the previous one?
primaryText.setText(Questions[0]);
enter.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v)
{
if(numberVal.getText().toString().equals("14")) {
primaryText.setText(Questions[1]);
}
if(numberVal.getText().toString().equals("5")) {
primaryText.setText(Questions[2]);
}
}
}
);
instead of
OnClickListener
try :
MyBtn1.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_UP){
//do stuff
}
else if(event.getAction() == MotionEvent.ACTION_DOWN){
//do stuff
}
else if(event.getAction() == MotionEvent.ACTION_CANCEL){
//do stuff
}
return true;
}
});
boolean questionComplete = false;
enter.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v)
{
if(!(questionComplete)){
if(numberVal.getText().toString().equals("14")) {
questionComplete = true;
primaryText.setText(Questions[1]);
}
}
else {
if(numberVal.getText().toString().equals("5")) {
primaryText.setText(Questions[2]);
}
}
}
}
);
This would make it so that after the first question is complete, it would display the second question. It would only work for two questions though
I am using Google Analytics for my android applications and see NullPointerException on the line "wvBrowser.goBack();" although I check it before if the wvBrowser(WebView) is null. I can't make it happen on my normal tests. Any idea?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if (wvBrowser!=null) {
if(wvBrowser.canGoBack() == true){
wvBrowser.goBack();
return true;
}else{
}
}
}
}
return super.onKeyDown(keyCode, event);
}
i've been trying to create an "onHold" action on the enter key of the keyboard.
searchField.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
searchField.setText(searchField.getText().toString().replace("\n", ""));
if((CounterRunning)&&(event.getAction() == KeyEvent.ACTION_UP ) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
CounterRunning = false;
counter.cancel();
AddItem();
}
if((event.getAction() == KeyEvent.ACTION_DOWN ) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
CounterRunning = true;
counter.start();
}
}});
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
CounterRunning = false;
AskForDate();
}
#Override
public void onTick(long millisUntilFinished) {
}
}
i want the user to run AddItem() on click and run AskForDate() if the user hold on the enter key.
but the action KeyEvent.ACTION_DOWN is only triggering when i remove the finger from the keyboard, am i missing something? tested on android 2.3.7 (CM7.2) and android 4.0.4 (CM9), both with default softkeyboard
onKeyLongPress event
try the upper one, it says you must call startTracking in your listener
so your code should be like
if((event.getAction() == KeyEvent.ACTION_DOWN ) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
event.startTracking();
}
and since Activity subclasses KeyEvent.Callback you can directly call your AskForDate() from there.
I know you guys are probably tired of these kinds of posts, but why doesn't anything happen when I press volume down? I'm just trying to make a simple code, but apparently it's not working.
package com.cakemansapps.lightwriter;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.view.KeyEvent;
import android.util.Log;
public class LightWriter extends Activity implements OnTouchListener {
private static final String TAG = "Touch" ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
}
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
Log.w("LightWriter", "I WORK BRO.");
return true;
}
return super.onKeyLongPress(keyCode, event);
}
public boolean onTouch(View view, MotionEvent me) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
I don't know if you can get long press events for the hardware keys.
I've used this code to listen for the volume button before.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
// Do something
}
return true;
}
If that doesn't work for you let us know what device you are testing on.
Kotlin
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
// Do something
}
return true
}
Another approach
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
// ... your code
return true;
} else if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
// ... your code
return true;
} else
return super.onKeyDown(keyCode, event);
}
try these. just tested them:
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
super.onKeyLongPress(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
Log.w("LightWriter", "I WORK BRO.");
return true;
}
return false;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
Log.w("LightWriter", "I WORK BRO.");
return true;
}
return true;
}
use this code to handle Volume key event
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
super.onKeyUp(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
{
Toast.makeText(MainActivity.this,"Up working",Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
Toast.makeText(MainActivity.this,"Down working",Toast.LENGTH_SHORT).show();
return true;
}
return false;
}