Android Button onclick | Event Handler

ANDROID CODE FOR FRESHERS

Android Button onclick | Event Handler 

 

Java Code(EventAct )

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

public class EventAct extends Activity {
LinearLayout l1;
Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event);
l1=(LinearLayout)findViewById(R.id.l1);
b1=(Button)findViewById(R.id.b1);
b1.setOnLongClickListener(new View.OnLongClickListener()

{
@Override
public boolean onLongClick(View v)

{
l1.setBackgroundColor(Color.RED);
return false;
}
});
}
public void click(View v)
{
l1.setBackgroundColor(Color.GREEN);
}

}

Layout XML Code(activity_event)

<?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:layout_height=”match_parent”
android:id=”@+id/l1″
android:orientation=”vertical”
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=”com.andro.tech.androidproject.EventAct”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textSize=”@dimen/textsize”
android:layout_gravity=”center”
android:textStyle=”bold”
android:text=”EVENT HANDLER”
android:layout_marginBottom=”150dp” />

<Button
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:textSize=”10sp”
android:text=”CLICK ME”
android:onClick=”click”
android:layout_marginBottom=”10dp”/>

<Button
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:textSize=”10sp”
android:text=”LONG PRESS”
android:id=”@+id/b1″
android:layout_marginBottom=”80dp”/>

<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”/>

</LinearLayout>

 

Output :

Android Button onclick     Android Button onclick     Android Button onclick

Download Android Source Code

 

For More Android Programs – Click Here

 

Follow Us – Never Miss Update

Fb/PageTwitterGoogle+InstagramPinterest 

Show 2 Comments

2 Comments

  1. Great article with valuable information thanks for sharing

    • F D A Y T A L K

      Thanks for following Fdaytalk

Comments