veerlas
veerlas

Reputation: 41

TextInputEditText in android not getting focus

i have a spinner and 2 TextInputEditText inside a linear layout.

inside a fragment i added this layout to the popup window. for spinner i added a custom adapter with image and text. the TextInputEditText are not getting focus, i tried all the possible ways.

tried with below possible conditions

m_PopupWindow.Focusable = true;
 m_PopupWindow.SoftInputMode = SoftInput.StateVisible;
 m_PopupWindow.OutsideTouchable=(true);
 m_PopupWindow.Touchable=(true);
 m_PopupWindow.Update();

and also tried getting the keypad to focus with TextInputEditText click event, with this able to open the keypad but the TextInputEditText is not getting the focus.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:background="@drawable/popupBorder"
    android:focusableInTouchMode="true"
    xmlns:app="http://schemas.android.com/apk/res-auto">
         <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:orientation="vertical"
        android:gravity="center_horizontal">

         <Spinner
          android:id="@+id/spinner"
          android:gravity="center_horizontal"
          android:layout_width="328dp"
          android:minWidth="15dp"
          android:layout_height="wrap_content"
            style="@style/mySpinnerItemStyle"
          android:background="@drawable/bg_spinner"
          android:layout_marginTop="40dp"/>

      <android.support.design.widget.TextInputLayout
      android:id="@+id/txtUserName"
        android:layout_width="328dp"
        android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            >

        <android.support.design.widget.TextInputEditText
            android:id="@+id/userNameEditText"
            android:layout_width="328dp"
            android:layout_height="wrap_content"
                android:textIsSelectable ="true"
                android:focusable="true"
            android:imeActionLabel="Done"
            android:singleLine="true"/>

    </android.support.design.widget.TextInputLayout>

      <android.support.design.widget.TextInputLayout
      android:id="@+id/txtPassword"
        android:layout_width="328dp"
        android:layout_height="wrap_content"
                app:passwordToggleEnabled="true"
                app:passwordToggleTintMode="src_atop"
                app:passwordToggleTint="@drawable/selector_password_visibility_toggle"
                app:passwordToggleDrawable="@drawable/ibk_visible"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_marginTop="16dp"
                android:layout_marginBottom="20dp">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/PasswordEditText"
            android:layout_width="328dp"
            android:layout_height="wrap_content"
            android:imeActionLabel="Done"
            android:singleLine="true"
                android:inputType="textPassword"/>

    </android.support.design.widget.TextInputLayout>

            </LinearLayout>




View objPopupview = LayoutInflater.From(ViewGroup.Context).Inflate(Resource.Layout.AddAccountTabPage, null);
            m_PopupWindow = new PopupWindow(objPopupview, LayoutParams.WrapContent, LayoutParams.WrapContent, true);
            m_PopupWindow.Focusable = true;
             m_PopupWindow.SoftInputMode = SoftInput.StateVisible;
            m_PopupWindow.OutsideTouchable=(true);
            m_PopupWindow.Touchable=(true);
            m_PopupWindow.Update();

 txtUserNameEditBox.Click += delegate
                {
                    txtUserNameEditBox.RequestFocus();
                    txtUserName.RequestFocus();
                    txtUserName.FocusableInTouchMode = true;
                    InputMethodManager imm = (InputMethodManager)ViewGroup.Context.GetSystemService(Context.InputMethodService);
                    imm.ShowSoftInput(txtUserName, ShowFlags.Forced);
                    imm.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
                };

Upvotes: 4

Views: 3273

Answers (2)

Jessie Zhang -MSFT
Jessie Zhang -MSFT

Reputation: 13879

You can try to remove the following code in your root layout.

   android:descendantFocusability="blocksDescendants"

Note: When you set blocksDescendants to android:descendantFocusability , then the ViewGroup will block its descendants from receiving focus.

Upvotes: 0

Giddy Naya
Giddy Naya

Reputation: 4658

You are doing a couple of things wrong.

Remove the following attributes from parent LinearLayout

// remove this from parent layouts
android:focusableInTouchMode="true" 

You only use android:focusableInTouchMode="true" on parent ViewGroup so that its contents are read all at once for screen reader users making use of keyboard navigation, remote control or trackball which would require the view to be first selected, then follows and extra action like click an OK to trigger action/focus events.

...

Focusable in touch mode is a property that should be used sparingly and only in very specific situations as it breaks consistency with normal Android behavior.

Unless you expect the above stated behaviour, that line is unnecessary.

Secondly, remove the following attributes from parent LinearLayout. This i guess may be the culprit of your major challenge:

// remove this from parent layouts
android:clickable="true"  

When a view parent is clickable *OR* (focusable *AND* focusableInTouchMode) its onTouch event intercepts that of its children which can cause unexpected behavior preventing the children from being clickable.

Also comment out the following line:

// comment this out
m_PopupWindow.Touchable=(true);
m_PopupWindow.Focusable = true;

Further more you can call immediate request focus on your EditText by adding <requestFocus /> tag in xml

<EditText...>
    <requestFocus />
</EditText>

Upvotes: 1

Related Questions