What is Button

A button is a widget that the user can use for click or press to perform some action like adding numbers executing query , play the video etc.

Here i am going to demonstrate the use of button in android.


Button:.

First of all you need to create the project and give the name as buttondemo.

Drag and drop three EditText(Number) and one Button . The first two EditText is used for taking the input from the user and in third EditText we will display the result after clicking on the Button.

Smiley face Smiley face

Open the acitvity_main.xml file and the code look like this.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.niitpc.myapplication.MainActivity">
<EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginLeft="70dp" android:layout_marginStart="70dp" android:layout_marginTop="50dp" android:ems="10" android:inputType="number" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText" android:layout_alignStart="@+id/editText" android:layout_below="@+id/editText" android:layout_marginTop="37dp" android:ems="10" android:inputType="number" /> <EditText android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_alignStart="@+id/editText2" android:layout_below="@+id/editText2" android:layout_marginTop="40dp" android:ems="10" android:inputType="number" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText3" android:layout_alignStart="@+id/editText3" android:layout_below="@+id/editText3" android:layout_marginLeft="38dp" android:layout_marginStart="38dp" android:layout_marginTop="64dp" android:onClick="add" android:text="Button" /> </RelativeLayout>

Open the MainActivity.java file and write the following code for addition of two numbers.

package com.example.niitpc.myapplication;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

    EditText first,second,result;
    Button sum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        first=(EditText)findViewById(R.id.editText);
        second=(EditText)findViewById(R.id.editText2);
        result=(EditText)findViewById(R.id.editText3);

    }
    public void add(View v)
    {
        String x=first.getText().toString();
        String y=second.getText().toString();
        int number1=Integer.parseInt(x);
        int number2=Integer.parseInt(y);
        int s=number1+number2;
        result.setText(String.valueOf(s));

    }

}
       

Explanation:

EditText first,second,result; here we have to created three object of EditText

Button sum; here we have to created one object of Button.

1.first=(EditText)findViewById(R.id.editText);This statement initialized the object first of the EditText.

second=(EditText)findViewById(R.id.editText2);This statement initialized the object second of the EditText

result=(EditText)findViewById(R.id.editText2);This statement initialized the object result of the EditText

After that we have to created a method add(View v)

String x=first.getText().toString();This statement take the value from first object with the help of getText() method and assign into the String variable x.

String y=second.getText().toString();This statement take the value from first object with the help of getText() method and assign into the String variable y.

int number1=Integer.parseInt(x);This statement assign the value of x after conversion into number1 variable.

int number2=Integer.parseInt(y);This statement assign the value of y after conversion into number1 variable.

int s=number1+number2;This statement add the value of number1 and number2 and assign into s.

result.setText(String.valueOf(s));This statement set the value s after conversion of String into result.and this will display the result on to the screen.

do not forget assign the add() to Button onClick property. I have mention it into activity_main.xml file.


button

what is button for video click here

Smiley face                     Smiley face