Android touch in predefined position - android

I have an android code which starts vibrating for a random amount of time when the user touch a screen. Now I want to change it a bit. I want to ask the user to touch a predefined position, for example in the middle of screen. what should I do that only in some special coordinates touching become effective?

You can use a onTouchListener. The Listener gets a MotionEvent from which you can get th coordinates and check the position:
something.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// get coordinates from touch event
int x = (int) event.getX();
int y = (int) event.getY();
// check if the coordinates are in a specific area
return true;
}
});
To get the width and height you can use the following code:
Display d = getWindowManager().getDefaultDisplay();
width = d.getWidth();
height = d.getHeight();

onTouch(View v, MotionEvent event)
onTouch give you a MotionEvent, with this, you can simply do
event.getX();
event.getY();
for getting coordinates and do something with it (or not :))

Related

How to get the input from a touch screen from individual pixels?

I am creating a paint app for android, I have a doubt when we touch the display, can we get the input pixel by pixel to make the painting more accurate. Any one help me how to get pixel by pixel input ?
You can just set an OnTouchListener on your canvas view and get the x & y coordinates from there.
view.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getActionMasked() == MotionEvent.ACTION_DOWN)
{
//Here you can do whatever you need with the coordinates
int xPos = event.getX();
int yPos = event.getY();
}
return false;
}
});

how to do zoom image like facebook and tagging name?

I am new in android ,i want to do zoom image and tagging name on actual position on image.if I zoom image then position of tagged text should not change.tagged text would stick with in position on image.
Help me.thanks in advance.
You can check position like this way,
mView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
}
});
For demo purpose,check demo git SimpleTagImageView

How to increase SeekBar scrolling touch position

I have a seekbar.Normally when we scroll on seekbar setOnSeekBarChangeListener is fire.Now I want when we scroll anywhere on the screen setOnSeekBarChangeListener should be fire(So seekbar will be scrolled).Actually I'm not getting how to do this.So can any help me out.Thanks
use this override method for touchevent `
#Override public boolean onTouch(View v, MotionEvent event)
{
int x = event.getX();
int y = event.getY();
return true;
}
now when you will click on screen you will have x and y axis. suppose you have 240px width and u have click it in centre than u will have x = 120. you can assign it to seekbar. and make your seekbar max value to max value of widht. or convert that x=120 wrt to 100 and than assign it to seek bar.

Android get x y coordinates of tab

In Android tabs, how does one get the x,y coordinates of the currently selected tab? My goal is to automatically scroll to that tab (using scrollTo(x,y)) when it is selected in code.
You can find the x and y location of place where screen has been touched using this
main.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent e)
{
float x = e.getX();
float y = e.getY();
return true;
}
});
Hope it helps .

Android and location of touch event

In Android, we can set OnClickListeners for views.
But how do we intercept the location of the touch event?
there is method in Activity
public boolean onTouchEvent(MotionEvent event){
int action = event.getAction();
int x = event.getX()
int y = event.getY();
return yourBoolean;
}
Edit: Or you can define OnTouchListener for any View and can use its method
public abstract boolean onTouch(View v, MotionEvent event);
Edit2: the x and y values depends upon the method call. If you use onTouchEvent() of Activity then it indicates that no View consumes the touch event see documentation and if you handle it on any View then the x and y will be according to the View's area.
You can set the onTouchListener for the same view. The MotionEvent will have the x and y.

Categories

Resources