Android - How to get value of EditText?

Android | Set and get the value of EditText: In this tutorial, we will learn how to set and fetch/get the value of EditText in Android with the help of an example. By Manu Jemini Last updated : June 06, 2023

Set and Get value of EditText in Android

The setText() method is used to set the value of EditText and getText() is used for getting the value of EditText.

Syntaxes

EditText demo;
demo=(EditText)findViewById(R.id.demo);

After this you can use,

demo.getText();
demo.setText();

In below code, we took four EditText name, lastname, mobile and address and then use findviewbyid to looks through XML layout and returns reference to a view (In this case this is an EditText) and after that get text from all these EditText and set them in a TextView in a form of string.

Example: How to get value of EditText in Android

Consider the below-given files contains the source codes for this example.

Java File

package com.example.hp.demo;

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;

public class MainActivity extends AppCompatActivity {
    EditText name,lastname,mobile,address;
    TextView details;
    Button click;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name=(EditText)findViewById(R.id.name);
        lastname=(EditText)findViewById(R.id.lastname);
        mobile=(EditText)findViewById(R.id.mobile);
        address=(EditText)findViewById(R.id.address);
        click=(Button)findViewById(R.id.click);
        details=(TextView)findViewById(R.id.details);

        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                details.setText("Name: "+name.getText()+lastname.getText()+"\nMobile: "+mobile.getText()+"\nAddress: "+address.getText());
            }
        });
    }

}

XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:layout_gravity="center_horizontal"
        android:hint="Name" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lastname"
        android:hint="Last Name" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/mobile"
        android:layout_gravity="center_horizontal"
        android:hint="Mobile No." />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/address"
        android:hint="Address" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click"
        android:id="@+id/click" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/details"
        android:layout_weight="0.28"
        android:hint="Details" />
</LinearLayout>

Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

get value of EditText 1
get value of EditText 2


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.