Home » » Android Simple Button Click Example

Android Simple Button Click Example

Written By MR HARI on Kamis, 28 April 2011 | 00.07


SIMPLE BUTTON CLICK

SOURCE CODE [main.xml] is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
                android:layout_height="fill_parent">
               
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

                <Button android:id="@+id/button1"
android:layout_width="100px"
                                android:layout_height="wrap_content"
android:text="Button 1" />

                <Button android:id="@+id/button2"
android:layout_width="100px"
                                android:layout_height="wrap_content"
android:text="Button 2" />

</LinearLayout>

SOURCE CODE [ButtonExample.java] is

package com.ButtonExample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ButtonExample extends Activity
{
Button b1,b2;

public void onCreate(Bundle savedInstanceState)
{
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);

b1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast msg = Toast.makeText(getBaseContext(),
                                                                                "You have clicked Button 1", Toast.LENGTH_LONG);
                                                                msg.show();
}
                                });

b2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast msg = Toast.makeText(getBaseContext(),
                                                                                "You have clicked Button 2", Toast.LENGTH_LONG);
 msg.show();
}
                                });
 }
}

The OUTPUT will be


https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyoEWEkxJwaajJ4W5qKLOYkxwx5AFCxyqTUQoydQyODDBSwlEEsQ2TAli8lYUMt2BLXQg61Y3Zb4Kcq761FGQeZJBa21fOwh-t9v3rhyphenhyphenW_88CkF7n8YyZ0RsVnOO938QtaaCv6Y2O-mQY/


https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjE3ehMmzk6216yu3GTn7SfcGSnr0W66PxBckWi3Gbu9OIY1o-pW4RLC8xmuuB_waW1IhRJXjiO2TEB_Zf25czS1s_jeIAxGSZlWBZVi7ecqCPkaCDHhO49WZCCh76rhCgQJgj1WsaocDA/
Share this article :

0 komentar:

Posting Komentar

 
Support : Your Link | Your Link | Your Link
Copyright © 2013. Android Center - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger