/ Getting Started

Simple touch gesture Android sample

Hi friends, its been long time for the update. I have so much stuffs and samples to share with you. Here we go.

Android is a touch based mobile OS. So why shouldn't we start from touch gesture, long press and kind of examples.

Before we enter in to sample app make sure that you know basics things about layouts, UI elements. If not you can follow the procedures listed here.

Create a android application project, so then you will also create An Activity and its layout. because its the default process that happens when you create new app.

Consider that you created your Activity as MainActivity.java and its layout activity_main.xml.

UI Part

  1. Open your activity_main.xml and add a TextView, EditText and three buttons to your layout. You can do it by dragging those elements from the left panel of Graphical layout tab. Graphical layout will be available on bottom of your layout screen or Simply create those xml elements in activity_main.xml
  2. Add the attributes like id, height, width and text to the Button, EditText and TextView elements.

Now your layout will look like the below one.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textview"
        android:layout_below="@+id/textview"
        android:hint="Enter some text"
        android:layout_marginTop="18dp"
        android:ems="10" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText"
        android:layout_below="@+id/editText"
        android:layout_marginTop="29dp"
        android:text="Click me" />

    <Button
        android:id="@+id/longpress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button"
        android:layout_below="@+id/button"
        android:layout_marginTop="20dp"
        android:text="Long press" />

    <Button
        android:id="@+id/dragme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/longpress"
        android:layout_below="@+id/longpress"
        android:layout_marginTop="15dp"
        android:text="Drag me" />

</RelativeLayout>

     

Coding part

  1. Open your MainActivity.java. You can recall the Activity lifecycle call back methods.
  2. Declare and initialize variables for the elements created in activity_main.xml. And action listeners like ClickListener, TouchListener, LongClickListener as like below.

public class MainActivity extends Activity {
	TextView mTextView;
	EditText mEditText;
	Button mclickMe,mLongpress,mdragMe;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mTextView = (TextView) findViewById(R.id.textview);
        mEditText = (EditText) findViewById(R.id.editText);
        mclickMe  = (Button)findViewById(R.id.button);
        mLongpress = (Button)findViewById(R.id.longpress);
        mdragMe		=	(Button)findViewById(R.id.dragme);
        
        mclickMe.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				if(mEditText.getText().length()>0)
					mTextView.setText(mEditText.getText());
				else
					mTextView.setText("No text entered.........");
				Toast.makeText(getApplicationContext(), "Click works", Toast.LENGTH_LONG).show();
			}
		});
        

        mLongpress.setOnLongClickListener(new View.OnLongClickListener() {	
			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "Long press", Toast.LENGTH_LONG).show();
				return false;
			}
        });
        
        
        mdragMe.setOnTouchListener(touchListener);
    }
    
    View.OnTouchListener touchListener=new View.OnTouchListener() {
		
		@Override
		public boolean onTouch(View v, MotionEvent m) {
			// TODO Auto-generated method stub
            return true;
		}
	};
    }

You can see listeners are intialized in different ways in above code. So now if you run your application, click and long press action will work on respective buttons.

Now we enter in to the interesting part dragging a button. why a button ?. Because i assigned touchlistener for button. You can set TouchListener for the any view. Just add the following few lines mentioned below.


 View.OnTouchListener touchListener=new View.OnTouchListener() {	
		@Override
		public boolean onTouch(View v, MotionEvent m) {
			// TODO Auto-generated method stub
			if (m.getAction() == MotionEvent.ACTION_DOWN){

			}else if (m.getAction() == MotionEvent.ACTION_MOVE  ){
               RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
               params.leftMargin = (int) (m.getRawX() - (v.getWidth() / 2));
               params.topMargin = (int) (m.getRawY() - 2*(v.getHeight()));
               v.setLayoutParams(params);
            }
            return true;
		}
	};
 

These lines let that button to move across your screen and also it will resize based on position.

You can also try this example. I gave this example So you can know that Android is very easy to learn.

Happy coding :-).

Simple touch gesture Android sample
Share this