Download Code

Android Studio EditText Example

In android studio edittext example you will learn how to use Edit Text in android application.Also some basic information about Text Fields and Input Controls.

Input Controls

Input Controls are the interactive app UI components.Android provide some controls like

  • Button
  • Text Fields
  • Check Box
  • Radio
  • Spinner
  • Seek bar
  • Pickers

Text Fields

Text Field allows the user to type text in android application.Text Field can be single line or multiple line.For example Edit Text.There are some different types of Edit Text.

  • Plain Text
  • Person Name
  • Password
  • Email
  • Postal Address
  • Phone
  • Time
  • Date

Now let’s talk about Edit Text.

Edit Text

Basically used to take user input for any particular purpose.Subclass of Text View which turned itself into Edit Text.

So you will code for edit text and display in Toast on button click what user will input in that edit text.

Create android studio edittext example application project.Then go to your activity_main.xml file and add EditText,Button.

activity_main.xml
android studio edittext,android studio edittext example,edittext example,edittext,

After that go to your MainActivity.java class and do the following steps.

  • Create/declare variables for EditText,Button.
  • Create a public method to refer id’s of UI Components.
  • Apply click event on button
  • Display toast of edittext on button click.

MainActivity.java

package com.example.anonymous.studioedittext;

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.Toast;

public class MainActivity extends AppCompatActivity {

    EditText userInput;   // user input variable
    Button displayInput;  // button variable
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();  // calling user defined method
        // apply click event on button
        displayInput.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // displaying toast and also getting user input using getText() method
                Toast.makeText(getApplicationContext(), userInput.getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
    // user defined method
    private void initialize(){
        userInput = (EditText) findViewById(R.id.editText1);   // refering id of edit text
        displayInput = (Button) findViewById(R.id.button1);    // refering id of button
    }

}

Compile and run your android studio edittext example application project.

android studio edittext,android studio edittext example,edittext example,edittext,

That’s it for this android studio edittext example.

Don’t forget to leave your feedback.

Thanks.