requestFocus not working

27,299

Solution 1

Solved it! In manifest I added following to the activity:

android:windowSoftInputMode="stateAlwaysVisible"

Solution 2

I usually use the following to set the focus: Add following attributs to your xml-layout

<AutoCompleteTextView 
    android:focusable="true" 
    android:focusableInTouchMode="true"> 
</AutoCompleteTextView>

and set focus programmatically like

((AutoCompleteTextView) findViewById(R.autocomplete_zone)).requestFocus();

f.e. in onResume or onWindowChanged

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    if (hasFocus) {
        ((AutoCompleteTextView) findViewById(R.autocomplete_zone)).requestFocus();
    }
}

Solution 3

None of the above worked for me... this is what I used

txtView.getParent().requestChildFocus(txtView,txtView);

Solution 4

To do this in code, in your Activity:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

Solution 5

You can try this in the code if you want to forcefully show the keyboard.

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

then you can to use this code to close the keyboard:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(autocomplete_zone.getWindowToken(), 0);
Share:
27,299

Related videos on Youtube

DarkLeafyGreen
Author by

DarkLeafyGreen

Tackling Complexity in the Heart of Software

Updated on September 11, 2020

Comments

  • DarkLeafyGreen
    DarkLeafyGreen over 3 years
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="20dp" >
    
        <AutoCompleteTextView
            android:id="@+id/autocomplete_zone"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:dropDownVerticalOffset="0.2dp"
            android:ems="10"
            android:hint="@string/zone_hint"
            android:inputType="text"
            android:lines="1"
            android:maxLines="1"
            android:popupBackground="#00ffffff"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff" >
    
            <requestFocus />
        </AutoCompleteTextView>
    
        <ProgressBar
            android:id="@+id/progressBar1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignBottom="@id/autocomplete_zone"
            android:layout_alignRight="@id/autocomplete_zone"
            android:layout_alignTop="@id/autocomplete_zone"
            android:paddingBottom="5dp"
            android:visibility="invisible" />
    </RelativeLayout>
    

    In the above markup

    <requestFocus />
    

    does not work. The text view is not focused when the activity is started. Is this because of the progress bar which overlays the text view? Any ideas how to fix this?

  • DSlomer64
    DSlomer64 almost 10 years
    Sorta confused about the down vote... and the edit... Did the edit make the Answer OK? Maybe ignore the Answer?
  • JFreeman
    JFreeman over 6 years
    Thank you very much! I had the same problem and this was the only solution that worked.
  • JFreeman
    JFreeman over 6 years
    This seems like a good answer, and based upon logic it should work but it does not. Instead i found that Evan Parsons solution was the only one that worked for me... why would that be?