Android tutorial EditText



EditText is a text box where a user can enter text.
In this android tutorial we are going to know how to use edit text through a simple example.
In the following example we are going to create a simple app that displays the Text entered in the EditText as a Toast message.
Don’t worry about Toast and Button you will learn about them in next chapters of this course right now just learn how to use EditText.

Here is a simple Android EditText Example

First create a new project in Android Studio
Then click on Design View
Then in the XML file drag and drop a Plain Text field from the Palette. Also drag a button from the palette and drop it below the EditText field.
Double click on the EditText to give it an id which is the identity of the EditText in the Java file.
Also double click on the button to assign it a name.
 


You can also copy and paste the following code in your .xml file 

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    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"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main">



    <EditText

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/eText"

        android:layout_alignParentTop="true"

        android:layout_alignParentStart="true"

        android:layout_marginTop="170dp"

        android:layout_alignParentEnd="true" />



    <Button

        style="?android:attr/buttonStyleSmall"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Button"

        android:id="@+id/button"

        android:layout_below="@+id/eText"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="66dp"

        android:onClick="onButtonClick" />

</RelativeLayout>


Now go to  .java file. MainActiviy.java in my case and enter the code given below

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

public class MainActivity extends AppCompatActivity {

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

           }
   
public void onButtonClick(View view){
      
        EditText eText = (EditText)findViewById(R.id.
eText);

        Toast.makeText(MainActivity.
this,"You entered "+" " +eText.getText(),Toast.LENGTH_SHORT)
                .show();
    }
}


Now just go to the .xml file and select the button and on right side in the properties of the button  scroll down and in the onClick property select the onButtonClick method.

See the picture below if you don't know where the properties are

Android Studio
 So this was a simple android edittext example


0 comments: