ActionBarSherlock disappears when focus changes from EditText to Button

I have a simple login layout containing two EditText fields and a button to login. The problem is the ActionBar disappears when the soft keyboard is open and I change focus from the EditText to the button and the ActionBar comes back again when I click. The problem does not occur when the soft keyboard is closed and I navigate through EditTexts and Button using DPAD.

I am using ActionBarSherlock and the problem only occurs on Android 2.x emulators, on 4.x emulators everything is fine. I know ActionBarSherlock uses native ActionBar versions in Android versions where it is available, so this is probably a problem with the ActionBarSherlock code.

I also ran a test to check the value ActionBar.isShowing()

, but this returned even when the ActionBar was not visible on the screen.

I can't figure out what's going on in this case, does anyone have any ideas?

XML layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="UselessParent" >

    <LinearLayout
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/username"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:hint="@string/username"
            android:inputType="textNoSuggestions"
            android:nextFocusUp="@+id/loginButton"
            android:imeOptions="actionNext" />

        <EditText
            android:id="@+id/password"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:hint="@string/password"
            android:inputType="textPassword"
            android:imeOptions="actionDone" />

        <Button
            android:id="@+id/loginButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="@string/login"
            android:textSize="20sp"
            android:nextFocusDown="@+id/username" />

    </LinearLayout>

</RelativeLayout>

      

FRAGMENT CODE

public class LoginFragment extends BaseFragment {

    @InjectView(R.id.loginButton) protected Button mLoginButton;
    @InjectView(R.id.username) protected EditText mUsernameEditText;
    @InjectView(R.id.password) protected EditText mPasswordEditText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.login, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();

        mLoginButton.setEnabled(allFieldsValid());
        mLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleLogin();
            }
        });

        mPasswordEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE && allFieldsValid()) {
                    handleLogin();
                }
                return false;
            }
        });

        TextWatcher fieldValidatorTextWatcher = new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mLoginButton.setEnabled(allFieldsValid());
            }
        };

        mUsernameEditText.addTextChangedListener(fieldValidatorTextWatcher);
        mPasswordEditText.addTextChangedListener(fieldValidatorTextWatcher);
    }

    private void handleLogin() {
        getSherlockActivity().setSupportProgressBarIndeterminateVisibility(true);

        Intent intent = new Intent(getActivity(), LoginService.class);
        intent.putExtra(BaseIntentService.EXTRA_STATUS_RECEIVER, mResultReceiver);
        intent.putExtra(LoginService.PARAM_USERNAME, mUsernameEditText.getText().toString());
        intent.putExtra(LoginService.PARAM_PASSWORD, mPasswordEditText.getText().toString());
        getActivity().startService(intent);
    }

    private boolean allFieldsValid() {
        return usernameFieldIsValid() && passwordFieldIsValid();
    }

    private boolean usernameFieldIsValid() {
        return !TextUtils.isEmpty(mUsernameEditText.getText());
    }

    private boolean passwordFieldIsValid() {
        return !TextUtils.isEmpty(mPasswordEditText.getText());
    }

    @Override
    public void onReceiveResult(int resultCode, Bundle resultData) {
        getSherlockActivity().setSupportProgressBarIndeterminateVisibility(false);
        super.onReceiveResult(resultCode, resultData);
    }

    @Override
    public void onReceiveResultSuccess(Bundle resultData) {
        ((LoginActivity) getActivity()).redirectToSelectTeamwebActivity(resultData.getInt(LoginService.RESULT_USER_ID));
    }

    @Override
    public void onReceiveResultFailure(Bundle resultData) {
        mPasswordEditText.setText("");
        String errorMessage = getString(R.string.invalid_login_credentials);
        Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
    }

}

      

+2


source to share


1 answer


ActionBarSherlock attaches the compatibility action bar in pre-ICS inside the content view, not in the Window decor view. Because of this, it is susceptible to more inconvenience, which can cause unexpected behavior like the one you see.



If you have set windowSoftInputMode

to pan content when the IME is open, the action bar will disappear from the screen. The easy way would be to use another mode (like resizing) that will reposition the content view and keep the action bar on the screen.

+4


source







All Articles