Add Requestfocus on EditText programmatically android

Change editText box selection on button click handling using Requestfocus function through programming file.

In my this tutorial i am creating two edittext boxes and two buttons into layout xml file and selecting edittext box on button click event.So here is the complete step by step tutorial for Add Requestfocus on EditText programmatically android.

android-project-download-code-button

How to Add Requestfocus on EditText programmatically android.

Code for MainActivity.java file.

 package com.edittextrequestfocus_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity {

 EditText one,two;
 Button first,second;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 one = (EditText)findViewById(R.id.editText1);
 two = (EditText)findViewById(R.id.editText2);
 first = (Button)findViewById(R.id.button1);
 second = (Button)findViewById(R.id.button2);
 
 first.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 one.requestFocus();
 
 }
 });
 
 second.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 two.requestFocus();
 
 }
 });
 
 }
}

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="17dp"
 android:ems="10"
 android:hint="FIRST" >

 <requestFocus />
 </EditText>

 <EditText
 android:id="@+id/editText2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="22dp"
 android:ems="10"
 android:hint="SECOND" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText2"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="30dp"
 android:text="SELECT FIRST EDITTEXT" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="24dp"
 android:text="SELECT SECOND EDITTEXT" />

</RelativeLayout>

Screenshot:

Add Requestfocus on EditText programmatically android

Click here to download Add Requestfocus on EditText programmatically android project.

Leave a Reply

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