Stop edittext gaining focus at activity startup android

How to disable automatic input focus gaining on EditText android example.

When application developer user EditText with custom list view then it will automatically take the advantage and gain focus to EditText so my this tutorial will help you to get rid from this error problem. So here is the complete step by step tutorial for Stop edittext gaining focus at activity startup android.

android-project-download-code-button

How to Stop edittext gaining focus at activity startup android.

Layout elements:

  • EditText.
  • List View.

Code for MainActivity.java file.

 package com.stopedittextgainingfocus_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

 EditText edt;
 ListView list;
 String[] listData = new String[] {"Android Examples","Android Studio","SDK Manager","Eclipse"
 ,"Java","JDK"};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 edt = (EditText)findViewById(R.id.editText1);
 list = (ListView)findViewById(R.id.listView1);
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2, android.R.id.text1, listData);
 
 list.setAdapter(adapter);
 }
}

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.stopedittextgainingfocus_android_examples.com.MainActivity"
 android:descendantFocusability="beforeDescendants"
 android:focusableInTouchMode="true" >

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

 <ListView
 android:id="@+id/listView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="14dp" >
 </ListView>

</RelativeLayout>

Screenshot:

Stop edittext gaining focus at activity startup android

Click here to download Stop edittext gaining focus at activity startup android project.