Add EditText numbers and Diplay on TextView in Android

Add EditText numbers and Diplay on TextView in Android - Hello friends U-See, In the article you read in this time with the title Add EditText numbers and Diplay on TextView in Android, We have prepared this article well for you to read and take the information in it. Hopefully the contents of the post Artikel Android, Which we write you can understand. Okay, happy reading.

Title: Add EditText numbers and Diplay on TextView in Android
link : Add EditText numbers and Diplay on TextView in Android

Read also


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.


That's Add EditText numbers and Diplay on TextView in Android

That's an article Add EditText numbers and Diplay on TextView in Android This time, hopefully can benefit for you all. Well, see you in other article postings.

You are now reading the article Add EditText numbers and Diplay on TextView in Android With link address https://u-see1.blogspot.com/2017/06/add-edittext-numbers-and-diplay-on.html

Postingan terkait:

Belum ada tanggapan untuk "Add EditText numbers and Diplay on TextView in Android"

Post a Comment