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

 

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:

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)

 

XML (activity_main.xml)


 

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 *