WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

EditText Tutorial Android


It is an overlay over TextView that configures itself to be editable.This is a subclass of TextView with text editing operations.


EditText

EditText code in XML:

<EditText
android:id="@+id/demoEditText"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>

Retrieving From EditText In Java Class:

EditText simpleEditText = (EditText) findViewById(R.id.simpleEditText);
String editTextValue = simpleEditText.getText().toString();

Attributes EditText

id id is an attribute used to uniquely identify a text EditText.
<EditText
android:id="@+id/simpleEditText" 
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
gravityThe gravity attribute is an optional attribute which is used to control left, right, center, top, bottom, center_vertical, center_horizontal etc.
<EditText
    android:id="@+id/simpleEditText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Enter Email address"
    android:gravity="right"/>

text text attribute is used to set the text in a EditText
<EditText
    android:id="@+id/simpleEditText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="First name"/>

setting main activity java
EditText editText = (EditText)findViewById(R.id.simpleEditText);
editText.setText("First name");//set the text in edit text

hint hint is an attribute used to set the hint or what you want to enter in this edit text
<EditText
    android:id="@+id/simpleEditText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:hint="Enter Your mail Here" />

textColor textColor attribute is used to set the text color of a text edit text.
<EditText
    android:id="@+id/simpleEditText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Password"
    android:textColor="#000"/>


textColor in EditText In Java class
EditText demoEditText=(EditText)findViewById(R.id.demoEditText);
simpleEditText.setTextColor(Color.RED);//set the red text color

background background attribute is used to set the background of a edit text
<EditText
    android:id="@+id/simpleEditText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:hint="Enter Your Monospace Here"
    android:padding="20dp"
    android:textColorHint="#fff"
    android:textStyle="bold|italic"
    android:background="#000"/>

EditText Android

Open android studio and create new project name is Editextdemo open res -> layout -> xml (or) 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"
    tools:context=".MainActivity">

<EditText
        android:id="@+id/simpleEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#F2F2F2"
        android:hint="Enter Your Name Here"
        android:padding="15dp"
        android:textColorHint="#000"
        android:textStyle="bold|italic"
        android:layout_marginTop="100dp" />

  <Button
        android:id="@+id/displayText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#000"
        android:padding="10dp"
        android:text="Display Text"
        android:textColor="#0f0"
        android:textStyle="bold" />


</RelativeLayout>

open app -> java -> package -> MainActivity.java


package example.welookups.edittextdemo;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
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);
        final EditText simpleEditText = (EditText) findViewById(R.id.simpleEditText);//get the id for edit text
        Button displayText = (Button) findViewById(R.id.displayText);//get the id for button
        displayText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (simpleEditText.getText().toString() != null)//check whether the entered text is not null
                {
                    Toast.makeText(getApplicationContext(), simpleEditText.getText().toString(), Toast.LENGTH_LONG).show();//display the text that you entered in edit text
                }
            }
        });
    }


}