I have a few buttons in a tab layout for which the OnClickListener works fine. The purpose of these buttons is to start a new activity when clicked upon. I want my code to work for the touch mode of the phone as well. In other words I wanted to know how to implement the OnTouchListener for the code. Currently I have something like
public boolean onTouch(View v, MotionEvent event) {
startActivity(new Intent().setClass(this, OtherActivity.class));
return true;
}
I am just thinking about it the same way as I would think about the onClick() method but apparently I am wrong.
Any help in this matter would be appreciated.
Thanks!
Touching a button on the screen should generate click events the same as using a hardware keypad. Try using the Log object and LogCat to see when methods are called.
Related
I am currently working on a Java SDK which gets installed by the original developer in his existing Android app. Now I need to set some event listeners e.g.:
public void registerTouchListener(ScrollView scrollview) {
scrollview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("SDK", event.toString());
return false;
}
});
}
The issue is that the passed in scrollview might already have a registered listener, so my question is how I can hook into the existing listener or register a second listener?
Maybe too hacky, but could you somehow "Steal" the touch event before it gets to the Scrollview by checking the coordinates of where the touch is going to land and if it's in your list of registered Scrollview. Then act as if you haven't handled the touch event -- that way you could act on that Scrollview prior to it being passed on the touch event?
Rather than providing the API like this, could you rather provide an API that had a function which would call onTouch directly at the end (or beginning) of their event? Not quite as friendly because it won't live in the initialization section, but it will be much clearer and less buggy.
My (OpenGL ES 2.0) app has a splashscreen which is basically an Android View displayed over my GLSurfaceView.
I do all my initial setup on an AsyncTask and then (out of necessity), load my bitmaps/textures on the GLRendering thread in onSurfaceCreated. Once everything has loaded, I dismiss my splashscreen.
Everything works as expected apart from one thing. The splashscreen still accepts input and if the screen is touched just after the app is launched, I (obviously) get an null pointer exception.
I simply create my view like so:
splashscreen = new SplashScreen(getApplication(), width, height); //Where SplashScreen is a custom class that extends View
I then add it to my layout like so:
layout.add(myView); //My GLSurfaceView
layout.add(splashscreen); //The splashscreen View
setContectView(layout);
Once everything has loaded, I simply remove the (splashscreen) View to reveal my GLSurfaceView underneath:
layout.removeView(splashscreen);
splashscreen=null; //Null it
currently, in my onTouch method, to get around the issue, I have put a check in place like so:
public boolean onTouchEvent(MotionEvent event) {
if(splashscreen==null){
//Do touch related stuff here
}
return true;
}
This does work, however, I'm sure it's not the cleanest way to achieve what I'm after.
I've tried this:
splashscreensetClickable(false);
and
layout.setClickable(false);
as well as setEnabled(false)
Nothing seems to work, but I'm sure there must be a way of doing this.......
Generally, you will want the splash screen to prevent the user from doing anything while it is present. You can actually do this, because views in android, as in other UI frameworks, have a hierarchy in which touch events cascade.
Even though you don't want the splash screen to be interactive, you can still steal all of the touch events that occur on it, and just do nothing with them. The answer itself is to override onTouchEvent in your SplashScreen class with:
#Override
public boolean onTouchEvent(MotionEvent event) {
return true; // Splash screen is consuming all touch events on it.
}
Returning true in this basically says that nothing under this view should receive a touch event. As long as it is visible and over top of the surface view, your surface view will not get any touch events. See here for details.
First off I am kinda new to Android, so with that being said. So I have a spinner, and every time I make a selection the phone will scroll back up to the last edit text who has focus. That is very annoying so I set the spinner as focusable, but for some reason I then have to click the spinner twice to get it to open (the first click gives the spinner focus, the second opens the spinner). So the best I have come up with so far is this:
activitySpinner = (Spinner) findViewById(R.id.acivity_dropdown);
activitySpinner.setFocusable(true);
activitySpinner.setFocusableInTouchMode(true);
activitySpinner.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
activitySpinner.requestFocus();
activitySpinner.performClick();
return true;
}
});
That takes care of the need for two clicks, but that causes problems because it will open the spinner on the slightest touch, even if all I wanted to do was scroll down. Am I looking at this problem the wrong way? Is there a way to make the spinner focusable and also allow it to open on the first click?
Things I've tried:
Setting focusable in the xml,
setting focusable and focusable in touch mode in java,
the code above
Using setFocusable(true) and setFocusableInTouchMode(true) are correct. To fix the two-touch problem created by the latter check for the ACTION_UP event in your touch handler and return false to let the event bubble up which eliminates the extra requestFocus() call:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP && !v.hasFocus()) {
v.performClick();
}
return false;
}
The hasFocus() check makes this specific to the click-twice problem; if the control already has focus, your actual tap should bring up the list without additional work.
Try this also
spinner.requestFocusFromTouch();
I'm developing an android application and when I press the back button from my device (normal press time for a person, 1 second or less), it skips from my activity, to the previous activity (menu) and then exits the application.
But if I tap the back button quickly, it reacts as expected, it goes to the menu.
I've tried to find a solution but no success.
I've always tried to override the back button default behavior but no success either.
Is there a way to set a reaction time for the back button to react?
Many thanks in advance!
P.S.- I have other activities that maintains the expected behavior in back button when pressed with a normal press time.
"Is there a way to set a reaction time for the back button to react?"
Yes, you can simply record the time when the button is pressed and react differently in onBackPressed by calculating the (currentTime-lastTimePressed)
To allow this to work with previous activities, you can ask activities to startActivityForResult, so that when you finish your activity you can pass on the time as well to let them know if they should exit as well.
I was developing an extra option for an application that already exists, and I found out that I should extend not from the Activity from Android but an already extended Activity called SEActivity. So in this extended version of Activity they override the method onKeyDown like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
else
return super.onKeyDown(keyCode, event);
}
By extending this SEActivity, the Back button works with the expected behavior.
Thanks anyway :)
i have a game, consisting of 7 buttons , arranged such that 1 button is in the center and rest 6 around it.
click on the buttons results in change in textview. using click can really be cumbersome for the user, therefore i would like to add functionality of dragging finger over buttons to perform onclick ..something similar to boggle type of games like -> https://market.android.com/details?id=com.ant.wordfind.client&feature=search_result
i have tried to implement the same ,problem is when i drag of finger only one button gets registered , even if i press other buttons by dragging cursor -> other button touch are just ignored.given the
following code.
public boolean onTouch(View v, MotionEvent arg1)
{
if(arg1.getAction() ==MotionEvent.ACTION_MOVE)
{
Button b;
switch (v.getId())
{
case R.id.b1:
case R.id.b2:
case R.id.b3:
case R.id.b4:
case R.id.b5:
case R.id.b6:
case R.id.b7:
b = (Button)findViewById(v.getId());
b.performClick();
break;
}
return true;
}
am i missing somethng ? wht i want is whn buttons are touched ..they perform onclick functionality.once one button click is registered rest touches are ignored
any help would be appreciated .
i dont want something like piano/soundboard where there is continuous click of button ..but just one click as i drag finger over the button
thanks !
At onTouch(), you shouldn't always use return true. Return false if you want the subsequent actions to be captured.
onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.
Ref: http://developer.android.com/guide/topics/ui/ui-events.html