• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
Posted by Jack - - 7 comments


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://lh5.googleusercontent.com/_xnrF0YSUul8/TbkRukOp9rI/AAAAAAAAAIM/orjOySOvp0g/simple_button1.png


https://lh3.googleusercontent.com/_xnrF0YSUul8/TbkRuhUTv2I/AAAAAAAAAIQ/CMWs8yNDJ68/simple_button2.png

7 Responses so far.

  1. I have a main screen where there are 2 button. I want to display a 5 lines of text on click of 1st button and there again on click of back button i have to come to previous main screen. Please tell me how to do it.thanks in advance

  2. thanks..im beginner..so its very helpful for me.!

  3. awesome !! it helped a lot

  4. Button and its click events in android
    http://themasterworld.com/android-button-help-to-create-button/

Leave a Reply