Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requestFocus not working

<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?

like image 241
UpCat Avatar asked Nov 06 '13 19:11

UpCat


People also ask

What is the use of requestFocus?

- The purpose of the requestFocus() is to get the focus on the particular component and also on the window that contains the component. - Requests that the component gets the input focus.

How do I change the focus to next text in Android?

setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (edtPasscode3. getText(). length() == 1) edtPasscode4. requestFocus(); return false; } });

What is Focusableintouchmode?

This special mode was created for widgets that receive text input, like EditText or, when filtering is enabled, ListView. ... Focusable in touch mode is a property that you can set yourself either from code or XML.


2 Answers

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

txtView.getParent().requestChildFocus(txtView,txtView);
like image 91
Evan Parsons Avatar answered Oct 06 '22 21:10

Evan Parsons


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);
like image 21
nikhil.thakkar Avatar answered Oct 06 '22 20:10

nikhil.thakkar