My android apps button example

With the popularity of mobile devices like smartphones, iPhones and tablets there are many buyers and users of these devices. Mobile application also becoming popular and in connection with that mobile apps developer like android programmers are also in demand. There are more beginner programmers who want to learn on how to develop android applications. The example android application in this tutorial is on how to use a button. In creating or developing an android application you need first to have your design interface. For example you are going to create an android application that will accept two numbers as input, and then the program will compute the sum of the numbers and display the result. Your interface will need the following a TextView that will be use to prompt and give hint to user, EditText that will be use by user to input numbers and a button that will trigger the click event and will do the instruct on how to compute and add the two numbers.

In developing android application you can use the eclipse, android studio and others as your tools in creating android apps. Regarding the design for your android apps you can create an xml document that is for the design and you can creating the design or interface of your apps using drag and drop in design view or you can code it in text view.

In this tutorial I used the android studio to create example android apps that use a button. To start in our example tutorial on how to create and use button open the android studio application. Click on the activity_main.xml file and write the following code:
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="@+id/widget0"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sum of two numbers"
        android:id="@+id/textView1"
        android:layout_x="90dp"
        android:layout_y="14dp"
        android:autoText="false"
        android:editable="false"
        android:textColor="@android:color/black"
        android:textSize="@dimen/abc_action_bar_content_inset_material"
        android:typeface="sans"> </TextView>

    <TextView android:layout_width="wrap_content"
        android:layout_x="21dp"
        android:layout_height="wrap_content"
        android:text="Enter Num 1:"
        android:id="@+id/textView2"
        android:layout_y="95dp"
        android:textColor="@android:color/black"> </TextView>

    <EditText android:text=""
        android:layout_width="99dp"
        android:layout_x="161dp"
        android:id="@+id/num1"
        android:layout_height="wrap_content"
        android:layout_y="76dp"
        android:textColor="@android:color/black"> </EditText>

    <TextView android:layout_width="wrap_content"
        android:layout_height="28dp"
        android:text="Enter Num 2:"
        android:id="@+id/textView3"
        android:layout_x="21dp"
        android:layout_y="146dp"
        android:textColor="@android:color/black"> </TextView>

    <EditText android:text=""
        android:layout_width="99dp"
        android:layout_x="161dp"
        android:id="@+id/num2"
        android:layout_height="wrap_content"
        android:layout_y="128dp"
        android:textColor="@android:color/black"> </EditText>

    <Button android:layout_width="wrap_content"
        android:id="@+id/calculate"
        android:layout_x="54dp"
        android:layout_height="wrap_content"
        android:text="Calculate"
        android:layout_y="310dp"> </Button>

    <Button android:layout_width="102dp"
        android:id="@+id/clear"
        android:layout_x="160dp"
        android:layout_height="wrap_content"
        android:text="Clear"
        android:layout_y="310dp"> </Button>

    <EditText android:text=""
        android:layout_width="285dp"
        android:layout_x="12dp"
        android:id="@+id/sum"
        android:layout_height="wrap_content"
        android:layout_y="188dp"
        android:textColor="@android:color/black"> </EditText>
</AbsoluteLayout>

The design will look like this:
android apps button example

After creating the design of application you need to have a code. Click on the MainActivity.java and write the following code:

package com.kid.sumofnumbers;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
    EditText num1;
    EditText num2;

    TextView sum;

    Button calculate;
    Button clear;

    double dblnum1=0;
    double dblnum2=0;
    double dblsum=0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initControls();

    }
    private void initControls()
    {
        num1=(EditText)findViewById(R.id.num1);
        num2=(EditText)findViewById(R.id.num2);
        sum=(TextView)findViewById(R.id.sum);

        calculate=(Button)findViewById(R.id.calculate);
        calculate.setOnClickListener(new Button.OnClickListener() {
            public void onClick
                    (View v) {
                calculate();
            }
        });

        clear=(Button)findViewById(R.id.clear);
        clear.setOnClickListener(new Button.OnClickListener() {
            public void onClick
                    (View c) {
                clear();
            }
        });
    }

    private void calculate()
    {
        dblnum1 = Double.parseDouble(num1.getText().toString());
        dblnum2 = Double.parseDouble(num2.getText().toString());

        dblsum= dblnum1+dblnum2;

        sum.setText( String.format( "Sum: %.2f", dblsum));
    }

    private void clear()
    {
        sum.setText("");
        num1.setText("");
        num2.setText("");

        EditText mEdit;
        mEdit = (EditText)findViewById(R.id.num1);
        mEdit.requestFocus();
    }
}