Studyon Minte9.com
Android




Buttons



1) Add a button (or imageButton)

	%--- /res/layout/activity_main.xml

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    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=".MainActivity" >

	    <Button
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="@string/button_text" 
		    />
	    
	    <ImageButton
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:src="@drawable/ic_device_access_alarms"
		    />
	    
	    <Button
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="@string/button_text" 
		    android:drawableLeft="@drawable/ic_device_access_alarms"
		    />
	    
	</LinearLayout>


2) Click events

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in 
your XML layout.

	
	%--- /res/layout/activity_main.xml

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    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=".MainActivity" >

	    <Button
		android:id="@+id/button_send"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="@string/button_text" 
		    android:onClick="sendMessage"	    
		    />
	    
	</LinearLayout>


	%--- MainActivity.java

	package com.example.myandroidapi2;

	import android.app.Activity;
	import android.os.Bundle;
	import android.view.View;
	import android.widget.TextView;

	public class MainActivity extends Activity {

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_main);
		}
		
		/** Called when the user touches the button */
		public void sendMessage(View view) {
			
			TextView message = new TextView(this);
			message.setText("I've been clicked!");
			setContentView(message);
		}

	}



3) OnClickListener

	%--- activity_main.xml

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    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=".MainActivity" >

	    <Button
		android:id="@+id/button_send"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="@string/button_text" 
		    />
	    <!--  android:onClick="sendMessage"	 -->
	    
	</LinearLayout>


	%--- MainActivity.java

	package com.example.myandroidapi2;

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

	public class MainActivity extends Activity {

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_main);
			
			/* -- LOOK HERE -- */
			Button button = (Button) findViewById(R.id.button_send);
			button.setOnClickListener(new View.OnClickListener() {
			    public void onClick(View v) {
				sendMessage(v);
			    }
			});
		}
		
		public void sendMessage(View view) {
			
			TextView message = new TextView(this);
			message.setText("I've been clicked!");
			setContentView(message);
		}

	}


http://developer.android.com/guide/topics/ui/controls/button.html
http://developer.android.com/design/downloads/index.html