4

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);
                };
veerlas
  • 41
  • 1
  • 7

2 Answers2

1

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>
Giddy Naya
  • 4,237
  • 2
  • 17
  • 30
  • Hi @GiddyNaya thanks for your response, i tried with the suggestions mentioned above, with setting up the tag the edit text getting the focus but the text is not getting entered into text box and also if we set the tag for both the Edittext in my layout then both are getting the focus at a time. – veerlas Aug 19 '19 at 12:37
  • You can only use the `` tag for just one view in your layout – Giddy Naya Aug 19 '19 at 13:17
  • Also remove the line `android:textIsSelectable="true"` from your `TextInputEditText`. It is selectable by default – Giddy Naya Aug 19 '19 at 13:23
  • i tried all the mentioned scenarios, but i think there is some other issue for this as even the edit text got focus not taking the text we are entering. – veerlas Aug 20 '19 at 06:45
  • thanks for your suggestions guys but unfortunately nothing worked fine, but i just removed the Linear layout and placed entire controls in Table layout and it worked , don't know exact issue but this one resolved the issue. – veerlas Aug 22 '19 at 06:17
0

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.

Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19