hey guys i try to get A popup menu when i click left click on the device i don't want open
popup menu by using button just want open when i click the left.
Like this :
http://ecee.colorado.edu/ecen3000/labs/lab8/files/javaLab1_files/image020.jpg
Thanks all...................
i tried
public void showMenu(View v) {
PopupMenu popup = new PopupMenu(this, v);
// This activity implements OnMenuItemClickListener
popup.setOnMenuItemClickListener((OnMenuItemClickListener) this);
popup.inflate(R.menu.actions);
popup.show();
}
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
default:
return false;
}
}
By touching left side of the screen you need to popup. My suggestion is first you have to add setOnTouchListener for the root layout you created in xml.
and by using the below override method u can get the touch event of x and y position where the
finger is placed on the screen, then restrict the range to corner as you need and compare the touch x and y, if it is between the range then popup your diaglog.
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
//finger placed down, this will run
}
else if (action == MotionEvent.ACTION_UP) {
//finger is up, this will run
}
else if (action == MotionEvent.ACTION_MOVE) {
//finger is moved, this will run
For clear understanding check this link (http://www.vogella.com/tutorials/AndroidTouch/article.html)
Related
I am trying to implement a "swipe and hold" gesture in Android where the user swipes from a point on the left to another point on the right, then continues to hold at the right point until a server-side action is complete. The idea being that if the user lets go of the hold, the server-side action will be cancelled.
I've been trying to go about this the following way but have encountered an issue when getting to the hold part of the gesture. What I'd like to have happen is that when the user reaches point B on the right, the ACTION_MOVE is somehow converted into an ACTION DOWN for the view on the right and from then on, as long as the event ACTION_DOWN continues the gesture is valid, but if the user lets go or leaves the view area, it is invalid. So the question is, is it possible to chain the ACTION_MOVE from left to right into a "hold" gesture once the user reaches point B on the right? I've tried adding an onTouch to point B after the swipe, but the only way that works is if the user lets go of the swipe, then presses point B again. Will this be possible, or should I go about this a different way, such as drag and drop?
Point A and B as found below are currently image views, but that's more placeholder than anything.
private boolean mValidGesture = false;
Rect outRect = new Rect();
int[] location = new int[2];
private boolean isViewInBounds(View view, int x, int y){
view.getDrawingRect(outRect);
view.getLocationOnScreen(location);
outRect.offset(location[0], location[1]);
return outRect.contains(x, y);
}
...
mPointA.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getRawX();
int y = (int)event.getRawY();
if(event.getAction() == MotionEvent.ACTION_MOVE){
if (isViewInBounds(mPointB, x, y)) {
mPointB.dispatchTouchEvent(event);
Log.d(TAG, "onTouch ViewB");
Toast.makeText(MainActivity.this, "Swipe complete", Toast.LENGTH_SHORT).show();
//This is the part that happens only after you let go of the swipe :(
mPointB.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
mValidGesture = true;
Log.d(TAG, "Gesture: " + String.valueOf(mValidGesture));
return true;
case MotionEvent.ACTION_UP:
mValidGesture = false;
Log.d(TAG, "Gesture: " + String.valueOf(mValidGesture));
return true;
}
return false;
}
});
} else if(isViewInBounds(mPointA, x, y)){
Log.d(TAG, "onTouch ViewA");
}
}
// Further touch is not handled
return false;
}
});
You don't need to put touchListener on pointB. Try something like this:
boolean serverActionStarted = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getRawX();
int y = (int)event.getRawY();
if(event.getAction() == MotionEvent.ACTION_MOVE){
if (isViewInBounds(mPointB, x, y)) {
// TODO : swipe completed. Start server action
serverActionStarted=true;
}
}else if(event.getAction() == MotionEvent.ACTION_UP && serverActionStarted){
// TODO : cancel the server action
serverActionStarted = false;
}
So basically, when the user swipes from pointA to pointB, you start the server action and set a boolean serverActionStarted to true. After that, if ACTION_UP event occurs, with the severAction having started, you cancel the server action.
I wan't to use a image view like below, to create a virtual cross controller or d-pad (It's not possible in my case to do this with different buttons).
When the cross image is displayed on the screen, I wan't to check on wich side the user has pressed to call a function like up(), down(), left() and right().
Cross image:
imageView1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
if((x>170&&x<230)&&(y>0&&y<170))
{//up
Toast.makeText(MainActivity.this,"up",Toast.LENGTH_SHORT).show();
}
else if((x>170&&x<230)&&(y>230&&y<400))
{//down
Toast.makeText(MainActivity.this,"down",Toast.LENGTH_SHORT).show();
}
else if((x>0&&x<170)&&(y>170&&y<230))
{//left
Toast.makeText(MainActivity.this,"left",Toast.LENGTH_SHORT).show();
}
else if((x>230&&x<400)&&(y>170&&y<230))
{//right
Toast.makeText(MainActivity.this,"right",Toast.LENGTH_SHORT).show();
}
return false;
}
});
//take iamge view 400px*400px
I am currently trying to detect an ongoing touch event in my Android app.
In detail I want to recognize within a fragment whether the user touches any part of the app's screen.
Android's OnTouchListener works as expected except if the touch event lasts longer than a few seconds without moving.
For example the first 1-2 seconds of touch are being detected but everything after won't.
Is there something like an "OnPressListener" or a workaround?
If it's a well defined gesture you are trying to detect, have a look at GestureDetector.
http://developer.android.com/reference/android/view/GestureDetector.html
You can use
aButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
Toast.makeText(getApplicationContext(), "Long Clicked " ,
Toast.LENGTH_SHORT).show();
return true; // <- set to true
}
});
on your aButton and if you are using API < 19 you have to add
android:longClickable="true"
Attribute to your aButton in layout xml.
I finally found it out.
The solution is to use a simple OnTouchListener:
private boolean pressed = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
pressed = true;
} else if ((action == MotionEvent.ACTION_UP)
|| (action == MotionEvent.ACTION_CANCEL)) {
pressed = false;
}
return true;
}
I have several buttons and I would like to press on one of them and drag through another presing them. Could you tell me which MotionEvent or another functionality should I use. I'm using onTouchListener.
There is an image where you can see what I want to do (first ACTION_DOWN on 1st button and drag through 2nd-7th buttons still pressing the screen) and finally press every white buttons:
Below is my onTouch button code:
button1 = (Button) findViewById(R.id.button1);
button1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
soundIDs[0] = sound.play(R.raw.sample1);
button1.setBackgroundResource(R.drawable.white_clicked);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
sound.stop(soundIDs[0]);
button1.setBackgroundResource(R.drawable.white);
break;
}
return false;
}
});
You are setting the OnTouchListener on just the one button. That's not going to help you know when the pointer moves (e.g. user drags his finger) into another button.
You could set an OnTouchListener on the view that contains the buttons. Then check for ACTION_DOWN, ACTION_MOVE, and ACTION_UP events. You would then have to do some simple hit detection to figure out which button to activate.
Something along the lines of:
getWindow().getDecorView()
.findViewById(android.R.id.content)
.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
int action = event.getAction();
if (action != MotionEvent.ACTION_DOWN
&& action != MotionEvent.ACTION_MOVE
&& action != MotionEvent.ACTION_UP) return false;
Rect hitRect = new Rect();
Button button;
for(int i = 0; i < myButtons.size(); i++) {
button = myButtons.get(i);
button.getHitRect(hitRect);
if (hitRect.contains((int)event.getX(), (int)event.getY())) {
button.setText("hit!");
}
}
return true;
}
});
Where myButtons is an ArrayList of your buttons (piano keys in your example).
Also, you'd probably want to modify this to properly deactivate the currently active button if the user's touch leaves the button, but doesn't hit another button.
I tested the above code on an android device with a layout that has 3 buttons in a row. Dragging your finger across all the buttons causes each button's text to change to "hit!"
Like I said above, you were setting the touch listener on the just one button, that will not work. In this example I have set the touch listener on the entire view for the activity.
In my app I draw an image covering the entire screen.
I want to now how can I know where the user touched the screen?
Thanks
Answered here: How do i get the x,y coordinate for a screen touch?
Here is a snippet of one of my games that does this. There are different ways to do it, but here I did it by subclassing View:
public class WorldView extends View {
...
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
Log.d(TAG,"touch event "+action+" x="+event.getX()+" y="+event.getY());
if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE)
{
int x = (int)event.getX();
int y = (int)event.getY();
Log.d(TAG,"setting target to "+x+","+y);
}
else
return super.onTouchEvent(event);
return true;
}