Home > Android > Android Program -Touch Activity

Android Program -Touch Activity

ANDROID

Android Program -Touch Activity

   

Java Code(TouchAct )

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

public class TouchAct extends Activity {
LinearLayout l3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_touch);
l3=(LinearLayout)findViewById(R.id.l3);
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
super.onTouchEvent(event);
int act = event.getAction();
if (act == MotionEvent.ACTION_DOWN)
{
l3.setBackgroundColor(Color.BLUE);
}
else
{
if (act == MotionEvent.ACTION_UP)
{
l3.setBackgroundColor(Color.WHITE);
}
else
{
if (act == MotionEvent.ACTION_MOVE)
{
l3.setBackgroundColor(Color.RED);
}

}
}
return true;
}

}

Layout XML Code(activity_touch)

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:orientation=”vertical”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:id=”@+id/l3″
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.andro.tech.androidproject.TouchAct”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textSize=”20sp”
android:layout_marginLeft=”10dp”
android:text=”@string/touchtext”
android:layout_marginBottom=”300dp” />

<Button
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”@string/sourcecode”
android:background=”@color/bgcolor”
android:textStyle=”bold”
android:textColor=”@color/text”
android:onClick=”view1″/>

</LinearLayout>


Output

   

Download Source Code

Follow Us – Never Miss Update

Fb/PageTwitterGoogle+InstagramPinterest 


Recent   

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »