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.
Related
I change all my activitys to Fragment. I got stuck in the onkeydown part.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (!sector.getText().toString().equals("Sector") && (media.isChecked() || completa.isChecked())) {
siguiente.setEnabled(true);
} else {
siguiente.setEnabled(false);
}
return super.onKeyDown(keyCode, event);
}
You can do this by defining method in your fragment class. For example:
public void onMyKeyDown(int key, KeyEvent event){
//define your statement like
if (Integer.parseInt(android.os.Build.VERSION.SDK) > 5
&& key == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Do you really want to Exit?")
.setIcon(R.drawable.logo)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}})
.setNegativeButton(android.R.string.no, null).show();
}
}
Call onMyKeyDown method whenever a key-down event is raised in your Activity class. example:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//call fragment method onMyKeyDown(keyCode, event)
return super.onKeyDown(keyCode, event);
}
I'm trying to make two buttons that will change add or subtract 1 from a value when tapped, and constantly add or subtract 1 ten times per second while the button is held. I can get the value to be changed when the button is tapped, or when it is held, but I can't get the behavior I want. Here's what I have:
btPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
setTempo(mTempo + 1);
mTempo=mTempo+1;
tvTempo.setText(Integer.toString(mTempo));
return true;
}
return false;
}
});
btMinus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
setTempo(mTempo - 1);
mTempo=mTempo-1;
tvTempo.setText(Integer.toString(mTempo));
return true;
}
return false;
}
});
btPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
if (getActivity() == null)
return;
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
tvTempo.setText(Integer.toString(mTempo));
mTempo++;
if (mTempo > 300)
mTempo = 300;
}
});
}
}, 100, 100);
break;
case MotionEvent.ACTION_UP:
timer.cancel();
}
return true;
}
});
btMinus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
if (getActivity() == null)
return;
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
tvTempo.setText(Integer.toString(mTempo));
mTempo--;
if (mTempo < 1)
mTempo = 1;
}
});
}
}, 100, 100);
break;
case MotionEvent.ACTION_UP:
timer.cancel();
}
return true;
}
});
return rootView;
}
Thanks for your help!
You can implement onKeyListener to determine how long button is held, like in this example:
Android long-touch event
or use onLongClickListener to perform a different function from a regular "click" like here:
On long click delete item
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);
};
How can I configure the back button to be pressed twice before the app exits? I want to trigger
#Override
public void onBackPressed() {
//custom actions
//display toast "press again to quit app"
super.onBackPressed();
}
Try this:
private boolean doubleBackToExitPressedOnce = false;
#Override
protected void onResume() {
super.onResume();
// .... other stuff in my onResume ....
this.doubleBackToExitPressedOnce = false;
}
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Press twice to exit", Toast.LENGTH_SHORT).show();
}
This snippet handle also the reset state when the activityis resumed
I see this question is a bit old but I though this might help some people looking for an alternative to the answers already given.
This is how I handle backing out of my applications. If someone has a better -- or a Google suggested -- method of accomplishing this I'd like to know.
Edit -- Forgot to mention this is for Android 2.0 and up. For previous versions override onKeyDown(int keyCode, KeyEvent event) and check for keyCode == KeyEvent.KEYCODE_BACK. Here is a good link to check out.
private boolean mIsBackEligible = false;
#Override
public void onBackPressed() {
if (mIsBackEligible) {
super.onBackPressed();
} else {
mIsBackEligible = true;
new Runnable() {
// Spin up new runnable to reset the mIsBackEnabled var after 3 seconds
#Override
public void run() {
CountDownTimer cdt = new CountDownTimer(3000, 3000) {
#Override
public void onTick(long millisUntilFinished) {
// I don't want to do anything onTick
}
#Override
public void onFinish() {
mIsBackEligible = false;
}
}.start();
}
}.run(); // End Runnable()
Toast.makeText(this.getApplicationContext(),
"Press back once more to exit", Toast.LENGTH_SHORT).show();
}
}
You could do what you're asking with a global integer and just count it, if > 2, quit.
But you could take a better (IMO) approach, where you question the user if they would like to quit or not:
private void questionQuit(){
final CharSequence[] items = {"Yes, quit now", "No, cancel and go back"};
builder = new AlertDialog.Builder(mContext);
builder.setCancelable(false);
builder.setTitle("Are you sure you want to quit?");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item){
case 0:
quit();
break;
case 1:
default:
break;
}
}
}).show();
AlertDialog alert = builder.create();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK :
int i = 0 ;
if(i == 1 )
{
finish();
}
i++;
return true;
}
}
return super.onKeyDown(keyCode, event);
}
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;
}