Saturday, 23 February 2013

Button Working using onClick method

Demo:





1)main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#5F04B4"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="100dp"
        android:onClick="click"
        android:text="Click"
        android:textSize="20dp" />

</LinearLayout>

2)Activity:

package com.venky.buttononclick;

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

public class ButtonWorkingthroughOnClickActivity extends Activity {
      /** Called when the activity is first created. */

      Button btnClick;

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

            btnClick = (Button) findViewById(R.id.button1);
      }

      public void click(View v) {
            // TODO Auto-generated method stub

            Toast.makeText(getApplicationContext(), "Button is Clicked",
                        Toast.LENGTH_SHORT).show();
      }
}


3)Download this Project Click Here


No comments:

Post a Comment