Skip to content
Fix Code Error

How to stop EditText from gaining focus at Activity startup in Android

March 13, 2021 by Code Error
Posted By: Anonymous

I have an Activity in Android, with two elements:

  1. EditText
  2. ListView

When my Activity starts, the EditText immediately has input focus (flashing cursor). I don’t want any control to have input focus at startup. I tried:

EditText.setSelected(false);
EditText.setFocusable(false);

No luck. How can I convince the EditText to not select itself when the Activity starts?

Solution

Excellent answers from Luc and Mark however a good code sample is missing. Adding the tag android:focusableInTouchMode="true" and android:focusable="true" to parent layout (e.g. LinearLayout or ConstraintLayout) like the following example will fix the problem.

<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
    android:focusable="true" 
    android:focusableInTouchMode="true"
    android:layout_width="0px" 
    android:layout_height="0px"/>

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:nextFocusUp="@id/autotext" 
    android:nextFocusLeft="@id/autotext"/>
Answered By: Anonymous

Related Articles

  • Android Linear Layout Parameters
  • Combining items using XSLT Transform
  • How to avoid "module not found" error while calling…
  • Programmatically Hide/Show Android Soft Keyboard
  • ListView inside ScrollView is not scrolling on Android
  • Reference - What does this regex mean?
  • How to implement HorizontalScrollView like Gallery?
  • Android : Fragment doesn't fit into Fragment Container
  • AppCompat v7 r21 returning error in values.xml?
  • MaterialCardView is in front of NavigationView. How…
  • Is CSS Turing complete?
  • java.lang.NullPointerException: Attempt to invoke…
  • Remove Text from the edit text when edit text is focused
  • Unable to move from one fragment to another
  • how to show progress bar(circle) in an activity…
  • Android adding simple animations while…
  • Fragment Not Showing in the Activity
  • Use of Jquery on scroll event
  • How to set dropdown arrow in spinner?
  • Error in styles_base.xml file - android app - No…
  • How to center the content inside a linear layout?
  • How to show one layout on top of the other…
  • Maven2: Missing artifact but jars are in place
  • Android ListView headers
  • LinearLayout not expanding inside a ScrollView
  • Navigation View and Constraint Layout
  • Android:java.lang.OutOfMemoryError: Failed to…
  • Fragment not showing properly
  • How do I align views at the bottom of the screen?
  • Unable to run Robolectric and Espresso with a…
  • onClick for buttons on custom CardView
  • Constraint Layout Vertical Align Center
  • Strange out of memory issue while loading an image…
  • Text size and different android screen sizes
  • Android + Pair devices via bluetooth programmatically
  • How to put text view in the center of the layout if…
  • How to Intialize the Binding Properties in the…
  • Android ConstraintLayout - position view to center…
  • DatePicker onDateSet method not running
  • Fix top buttons on scroll of list below
  • How to customize listview using baseadapter
  • Android: how to draw a border to a LinearLayout
  • For loops print only last value
  • Android: How to Programmatically set the size of a Layout
  • Android ListView Text Color
  • Issue with edittexts when using inside tag
  • Union of multiple Database queries with same parameters
  • Convert array to nested JSON object - Angular Material tree
  • Error inflating class…
  • Android Layout Right Align
  • android.content.res.Resources$NotFoundException:…
  • ConstraintLayout 2 TextView first one ellipsize
  • Button is null object reference (Android Studio)
  • Creating a SearchView that looks like the material…
  • Defining a percentage width for a LinearLayout?
  • Centering in CSS Grid
  • How do i calculate total without manually inputting it?
  • Active tab issue on page load HTML
  • Android Layout Animations from bottom to top and top…
  • No adapter attached; skipping layout on Kotlin
  • Failed to load AppCompat ActionBar with unknown…
  • Add Items to ListView - Android
  • How do I setup an on-click event listener on content…
  • Why Do I get a Stack Overflow error when using…
  • How to position a textView above recyclerView?
  • Android ListView selected item stay highlighted
  • How to add 'load more' functionality to items on a…
  • How to pass values between Fragments
  • How to center the elements in ConstraintLayout
  • Couldn't load memtrack module Logcat Error
  • Android custom Row Item for ListView
  • Android app unable to start activity componentinfo
  • Only last child from Firebase is displayed in recyclerview
  • Getting duplicated cardviews using SwipeRefresh Layout
  • ListView show item only after refresh
  • What are the nuances of scope prototypal /…
  • Android App crashes on animated gradient background
  • Unable to get Notification pop-up
  • OnItemClickListener using ArrayAdapter for ListView
  • Android activity life cycle - what are all these…
  • android.view.InflateException: Binary XML file line…
  • NullPointerException: Attempt to invoke virtual…
  • Android list view inside a scroll view
  • Percentage width in a RelativeLayout
  • Android TextView Text not getting wrapped
  • java.lang.RuntimeException: Unable to start activity…
  • Xamarin Forms - Make custom cell bind to original…
  • Error inflating class android.support.v7.widget.Toolbar?
  • Display Grid does not work in Polymer correctly
  • Playing HTML5 video on fullscreen in android webview
  • How come my widgets aren't positioned properly when…
  • Put buttons at bottom of screen with LinearLayout?
  • Iron flex layout: not getting the proper layout
  • Set margins in a LinearLayout programmatically
  • Move to another EditText when Soft Keyboard Next is…
  • Duplicate ID, tag null, or parent id with another…
  • What is your most productive shortcut with Vim?
  • How do you close/hide the Android soft keyboard using Java?
  • How do I create a new TextView and display it? (with…
  • Fit Image in ImageButton in Android

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Notepad++ add to every line

Next Post:

How to for each the hashmap?

Leave a Reply Cancel reply

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

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error