How to get EditText box entered value on android app

How to retrieve EditText box text string value on button click in android.

EditText box entered value can be easily access by .getText() function. This function is used with the combination of  EditText defined variable. So here is the complete step by step tutorial for How to get EditText box entered value on android app.

How to get EditText box entered value on android app

Code for MainActivity.java file.

package com.android_examples.com.edittext;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

Editable HoldValue;
EditText GetValue;
Button AccessValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

GetValue =(EditText)findViewById(R.id.enter_text);
AccessValue =(Button)findViewById(R.id.get_text);

AccessValue.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

HoldValue = GetValue.getText();
Toast.makeText(MainActivity.this, HoldValue, Toast.LENGTH_SHORT).show();

}
});
}
}

Code for activity_main.xml file.

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.android_examples.com.edittext.MainActivity” >

<EditText
android:id=”@+id/enter_text”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”103dp”
android:ems=”10″
android:inputType=”text” >

<requestFocus />
</EditText>

<Button
android:id=”@+id/get_text”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/enter_text”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”73dp”
android:text=”Click here to retrieve edittext box value” />

</RelativeLayout>

How to get EditText box entered value on android app

Leave a Reply

Your email address will not be published. Required fields are marked *