How to recognize touch on the home screen - android

I am very new to android. I just want to know how can i recognize the touch on the screen of android phone.
Till now what i did, created an application and on that application view i am able to recognize the touch.
Please refer screen shot to know that where i am able to recognize touch.
Please refer screen shot where i am not able to recognize touch.
Please give me some suggestion how to recognize touch on main screen of android phone.

What you are looking for is the onTouchListener.
Here is an example code, you can use any view to trigger this method:
yourView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){ //When finger is lifted off the screen
// Do what you want, for example, raise a Toast:
Toast.makeText(yourActivity.this, "Screen has been touched", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
Read more Here
Hope this has helped!

You cannot do that outside your own app. Home screen is like another app(System) for your app. Detecting actions in another app is a kind of intrusion and android won't allow that.
If you create your own launcher, you can. But cannot detect touches with the system default launcher.

Related

Android SurfaceView: Better way to tell if screen is touched (java)

I need a way to tell if there is at least one finger touching the screen. I had no problem doing this in LibGDX but now I can't do it without it. This is how I have it setup:
public static boolean screenTouched = false;
//This methods is run at 60FPS by the main thread (it's the main game loop)
public static void update(){
//Run other updates...
screenTouched = false;//Called after other updates
}
public boolean onTouchEvent(MotionEvent e){
screenTouched = true;
}
By the way, this is all in the main activity class.
This seems like it would work fine, right? It doesn't, onTouchEvent(MotionEvent e) is only called by android when the user's finger moves on the screen and doesn't care about whether they are just touching it or not. The problem with this is that if the user touches the screen and keeps their finger in place while still touching the screen, onTouchEvent will not be called and screenTouched will stay false. Anyone know a way around this to be able to tell if the screen is being touched even if the users finger isn't moving?

how to response click event when screen off

i have a app,if my app screen off some times,i want to click the screen to awake the screen on.i know i can press the power to deal with using flag_user_present to receive the broadcast or
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
//do nothing but show a warning message
Toast.makeText(this, "you pressed the power button",Toast.LENGTH_SHORT).show();
return true;
}
return super.dispatchKeyEvent(event);
}
but i want to click the screen to do the same thing ,so i overload the ontouch event,but when screen off,the ontouch not receive focus and deal with the click event.
so my question how to deal with click event when screen off(my screen not lock and stay the same activity,only screen off)
You won't be able to do this when the device shuts off the screen. If the screen is off, then the touchscreen usually get's shut down immediately by the kernel (if it's not modified otherwise). This happens for obvious reasons.
You could use what tsp said in his answer, and emulate the screen off by turning it black, turn the brightness as far low as possible and then still listen for touch events. Keep in mind though, that this behavior is unexpected by the user and he might think the screen is off, where it isn't. I don't recommend doing this! Also, not all devices allow a zero brightness.
u can use
android:keepScreenOn="true"

Android - Is it safe to assuming MotionEvent.ACTION_UP will 100% mean a user has touched view?

I have a view (WebView to be specific). In that view, I have something like:
setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//user has clicked
}
}
});
will this 100%, ALWAYS guarantee me that the user has tapped/clicked on the view? If not, under what cases would this not guarantee me a click??
I want to intercept all user "clicks". think of "clicking" like you would "click" a button, but just on a mobile device. Imagine this code being called 100 million times by different devices
MotionEvent.ACTION_UP is when you lift up your finger from the screen. You can be sure the user has touches a view once the onTouch is called.
I think that while MotionEvent.ACTION_UP won't always be called when the touch ends (as mentioned here: ACTION_UP not always called?), it's certainly safe to assume that a user has touched the screen. That's how the API describes it, IMO:
A pressed gesture has finished, the motion contains the final release
location as well as any intermediate points since the last down or
move event.

How to track mobile button(not widget button) click event

In Android application is it possible that when I press my mobile *(star) button(not widget button) then I can perform any particular events in my application? If it's possible, then how may I achieve it?
If you mean the * key from your hardware keyboard ( on the devices that have it) you can capture it using KeyCode.
Here you can find an extensive list of all the keys you can intercept.
To do it:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_STAR: //here you check any key you want
{
//your code here
return true;
}
}
return super.onKeyDown(keyCode, event);
}
EDIT
Answering your comment, I don't believe this is possible. The KeyDown/Up events are handled on Activities. And you won't have an Activity active. Check this out!
EDIT
Yeah, according to this guy you can't.
If the button is within your own app, then yes.
If you mean a button in any other app (I think you mean the * key on the dial pad), then no.

Android Camera Button Half Way Pressed Listener

I am building a custom camera app and have got the basics to work. I have also been able to block the camera button from initializing the real camera app. The only thing that I would like to do is build in autofocus when the camera button is half pressed.
I am comfortable using camera.autofocus, but cannot find a way to listen for the camera button to be halfway pressed (like the default camera app does) to start the autofocus call.
Is there a keycode or another way to listen for the camera button being depressed to its half way point?
I got a little creative and just toasted any key down event in android. I ended up finding out that the key code for camera focus is 80 this way. This also matches up with the android documentation once I knew what I was looking for.
http://developer.android.com/reference/android/view/KeyEvent.html
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
Toast.makeText(this, new Integer(keyCode).toString(), Toast.LENGTH_LONG).show();
return true;
}
return false;
}
Hope this helps others.
The camera button is a virtual button on the screen, I am not sure how it can the half pressed, or even could be mimicked in any way.

Categories

Resources