Saturday 19 January 2013

Working with Button

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/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="150dp"
        android:onClick="onClick"
        android:text="Click Me" />

</LinearLayout>

2)Activity:
 
package com.venkool.button;

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 ButtonActivity extends Activity implements OnClickListener {
      /** Called when the activity is first created. */

      Button button;

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

            // set the UI
            button = (Button) findViewById(R.id.button);

            //set the Listener for button
            button.setOnClickListener(this);
      }

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

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


3)Download this project Click Here

No comments:

Post a Comment