EditText Tutorial android studio
In this tutorial we will learn how to:
✓Use EditText
✓Get Data from EditText
✓Validate EditText
✓Save Data of EditText(on Button click to TextView)
MainActivity.java
✓Use EditText
✓Get Data from EditText
✓Validate EditText
✓Save Data of EditText(on Button click to TextView)
Step 1: Create a new project OR Open your project
Step 2: Code
activity_main.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:padding="10dp" tools:context=".MainActivity"> <EditText android:id="@+id/f_name_et" android:hint="Enter First Name" android:inputType="text" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/l_name_et" android:hint="Enter Last Name" android:inputType="text" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/save_btn" android:text="Save" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/f_name_tv" android:text="First Name:" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/l_name_tv" android:text="Last Name:" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
package com.blogspot.devofandroid.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText mFirstNameEt, mLastNameEt; Button mSaveBtn; TextView mFirstNameTv, mLastNameTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mFirstNameEt = findViewById(R.id.f_name_et); mLastNameEt = findViewById(R.id.l_name_et); mFirstNameTv = findViewById(R.id.f_name_tv); mLastNameTv = findViewById(R.id.l_name_tv); mSaveBtn = findViewById(R.id.save_btn); mSaveBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //get text from edit text String mFName = mFirstNameEt.getText().toString().trim(); String mLName = mLastNameEt.getText().toString().trim(); //validate Edit Text if (mFName.isEmpty()){ Toast.makeText(MainActivity.this, "Enter first name", Toast.LENGTH_SHORT).show(); } else if (mLName.isEmpty()){ Toast.makeText(MainActivity.this, "Enter last name", Toast.LENGTH_SHORT).show(); } else { mFirstNameTv.setText("First Name: "+mFName); mLastNameTv.setText("Last Name: "+mLName); } } }); } }
Note: android:inputType="" is used to define the input type. The possible input types are:
text, none, number, date, datetime, numberDecimal, numberPassword, numberSigned, phone, textAutoComplete, textAutoCorrect, textCapCharacters, textCapSentences, textCapWords, textEmailAddress, textEmailSubject, textFilter, textImeMultiLine, textLongMessage, textMultiLine, textNoSuggestion, textPassword, textPersonName, textPhonetic, textPostalAddress, textShortMessage, textUri, textVisiblePassword, textWebEditText, textWebEmailAddress, textWebPassword, time.
Comments
Post a Comment