Search code examples
javaandroidandroid-studioandroid-edittextandroid-alertdialog

Empty EditText value displayed in AlertDialog


I am new in Android development and I got a problem. I have 2 EditText fields of login and password and 1 Button. When I click on the Button, I am getting AlertDialog with message "Logged in as " but I need it to be "Logged in as *value entered into "login" EditText". The EditText value is always empty. It's just a simple implementation, but it doesn't work. What am I doing wrong here?

Here is the code:

public class FirstFragment extends Fragment {
    EditText loginText;
    EditText passwText;
    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        View view2 = inflater.inflate(R.layout.fragment_first, null, false);

        loginText = (EditText)view2.findViewById(R.id.loginText);
        passwText = (EditText)view2.findViewById(R.id.passText);
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false);
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick( View view) {
                handleLoginDialog();
            }
        });
    }

    private void handleLoginDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        //View view2 = getLayoutInflater().inflate(R.layout.fragment_first, null, false);

        String st = loginText.getText().toString();

        if (loginText != null && passwText != null){
            builder.setMessage("Logged in as " + st);
        }
        else
            builder.setMessage("Login or password was not entered!" );
        builder.show();
    }
}

Solution

  • I want to start the answer with what you did:

    You inflated 2 same layouts as :

    1. View view2 = inflater.inflate(R.layout.fragment_first, null, false);
    2. return inflater.inflate(R.layout.fragment_first, container, false);

    Let's call this second inflation as view1 which is the original view that was inflated for the Fragment and also is the one that is returned in the onViewCreated() method.

    Then, you set the set the onClickListener for button_first of view1.

    Then, inside the handleDialogLogin() method, you try to get the text value of EditText fields that you created with view2 object.
    That's where it went wrong. Because, during app usage, you insert the text in EditText fields of view1 but you are asking for text value of EditText fields of view2 which will obviously be empty.

    The correct and clean approach is here :

    public class FirstFragment extends Fragment {
        EditText loginText;
        EditText passwText;
        @Override
        public View onCreateView(
                LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState
        ) {
            // just inflate the layout for this fragment as it is not correct
            // place for declaring and initializing views
            return inflater.inflate(R.layout.fragment_first, container, false);
        }
    
        // this is the best place for declaring and initializing views
        public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
    
            // declare and initialize your EditText here 
            loginText = (EditText)view.findViewById(R.id.loginText);
            passwText = (EditText)view.findViewById(R.id.passText);
            view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick( View view) {
                    handleLoginDialog();
                }
            });
        }
    
        private void handleLoginDialog() {
            AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
    
            String st = loginText.getText().toString().trim();
            String pt = passText.getText().toString().trim();
    
            if (st.isEmpty() && pt.isEmpty())
               builder.setMessage("Login or password was not entered!" );
            else
               builder.setMessage("Logged in as " + st);
            builder.show();
        }
    }