Android EditText Requestfocus example through layout xml

Add requestfocus to select any editText from multiple edittext selection to open keypad automatically on activity starts.

Request focus is used to set automatically keypad function on edittext box so just after activity starts it will automatically select defined Requestfocus editText and open keypad so application user can directly insert data into editText box. So here is the complete step by step tutorial for Android EditText Requestfocus example through layout xml file.

android-project-download-code-button

Android EditText Requestfocus example through layout xml.

Application developer can add automatic selection Requestfocus by implementing <requestFocus /> tag inside layout xml file editText which user wants to selected automatically on activity start time.

Code for MainActivity.java file.

package com.edittextrequestfocus_android_examples.com;
import android.app.Activity;
import android.os.Bundle;


public class MainActivity extends Activity {

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

Code for activity_main.xml layout file.

 <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="com.edittextrequestfocus_android_examples.com.MainActivity" >

 <EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="160dp"
 android:ems="10"
 android:hint="EditText box 1" />

 <EditText
 android:id="@+id/editText2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/editText1"
 android:layout_below="@+id/editText1"
 android:layout_marginTop="16dp"
 android:ems="10"
 android:hint="EditText box 2" >
 <requestFocus />
 </EditText>

 <EditText
 android:id="@+id/editText3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText2"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="16dp"
 android:ems="10" 
 android:hint="EditText box 3">

 
 </EditText>

</RelativeLayout>

Screenshot:

Android EditText Requestfocus example through layout xml

Click Here to Download Android EditText Requestfocus example through layout xml project.

Leave a Reply

Your email address will not be published. Required fields are marked *