How to increase SeekBar scrolling touch position - android

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.

Related

How can I set any view to the middle of my touch position?

How can I set any view to the middle of my touch position using onTouchEvent?
Red Circle Is My Click Position
Black Circle Is The View Like Button, ImageView, And So On...
Can someone leave an example?
Edit
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
imgCircle.setX(event.getX() - imgCircle.getWidth() / 2);
imgCircle.setY(event.getY() - imgCircle.getHeight() / 2);
}
return false;
}
Example of a solution:
#Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
yourView.setX(x);
yourView.setY(y);
return false;
}
You get the coordinates from click and set them to the view. If you have multiple views inside of one you might have to get relative position of the parent view and then subtract the clicked position from parent position.
If you want to set the view position to the centre just subract half of view width from x and half of height from y.
EDIT
How to set position to centre of view:
yourView.setX(x-yourView.getWidth()/2);
yourView.setY(y-yourView.getHeight()/2);

How to get the absolut position of a touch event?

I made a Recylerview and added a button to the left bottom of the Element.
Now when I use:
private View.OnTouchListener onWidthClickListener =
new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
int x = (int) event.getX();
Log.d(TAG, "Width: " + x);
}
return false;
}
};
I get the position of the courser but it is absolute to the button and I want it relative to the hole Screen.
Because it is a RecylerView the button has no fixed position relative to Screen, therefor I can't just add or subtract the values of the layout.
If you want the coordinates relative to the top left corner of the device screen, then use the raw values.
int x = (int)event.getRawX();
int y = (int)event.getRawY();
Exaplain:
getX() and getY()--> return you coordinates relative to the View .
MotionEvent.getRawX() and MotionEvent.getRawY() -->return absolute coordinates,regarding the screen of the device.
For more details:doc
You can use event.getRawX() and event.getRawY() to get position relative to screen.

Need to get the X and Y value of image where it clicked I am using image which larger then device in android

Hi, I know how to get the clicked position in screen .. my problem is I need to get the X and Y value of image where it clicked. It works fine if I have image size lesser then device but it goes wrong when using the large image.
I am Using horizontal and vertical scroll view to show the large image.
Using the event.getX() to get the position in the touch listener.
To get X and Y value on image put following code in OnTouchListener.
public boolean onTouch (View v, MotionEvent ev)
{
final int action = ev.getAction();
final int evX = (int) ev.getX();
final int evY = (int) ev.getY();
}

x and y coordinates of image in Layout

How to getthe X and y coordinates of an image in Layout in Android?
And how to get top, bottom, left and right of an image in Layout in Android?
by using Touch listeners you can get X and Y values of view
layout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
return false;
}
});
for x and y coordinates of any view compared to its parents , use :
getLeft() , getTop() , getRight() ,and getBottom() .
for raw x and y coordinates of any view compared to the screen/window , use:
getLocationInWindow(int[] location) , getLocationOnScreen(int[] location)
do note that all of those methods won't work till the view is positioned and layout procedure has finished.
more methods are written on the API here.

Android touch in predefined position

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 :))

Categories

Resources