Android - How to get value of EditText?

In this article, we are going to learn how to fetch/get the value of EditText in Android?
Submitted by Manu Jemini, on November 27, 2017

Now, before we start we need to know some method of EditText which we use to get or fetch the text written in an EditText that is .getText() method and the other one is .setText() method that we use to set some pre-written text or string.

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.

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

Related Tutorials

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.