- String value (Name or some other text)
- Integer value (Positive and Negative Numbers without decimal point)
- Decimal or Double values (Positive and Negative number with a decimal point)
Get String value from EditText
First, you need to create EditText that is capable of inputting text value from the user. For this, you will have to set android:inputType="text" in the XML. The XML code of EditText is:
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:inputType="text" />
To get its value in java code, first, find the EditText object and then extract its value.
EditText editText=findViewById(R.id.editText);
String value=editText.getText().toString();
Also Read: Set EditText to input Numbers only
Get Integer value from EditText
Create an EditText that is restricted to input signed or unsigned Integer. See the full guide on EditText with numbers here. You can set android:inputType="numberSigned" to input Integers.<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:inputType="numberSigned" />
Now get the Integer value in Java code. First, find the EditText, then get its value and then parse the value to Integer. Here it is also important to check if the EditText is empty. If it is empty, do not try to convert the received value to int. Otherwise, the application will crash.
EditText editText=findViewById(R.id.editText);
String temp=editText.getText().toString();
int value=0;
if (!"".equals(temp)){
value=Integer.parseInt(temp);
}
Get Decimal or Double value from EditText
Create EditText to input decimal values. See full instructions here. Set android:inputType="numberDecimal" in XML.
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:inputType="numberDecimal" />
Now get value in Java code. Make sure to check if the EditText is empty.
EditText editText=findViewById(R.id.editText);
String temp=editText.getText().toString();
double value=0;
if (!"".equals(temp)){
value=Double.parseDouble(temp);
}
Get value from EditText and set it to TextView
First, we create an EditText, a button and a TextView in XML file. Following is the code for activity_main.xml<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now in the onClickListener of the button, get the value from EditText and set it to the TextView. Following is the code for MainActivity.java
package com.umersoftwares.edittext;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText=findViewById(R.id.editText);
Button button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((TextView) MainActivity.this.findViewById(R.id.textView)).setText(editText.getText().toString());
}
});
}
}
How to check if the EditText is empty?
Get the text from EditText and check if its length is zero. If its length is zero, the edittext will be empty.
EditText editText=findViewById(R.id.editText);
String text=editText.getText().toString();
if (text.length()==0){
//EditText is empty
Toast.makeText(this, "Empty", Toast.LENGTH_SHORT).show();
}else{
//EditText is not empty
Toast.makeText(this, "Not Empty", Toast.LENGTH_SHORT).show();
}
You can also check if the text is equal to empty String like I did in the examples above.
if ("".equals(text)){
//EditText is empty
Toast.makeText(this, "Empty", Toast.LENGTH_SHORT).show();
}else{
//EditText is not empty
Toast.makeText(this, "Not Empty", Toast.LENGTH_SHORT).show();
}
I hope this post helped you. If it did, share this with others.
0 Comments
Thanks for leaving a comment. I will try to reply you soon.