Button Down Event in Eclipse (Android) - java

I wanna use a button (not a key) just like backspace so when it is down do something repeatedly.
I've found proper code for hardware keys but as I mentioned I want a BUTTON do such things.
Thanks

You can set an OnTouchListener on a Button instance. You can then override the onTouch method of the of the listener to do what it is that you want until MotionEvent passed to the onTouch method has MotionEvent.getAction == MotionEvent.ACTION_UP. See this link for an example:
Android onTouch Listener event
A switch statement is sufficient, just customize it to fit your needs using what I said above. --hope this helps, Scott

Thanks Scott. Finally I found the answer and did the job.
public MyActivity extends Activity
{
private Handler mHandler = new Handler();
private Runnable mUpdateTask = new Runnable()
{
public void run()
{
Log.i("repeatBtn", "repeat click");
mHandler.postAtTime(this, SystemClock.uptimeMillis() + 100);
}
};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button repeatButton = (Button) findViewById(R.id.repeatButton);
repeatButton.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View view, MotionEvent motionevent)
{
int action = motionevent.getAction();
if (action == MotionEvent.ACTION_DOWN)
{
Log.i("repeatBtn", "MotionEvent.ACTION_DOWN");
mHandler.removeCallbacks(mUpdateTask);
mHandler.postAtTime(mUpdateTask, SystemClock.uptimeMillis() + 100);
}
else if (action == MotionEvent.ACTION_UP)
{
Log.i("repeatBtn", "MotionEvent.ACTION_UP");
mHandler.removeCallbacks(mUpdateTask);
}
return false;
}
});
}
}

Related

How To Use OnTouchListener To Open New Activity

I Tried OnClickListener an OnLongClickListener And yea it worked but those are too quick and i want to make them even more longer and i am just unable to use OntouchListener To Open New Activity And i have no clue almost tried everything nothing worked
Activity Name: Website
Button id: action_button (its a floatingActionbutton)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton actionButton = findViewById(R.id.action_button);
defineView();
handleIntent();
defineActionBar();
checkPermission();
//i tried both here
public void openWebsite() {
Intent intent = new Intent(this, Website.class);
startActivity(intent);
Not sure why you want to use OnTouchListener instead of OnClickListener since I don't think there is any difference between them referring to button events.
whit OnClickListener you could capture long press with:
button.setOnLongClickListener {
//ACTION
true
}
EDIT: (for custom duration, you should use OnTouchListener, thats right)
you can do something like this:
long time = 0;
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
time = System.currentTimeMillis();
}
else if(motionEvent.getAction() == MotionEvent.ACTION_UP){
double duration = (System.currentTimeMillis() - time / 1000.0);
if(duration > 5){
action2();
return true;
}else if(duration > 3.2){
action1();
return true;
}
}
return false;
}
});

App button as keyboard press

I'm trying to map a button in my fragment layout as a keypress on a keyboard.
This is my button press:
Button down = (Button)(myView.findViewById(R.id.btnDOWN));
down.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i(TAG, "Down pressed");
return false;
}
});
I wasn't able to find any library which would allow me to do so, all I've found was reverse case, when a keyboard or controller press would be sent to device.
What I'm trying to generally do here, is an app that acts like a controller for RetroPie by using the phone as a bluetooth keyboard, with only the keys necessary for a specific controller.
The method you're using is not correct. you should use the setOnClickListener:
Here an example from Android official guide:
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}
If you're looking for continuos aka long click, you should change it to setOnLongClickListener:
down.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
It wasn't hard to find in this case the solution on this site

How to play sound in custom keyboard

I've recently made my own keyboard that is always shown to the user.
I've also found how to play sounds while pressing a key but if I press one and then inmediatly another, only plays the first sound.
Is there any way for making the sound to be played each time I press a key even if there's 0,1 millisecond between them?
Here is my code:
final MediaPlayer mp = MediaPlayer.create(this, R.raw.sn_tecla);
final MediaPlayer mpspc = MediaPlayer.create(this, R.raw.sn_spc);
texto1.setTypeface(fuente);
//This is for each key.
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
texto1.setText(texto1.getText() + "1");
}
});
Thank you in advance.
use soundpool
http://developer.android.com/reference/android/media/SoundPool.html
SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
/** soundId for Later handling of sound pool **/
int soundId = sp.load(MainActivity.this, R.raw.windows_8_notify, 1); // in 2nd param u have to pass your desire ringtone
sp.play(soundId, 1, 1, 0, 0, 1);
You can solve your problem by declaring a mediaPlayer object for each of your buttons(a slight overhead, but it works).
Also, instead of an onClickListener, use an onTouchListener.
For example: If you have 2 buttons and each have to make the same sound, Your code will go like this:
final MediaPlayer[] mediaPlayers = new MediaPlayer[2];
for(int i =0;i<2;i++){
//if both buttons ought to have the same sound
mediaPlayers[i] = MediaPlayer.create(getApplicationContext(), R.raw.beep);
mediaPlayers[i].setLooping(true);
}
button1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//when the user presses the button
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
mediaPlayers[0].start();
texto1.setText(texto1.getText() + "1");
}
//when the user releases the button
if(event.getAction() == MotionEvent.ACTION_UP){
mediaPlayers[0].pause();
}
return false;
}
});
button2.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
mediaPlayers[1].start();
texto1.setText(texto1.getText() + "1");
}
if (event.getAction() == MotionEvent.ACTION_UP) {
mediaPlayers[1].pause();
}
return false;
}
});
Hope this works for you!

Is there any method to find out the display-changing events of android smartphone?

I'm a student of Yonsei graduate school, Korea.
I want to make a simple application that measures an time interval - between
touching the screen and display-updating.
I found that following method catch the touching event.
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if(event.getAction() == MotionEvent.ACTION_DOWN ){
...
Now I'm searching the Android APIs, but I coudn't find the method which catches
display-updating event. If you have any information about this problem,
please show mercy to me. Thank you.
Try adding a ViewTreeObserver.onDrawListener to a view in your Activity's content view. You can make a class that implements the that interface. When you get the touch event, call a method on your draw listener to record the time of the next draw event.
private MyDrawListener myDrawListener = new MyDrawListener();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
findViewById(...).getViewTreeObserver().addOnDrawListener(myDrawListener);
}
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
myDrawListener.recordNextDrawTime();
}
}
public static class MyDrawListener implements ViewTreeObserver.OnDrawListener {
private boolean recordNextDrawTime;
public void recordNextDrawTime() {
recordNextDrawTime = true;
}
#Override
public void onDraw() {
if (recordNextDrawTime) {
Log.d("MyDrawListener", "Draw time = " + System.currentTimeMillis());
recordNextDrawTime = false;
}
}
}

Android java - correct way to handle multiple buttons from main activity

I've been building controls for the vehicle I want to control. However I'm still quite new to Java and android developing. So I am looking for best practices to handle multiple buttons from the UI. So far I've managed to create 2 buttons which are on the same screen, see code below. Is this a correct way to handle and create buttons?
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Left Button */
Button btnLeft = (Button)findViewById(R.id.btnLeft);
btnLeft.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// Create thread
case MotionEvent.ACTION_UP:
// End Thread
}
return false;
}
});
/* Right button */
Button btnRight = (Button)findViewById(R.id.btnRight);
btnRight.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// Create thread
case MotionEvent.ACTION_UP:
// End Thread
}
return false;
}
});
}
}
That code actually works - I'm planning to create threads inside the switch-case statements too, I haven't figured out that one yet. Any input would be appreciated.
step 1 : make activity implement OnClickListener
step 2 : override the method onClick
public class MainActivity extends Activity implements OnClickListener {
Button btnLeft, btnRight;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Left Button */
btnLeft = (Button) findViewById(R.id.btnLeft);
btnLeft.setOnTouchListener(this);
/* Right button */
btnRight = (Button) findViewById(R.id.btnRight);
btnRight.setOnTouchListener(this);
}
#Override
public void onClick(View v) {
if (v == btnleft) {
// do stuff for button left
}
if (v == btnRight) {
// do stuff for button right
}
}
}

Categories