0

Here is my login page layout, so when I rotate from portrait mode to landscape or contrary the AppCompatEditText gets focused

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:id="@+id/loginLayout"
    tools:context="com.jovan.matetracker.LoginActivity">

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/usernameLayout"
    android:layout_marginTop="170dp"
    android:layout_marginRight="32dp"
    android:layout_marginLeft="32dp"
    android:layout_marginBottom="8dp">

    <android.support.v7.widget.AppCompatEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/username"
        android:hint="@string/username"
        android:inputType="text"/>

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

I tried to set the clearFocus() attribute to the View inside the onResume() method but it doesn't work.

Update

public class LoginActivity extends AppCompatActivity {
    RelativeLayout loginLayout;
    TextInputLayout usernameLayout;
    AppCompatEditText username;


    public void goToRegister(View view) {
        Intent intent = new Intent(getApplicationContext(),RegisterActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        loginLayout = findViewById(R.id.loginLayout);
        usernameLayout = findViewById(R.id.usernameLayout);
        username = findViewById(R.id.username);

        username.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {

                if(username.getText().toString().isEmpty()) {
                    usernameLayout.setErrorEnabled(true);
                    usernameLayout.setError("Please enter your username!");
                    loginButton.setEnabled(false);
                } else {
                    usernameLayout.setErrorEnabled(false);
                    loginButton.setEnabled(true);
                }

            }
        });

        username.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(username.getText().toString().isEmpty()) {
                    usernameLayout.setErrorEnabled(true);
                    usernameLayout.setError("Please enter your username!");
                    loginButton.setEnabled(false);
                } else {
                    usernameLayout.setErrorEnabled(false);
                    loginButton.setEnabled(true);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

First what i did is commented the if statement in the onFocusChanged but i still had that error in my username Layout. Then I commented the if statemet inside the onTextChanged and after rotation I didn't had the error and the error message inside the TextInputLayout.

Fix

After a few hours of headache I found solution for my problem. this is the solution. I added an if statement in the onTextChanged() method

`if((savedInstanceState != null) && (confirmPassword.getText().toString().isEmpty())) {
                    confirmPasswordLayout.setErrorEnabled(false);
                    registerButton.setEnabled(true);
                }

` This is the full onTextChanged() method

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                if((savedInstanceState != null) && (confirmPassword.getText().toString().isEmpty())) {
                    confirmPasswordLayout.setErrorEnabled(false);
                    registerButton.setEnabled(true);
                } else if(confirmPassword.getText().toString().isEmpty()) {
                    confirmPasswordLayout.setErrorEnabled(true);
                    confirmPasswordLayout.setError("Please confirm your password!");
                    registerButton.setEnabled(false);
                } else {
                    confirmPasswordLayout.setErrorEnabled(false);
                    registerButton.setEnabled(true);
                }
            }
Jovan
  • 306
  • 6
  • 20

1 Answers1

0

If you do not want autofocus in your view, try adding this in your layout:

<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
    android:focusable="true" 
    android:focusableInTouchMode="true"
    android:layout_width="0px" 
    android:layout_height="0px"/>

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView 
    android:id="@+id/autotext"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:nextFocusUp="@id/autotext" 
    android:nextFocusLeft="@id/autotext"/>

source: Stop EditText from gaining focus at Activity startup

Federico Matera
  • 235
  • 2
  • 7
  • Thanks but it seems my problem is something else and not the focus of the view i'll update my question. – Jovan Jan 04 '18 at 17:03