Android Button Control

Advertisements


A Button is a Push-button which can be pressed, or clicked, by the user to perform an action.

Button Attributes

Following are the important attributes related to Button control. You can check Android official documentation for complete list of attributes and related methods which you can use to change these attributes are run time.

Inherited from android.widget.TextView Class:

AttributeDescription
android:autoTextIf set, specifies that this TextView has a textual input method and automatically corrects some common spelling errors.
android:drawableBottomThis is the drawable to be drawn below the text.
android:drawableRightThis is the drawable to be drawn to the right of the text.
android:editableIf set, specifies that this TextView has an input method.
android:textThis is the Text to display.

Inherited from android.view.View Class:

AttributeDescription
android:backgroundThis is a drawable to use as the background.
android:contentDescriptionThis defines text that briefly describes content of the view.
android:idThis supplies an identifier name for this view,
android:onClickThis is the name of the method in this View's context to invoke when the view is clicked.
android:visibilityThis controls the initial visibility of the view.

Example

This example will take you through simple steps to show how to create your own Android application using Linear Layout and Button.

StepDescription
1You will use Eclipse IDE to create an Android application and name it as GUIDemo4 under a package com.example.guidemo4 as explained in the Hello World Example chapter.
2Modify src/MainActivity.java file to add a click event.
2Modify the detault content of res/layout/activity_main.xml file to include Android UI control.
3Define required constants in res/values/strings.xml file
4Run the application to launch Android emulator and verify the result of the changes done in the application.

Following is the content of the modified main activity file src/com.example.guidemo4/MainActivity.java. This file can include each of the fundamental lifecycle methods.

package com.example.guidemo4;

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

public class MainActivity extends Activity {
	
	private EditText edText1,edText2,edText3;
	private Button btnProduct;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          addListenerOnButton();
	}

	private void addListenerOnButton() {

          edText1 = (EditText)findViewById(R.id.edittext);
          edText2 = (EditText)findViewById(R.id.edittext2);
          edText3 = (EditText)findViewById(R.id.edittext3);
		
          btnProduct = (Button)findViewById(R.id.button1);
          btnProduct.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
               String t1 = edText1.getText().toString();
               String t2 = edText2.getText().toString();
               String t3 = edText3.getText().toString();		
		
               int i1 = Integer.parseInt(t1);
               int i2 = Integer.parseInt(t2);
               int i3 = Integer.parseInt(t3);

               int product = i1*i2*i3;
               Toast.makeText(getApplicationContext(),
               String.valueOf(product),Toast.LENGTH_LONG).show();
            }			
	 });
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
        /* Inflate the menu; this adds items to the action bar
        if it is present */
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
	}
}

Following will be the content of res/layout/activity_main.xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/example_button" />

    <EditText
        android:id="@+id/edittext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="61dp"
        android:ems="10"
        android:inputType="text"
        android:text="@string/enter_text1" />

    <EditText
        android:id="@+id/edittext2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/edittext3"
        android:layout_below="@+id/edittext"
        android:layout_marginTop="17dp"
        android:ems="10"
        android:inputType="text"
        android:text="@string/enter_text2" />

    <EditText
        android:id="@+id/edittext3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/edittext"
        android:layout_below="@+id/edittext2"
        android:layout_marginTop="14dp"
        android:ems="10"
        android:inputType="text"
        android:text="@string/enter_text3" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/edittext3"
        android:layout_marginTop="35dp"
        android:text="@string/click_button" />

</RelativeLayout>

Following will be the content of res/values/strings.xml to define these new constants:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">GUIDemo4</string>
    <string name="action_settings">Settings</string>
    <string name="example_button">Example showing
    Button</string>
    <string name="enter_text1"/>
    <string name="enter_text2"/>
    <string name="enter_text3"/>
    <string name="click_button">Calculate product of 3
    numbers</string>

</resources>

Following is the default content of AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.guidemo4"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.guidemo4.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Let's try to run your GUIDemo4 application. I assume you had created your AVD while doing environment setup. To run the app from Eclipse, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Eclipse installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window:

Android Button Control

The following screen will appear after values are entered in 3 EditText and then the product is calculated by clicking on Button:

Android Button Control

Exercise:

I will recommend to try above example with different attributes of Button in Layout XML file as well at programming time to have different look and feel of the Button. Try to make it editable, change to font color, font family, width, textSize etc and see the result. You can also try above example with multiple Button controls in one activity.



Advertisements
Advertisements