Pages

Sunday, 15 September 2013

Add EditText numbers and Diplay on TextView in Android

This is very simple application which takes values from edit text boxes and add them and display add to text view when we click on a button. So, create a new project and drop edit text boxes and text views and a button. The code of android XML file is given below:

Add EditText numbers and Diplay on TextView in Android

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:background="#373">
<TextView 
  android:id="@+id/textView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Enter First Value:"
  android:textSize="25sp" />
 
<EditText
  android:id="@+id/editText1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:ems="10"
  android:inputType="numberDecimal">
 </EditText>
 
<TextView
  android:id="@+id/textView2" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Enter Second Value:"
  android:textSize="25sp" />
 
<EditText
  android:id="@+id/editText2"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:inputType="numberDecimal"
  android:ems="10" />

<TextView
  android:id="@+id/result"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="30sp" />
 
<Button
  android:id="@+id/button1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Add Values"
  android:onClick="add"
/> 
</LinearLayout>


Now open your Java file and initialize all objects. Take values from edit text boxes and convert them into double and add them and set on text view. The code of android Java file is given below with explanation:


package com.example.checkblogapp; //your package name
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }

 //This method is called when we click on add button
 //and it is declare in main XML file in button tag
 //we have to pass View object in this method
 
 public void add(View v)
 {
 //initialize objects
 TextView result=(TextView)findViewById(R.id.result);
 EditText et1=(EditText)findViewById(R.id.editText1);
 EditText et2=(EditText)findViewById(R.id.editText2);

 // get text from edit text boxes and convert into double
 double a=Double.parseDouble(String.valueOf(et1.getText()));
 double b=Double.parseDouble(String.valueOf(et2.getText()));
 
 //add them
 double c=a+b;

 //display addition on TextView
 result.setText("Add:"+c);
 
 }
}

Now run your project and test application. If you have any doubt please comment.
Share and help other.

Related Tutorials:-

Make Temperature conversion application

Create Menu using XML

Create Menu using Java

Access Call, Camera, Web pages

Open Second Activity using Intent

1 comment:

  1. hello there, can u give me a tutorial on how to create mycontact application, that can add contact, display it and retrieve @ search the contact and make a phone call.. i already read your tutorial but i didnt find all those kind of terms in 1 tutorial..can you help me?

    ReplyDelete

Back to Top