How to set finger touch functionality in android? - android

I am developing a small application in android in which i have few image in my application i want when user touch with one finger images can move left or right side and when user can touch with two fingers it could be zoom how can i do this please refer some tutorial code for me.
here is my code
and i used view flipper in xml v fdjf
public class Jaap extends Activity implements OnTouchListener{
float downXValue;
int counter = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set main.XML as the layout for this Activity
setContentView(R.layout.jaap);
// Add these two lines
LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
layMain.setOnTouchListener((OnTouchListener) this);
// Add a few countries to the spinner
}
public boolean onTouch(View arg0, MotionEvent arg1) {
// Get the action that was done on this touch event
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
// vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
// Flip!
if(counter > 0){
vf.showPrevious();
counter--;
}
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
// vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
// Flip!
if(counter < 131){
vf.showNext();
counter++;
}
}
break;
}
}
// if you return false, these actions will not be recorded
return true;
}
}

Check out this step-by-step tutorial, it contains code examples how to zoom in/out a picture and move it around.

Handle the MotionEvent in the onTouchEvent(MotionEvent) of the Activity.
In this check for the MotionEvent.getAction().
switch(MotionEvent.GetAction()) {
case ACTION_DOWN:
//handle the finger down functionality here
break;
case ACTION_POINTER_DOWN:
//handle the second finger down functionality here
break;
}
A sequence of events would be issued, mostly with the actions as follows:
ACTION_DOWN - one finger touch down
ACTION_MOVE -> the finger is moved
ACTION_UP - one finger touch is removed
ACTION_POINTER_DOWN - second finger touch down
ACTION_POINTER_UP - second finger touch up
You will have to check the X,Y positions in the event and determine what should be donw...
will see if there are any good tutorials/samples to explain these better...

getPointerCount() of motionEvent tells you how many fingers are touch. In gives number of touch
Thanks

Related

How to swipe with finger in a View flipper?

I have a view flipper containing two layouts with button. Now the issue is i am not able to swipe along with finger. Basically the view should swipe along with the finger. I hope you are getting what i am trying to say. Here is the onTouchevent code :-
bottomFormatFlipper = (ViewFlipper)findViewById(R.id.bottomFormatFlipper);
public boolean onTouchEvent(MotionEvent touchevent)
{
switch (touchevent.getAction())
{
// when user first touches the screen to swap
case MotionEvent.ACTION_DOWN:
{
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP:
{
float currentX = touchevent.getX();
// if left to right swipe on screen
if (lastX < currentX)
{
// If no more View/Child to flip
if (bottomFormatFlipper.getDisplayedChild() == 0)
break;
// set the required Animation type to ViewFlipper
// The Next screen will come in form Left and current Screen will go OUT from Right
bottomFormatFlipper.setInAnimation(this, R.anim.in_from_left);
bottomFormatFlipper.setOutAnimation(this, R.anim.out_to_right);
// Show the next Screen
bottomFormatFlipper.showNext();
}
// if right to left swipe on screen
if (lastX > currentX)
{
if (bottomFormatFlipper.getDisplayedChild() == 1)
break;
// set the required Animation type to ViewFlipper
// The Next screen will come in form Right and current Screen will go OUT from Left
bottomFormatFlipper.setInAnimation(this, R.anim.in_from_right);
bottomFormatFlipper.setOutAnimation(this, R.anim.out_to_left);
// Show The Previous Screen
bottomFormatFlipper.showPrevious();
}
break;
}
}
return false;
}
From this code, when i swipe, then after a second or so, the swipe animation occurs. I want that the view should swipe with my finger

Move finger across buttons in android apps

When the user touches the button sound shoud be played. There is a lot of buttons, and each has a different sound. When user move finger across all buttons, all sounds should be played. Something like piano app. How can I do it? I tried with ontouch lister but it doesn't work.
Within your onTouch method, you should capture different events starting from user TouchDown followed by TouchMove and finally TouchUp. Look for all x,y co-ordinates whether in your button area, if so, play sound.
Make sure that the current button selection is different than the previous one otherwise, if your finger moves over same button, that'll trigger another event for same button and you'll here tons of sound while moving finger:
psudo code:
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction())
{
case MotionEvent.ACTION_UP: // stop here
break;
case MotionEvent.ACTION_DOWN: // start here
break;
case MotionEvent.ACTION_MOVE:
// See if new x y co-ordinates in your buttonRect area
// RectF [] buttonRect = new buttonRect[10] ;
for(int i = 0; i < 10; i++)
{
if( buttonRect[i].contains(event.getX(), event.getY()))
{
// if it's a new button found than previously touch, play a sound
// store the button number that's been tapped by user
}
}
break;
}
}
Hope it helps.

