How to Get Value from Edittext on Android

Android development has made coding simpler and convenient, especially with the use of Android Studio. Edittext widgets are widely used in Android, they allow users to input text data into your app and your app can then make use of that data entered by the user. Creating and accessing text inputs in Android app is a critical part of app development. With this guide, you can learn how to get value from EditText on Android.

Video Tutorial:

What’s Needed

Before we proceed, you will need a few things that will be useful in following the steps provided in this guide. They are:

  • An Android device and/or emulator: For the purpose of testing your app.
  • Android Studio: For building and running your app.
  • A sample app or project: To apply the concepts and methods highlighted in this guide.

What Requires Your Focus?

First, it is important to understand that EditText is a subclass of TextView that allows the user to input text. Here are some areas that require your focus:

  • Designing of the view for EditText: This involves changing the appearance and behaviour of the EditText widget that will give users an easy-to-use interface to input text data.
  • Reading value from the EditText: This involves getting the value of the input entered in the EditText widget and utilising it in the app.
  • Validating input entered in EditText: This involves ensuring that only valid input is accepted.

Different Methods to Get Value from EditText on Android

Method 1: Using TextWatcher Class

This method makes use of the TextWatcher class in Android.
– Firstly, declare EditText in your activity.
– Next, create a Textwatcher object and override its methods.
– Attach the TextWatcher to the EditText using the addTextChangedListener() method.
– The onTextChanged() method of the TextWatcher interface will then take the input entered when the text is changed.

Steps:
1. Declare EditText in your activity:
"`Kotlin
val inputEditText = findViewById(R.id.my_edit_text)
"`

2. Create a TextWatcher object by overriding its methods:
"`Kotlin
private val textWatcher = object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// nothing to implement
}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
// action to perform when text is changed
}

override fun afterTextChanged(s: Editable?) {
// nothing to implement
}
}
"`

3. Attach the TextWatcher to the EditText:
"`Kotlin
inputEditText.addTextChangedListener(textWatcher)
"`

4. Get the input entered using the onTextChanged() method:
"`Kotlin
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
// get the input entered
val input = s.toString()
}
"`

Pros:
– Real-time input validation.
– TextBox can handle all types of EditText values.

Cons:
– Adding a TextWatcher object may lead to computation overhead.

Method 2: Using setOnClickListener()

This method utilises the setOnClickListener() method to get input entered in the EditText widgets.
– Firstly, declare EditText in your activity.
– Next, declare the button that will be used to get the input value.
– Set an OnClickListener to the button and get the input entered in the EditText.
– Implement button action in the onClick() method of the OnClickListener interface.

Steps:
1. Declare EditText in your activity:
"`Kotlin
val inputEditText = findViewById(R.id.my_edit_text)
"`

2. Declare the button in your activity:
"`Kotlin
val button = findViewById