8 Event handling





Algorithm:
Step 1) Create a new Android Studio Project named as eventhandling.

Step 2)Goto Project Structure and open activity_main.xml file

Step 3) Create two EditText boxes and Button in Activity_main.xml file or drag and drop from the
palette.

Step 4) Add android:onClick=”add” attribute to Button in the activity_main.xml file.
Implement the show method created in MainActivity.java file.

      public void add(View view) {
       }


Step 5) Add EditText and Button constraints to left,righ t,top and bottom of the parent view.

Step 6) Run the application from the menu bar select Run and Click on Run App option.


Program:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">

<EditText
android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="84dp" android:layout_marginStart="85dp" android:layout_marginTop="64dp" android:ems="10" android:inputType="textPersonName" android:hint="Enter First Number" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />

<EditText android:id="@+id/editText2"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="84dp" android:layout_marginStart="85dp" android:layout_marginTop="64dp" android:ems="10" android:inputType="textPersonName" android:hint="Enter Second Number" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />

<Button android:id="@+id/button"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="180dp" android:layout_marginEnd="148dp" android:layout_marginStart="148dp" android:layout_marginTop="63dp" android:text="ADD" android:onClick="add"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editText2" />
</android.support.constraint.ConstraintLayout>

MainActivity.java
package com.example.eventhandling;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;
public class MainActivity extends AppCompatActivity { EditText editText1,editText2;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
}
public void add(View view) { editText1=(EditText)findViewById(R.id.editText1); editText2=(EditText)findViewById(R.id.editText2); String value1=editText1.getText().toString(); String value2=editText2.getText().toString();
int a=Integer.parseInt(value1); int b=Integer.parseInt(value2); int sum=a+b;

Toast.makeText(getApplicationContext(),String.valueOf(sum),Toast.LENGTH_SHORT)
.show();

}
}


OUTPUT:

Comments

Popular posts from this blog

20 Simple Content Provider Example

14 Popup Menu

19 SQLIte SImple Database Example