Dragging and pressing buttons - android

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.

The view, which was added with addView do not Flip (using ViewFlipper, Android)

The problem is that all the views, added to main.xml are flipping correctly - after the last view goes first view, after first -goes last, its rounded, but if i add the view using method addView of ViewFlipper class it won't flip "rounded" it will stop on it and do some incorrect animation, it won't go to next view and will go to previous only if there was done only 1 flip to the end.
Please, say how to make it works as a round 1->2->3->1.
Here's the code of flipper realization:
public class Activity1 extends Activity implements OnTouchListener{
float downXValue;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set main.XML as the layout for this Activity
// Add these two lines
LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
layMain.setOnTouchListener((OnTouchListener) this);
}
public boolean onTouch(View arg0, MotionEvent arg1) {
// Get the action that was done on this touch event
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
View view = new View(this);
//HERE IS DECLARATION OF VIEW WHICH I NEED TO ADD
GraphicsView myview=new GraphicsView(this);
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
vf.addView(myview);
// Set the animation
vf.setInAnimation(view.getContext(), R.anim.push_right_in);
vf.setOutAnimation(view.getContext(), R.anim.push_right_out);
// Flip!
vf.showNext();
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
//HERE I'M ADDING IT
vf.addView(myview);
// Set the animation
vf.setInAnimation(view.getContext(), R.anim.push_left_in);
vf.setOutAnimation(view.getContext(), R.anim.push_left_out);
// Flip!
vf.showPrevious();
}
break;
}
}
// if you return false, these actions will not be recorded
return true;
}
Please, help. And answer plz if possible to add in main.xml the objects, that i defined in the code like myview is class object of GraphicsView, which extends from View.
Regards,
Keem
As per your code that you post, In your code you are adding view dynamically, on Touch Events, so as many time you touch your device screen, your OnTouch() call and every touch call this..
GraphicsView myview=new GraphicsView(this);
and also this.. ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
vf.addView(myview);
so on every touch you adding a view, and may be thats why you not getting right flipping.
For your solution, you should follow correct flow - may be as follows
in OnCreate() add different view in viewFlipper, out side your OnTouch().
In OnTouch() you set your animation and call vf.showPrevious() orvf.showNext() for propper navigation.

Android Home Screen effect

I want to do something like andriod home screen effect. when i click the screen and move. the current view will move and next view also can show.
Now the code only can show the current view.
Thank you for your help
The source as follow:
public class test extends Activity implements OnTouchListener{
float downXValue;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set main.XML as the layout for this Activity
setContentView(R.layout.main);
// Add these two lines
LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
layMain.setOnTouchListener((OnTouchListener) this);
// Add a few countries to the spinner
Spinner spinnerCountries = (Spinner) findViewById(R.id.spinner_country);
ArrayAdapter countryArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,new String[] { "Canada", "USA" });
spinnerCountries.setAdapter(countryArrayAdapter);
}
public boolean onTouch(View arg0, MotionEvent arg1) {
// Get the action that was done on this touch event
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
// Flip!
vf.showPrevious();
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
// Flip!
vf.showNext();
}
break;
}
case MotionEvent.ACTION_MOVE:
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
final View currentView = vf.getCurrentView();
currentView.layout((int)(arg1.getX() - downXValue),
currentView.getTop(), currentView.getRight(),
currentView.getBottom());
/*may be do something in thest*/
break;
}
// if you return false, these actions will not be recorded
return true;
}
}
You can use Viewpager.tofeeq ahmad says:
Viewpager is the best way to switching among view. It provide a way
to swipe views from left to right and right to left. Viewpager provide
strong way to swipe any views.
You have to extend PagerAdapter and then use Viewpager.You can see snippet and full source code in Guidance on Android, Java and Other mobile Technology.

Categories

Resources