Add two number by using Edit text , Text view and Button
Activity main xml Code:
<?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="com.example.seam.addtonumber.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginTop="161dp"
app:layout_constraintTop_toBottomOf="@+id/editText"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintHorizontal_bias="0.39"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="203dp"
android:layout_height="30dp"
android:layout_marginBottom="25dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/editText2"
app:layout_constraintVertical_bias="0.87" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText"
app:layout_constraintVertical_bias="0.159"
app:layout_constraintHorizontal_bias="0.39" />
</android.support.constraint.ConstraintLayout>
Main Activity java code:
package com.example.seam.addtonumber; import android.provider.Settings; 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.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private EditText edittext1,edittext2; TextView tv; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setOnbutton(); } public void setOnbutton() { Button bt=(Button) findViewById(R.id.button); //EditText e=(EditText)findViewById(R.id.editText); edittext1=(EditText)findViewById(R.id.editText); edittext2=(EditText)findViewById(R.id.editText2); tv=(TextView)findViewById(R.id.textView); bt.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String value1=edittext1.getText().toString(); String v2=edittext2.getText().toString(); int a=Integer.parseInt(value1); int b=Integer.parseInt(v2); tv.setText(String.valueOf(a+ b)); } }); } }
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন