Free Education

Online Education

Android Studio

Edit Text Box Code Snippet for Android Studio

 

Welcome. In this tutorial you will learn how to create an Edit Text box on your Android Studio.

Edit Text Box example for Android Studio

An Edit text is a text box in Android Studio where you can show some text, or the user can also enter some text. It is basically like a form to get data from the user.User can either enter or also modify the text present there.

The XML code for creating an EditText UI is

 <EditText
        android:id="@+id/edittext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />

 

Here, we also have an inputType attribute which is set to “text“. We can change the inputType to configure our keyboard for getting different types of Input. For ex- suppose if you want to accept a numeric pin, like any secret pin, then you can specify the inputType as “numericPassword“. This will then open the numeric keyboard when focused, and masks the text as dot or asterisk that is entered for privacy.

We can also receive the text entered by the user by using the getText() function. The syntax is shown below:

EditText eText;
String str = eText.getText().toString();

This will receive the text from the user and convert it to String. You can also convert it later to an Integer or floating type as per your requirement.

 

An example of edit text UI is shown in the picture below, where we gave the input Sample Page and it gave us the same text as output at the bottom as a Toast.

 

Edit text code snippet for Android Studio

The Complete Code Snippet for above example is given below:-

Java (MainActivity.java)

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.EditText;

public class edittext1 extends AppCompatActivity {

    EditText eText;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edittext1);

        eText = (EditText) findViewById(R.id.edittext);
        btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String str = eText.getText().toString();
                android.widget.Toast msg = android.widget.Toast.makeText(getBaseContext(),str, Toast.LENGTH_LONG);
                msg.show();
            }
        });
    }
}

 

XML (activity_main.xml)

<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"
>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="130dp"
android:text="Check" />

<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button"
android:layout_below="@+id/textView1"
android:layout_marginTop="61dp"
android:ems="10"
android:inputType="text" />


</RelativeLayout>

 

You can directly copy paste these snippets to your android studio, and they should work perfectly. Just save and run your project.

For a more detailed guide, you can also visit android’s official guide below:-

https://developer.android.com/reference/android/widget/EditText

Comment down below if you are facing any problems.

 

Also Read:-

Leave a Reply

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