0

enter image description hereI have this app wherein it outputs into the list view whatever I input on the status text field. However, I need to add a function that if the status text field is empty, the POST button should not work/be disabled. I actually read about the inclusion of a text watcher but I'm not sure how to integrate it.

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.FitWindowsFrameLayout;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;

    import java.util.ArrayList;
    import java.util.List;

    public class MainActivity extends AppCompatActivity {
        private ArrayList<FriendStatusMessage> mMessageList = new ArrayList<FriendStatusMessage>();
        ArrayAdapter<FriendStatusMessage> mAdapter = null;

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

            Button btnPost = (Button) findViewById(R.id.btn_post);
            btnPost.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            postMessage();
                            return;
                        }
                    }
            );


            mAdapter = new ArrayAdapter<FriendStatusMessage>(this, android.R.layout.simple_list_item_1, mMessageList);
            ListView lstMessages = (ListView) findViewById(R.id.lst_messages);
            lstMessages.setAdapter(mAdapter);
        }

        private void postMessage() {
            EditText edtUsername = (EditText) findViewById(R.id.edt_username);
            EditText edtStatusMessage = (EditText) findViewById(R.id.edt_status_msg);

            String userStr = edtUsername.getText().toString();
            String messageStr = edtStatusMessage.getText().toString();

            mMessageList.add(new FriendStatusMessage(userStr, messageStr));
            mAdapter.notifyDataSetChanged();
            return;

        }

        private class FriendStatusMessage {
            private String mUsername = "";
            private String mMessage = "";

            public FriendStatusMessage(String username, String statusMessage) {
                mUsername = username;
                mMessage = statusMessage;
            }

            public String getmUsername()
            {
                return mUsername;
            }

            public String getmMessage ()
            {
                return  mMessage;
            }

            public String toString() {
                return getmUsername() + ": " + getmMessage() + "\n";
            }
        }
    }
lexus
  • 101
  • 12

3 Answers3

0

How about just disallowing the action?

    private void postMessage() {
        EditText edtUsername = (EditText) findViewById(R.id.edt_username);
        EditText edtStatusMessage = (EditText) findViewById(R.id.edt_status_msg);

        String userStr = edtUsername.getText().toString();
        String messageStr = edtStatusMessage.getText().toString();

        boolean doAction = false;
        if (userStr.isEmpty()) {
            edtUsername.setError("Must enter username");
        }
        if (messageStr.isEmpty()) {
            edtStatusMessage.setError("Must enter status");
        }
        doAction = true;

        if (doAction) {
            mMessageList.add(new FriendStatusMessage(userStr, messageStr));
            mAdapter.notifyDataSetChanged();
        }
    }

Otherwise, you do need to move these to onCreate

EditText edtUsername = (EditText) findViewById(R.id.edt_username);
EditText edtStatusMessage = (EditText) findViewById(R.id.edt_status_msg);

And correctly addTextChangedListener onto those.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Try this, it would a better option

       void postMessage() {
                EditText edtUsername = (EditText) findViewById(R.id.edt_username);
                edtUsername.setError(null);
                EditText edtStatusMessage = (EditText) findViewById(R.id.edt_status_msg);
                edtStatusMessage.setError(null);

                String userStr = edtUsername.getText().toString();
                String messageStr = edtStatusMessage.getText().toString();

                if (TextUtils.isEmpty(userStr)) {
                        edtUsername.setError("This shouldn't be blank");
                        edtUsername.requestFocus();
                }else (TextUtils.isEmpty(messageStr)) {
                        edtStatusMessage.setError("This shouldn't be blank");
                        edtStatusMessage.requestFocus();
                }else{
                        mMessageList.add(new FriendStatusMessage(userStr, messageStr));
                        mAdapter.notifyDataSetChanged();
                }
       }
Ranjan
  • 390
  • 2
  • 10
0

You need to input text validation & addTextChangedListener for post button action. After empty text validation you can enable post button to execute post action. Also you can use TextWatcher on edtStatusMessage to execute post action.

    private  EditText edtUsername, edtStatusMessage ;
    private  Button btnPost;

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

            edtUsername = (EditText) findViewById(R.id.edt_username);

            edtStatusMessage = (EditText) findViewById(R.id.edt_status_msg);

            btnPost = (Button) findViewById(R.id.btn_post);

           btnPost.setEnabled(false); 
           edtStatusMessage.addTextChangedListener(listenerTextChangedStatus );

        }    


    private TextWatcher listenerTextChangedStatus = new TextWatcher() {
            public void afterTextChanged(Editable editable) {      


          }

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


      }

      @Override
      public void afterTextChanged(Editable s) {

         if(!edtUsername.getText().toString().isEmpty() && !s.getText().toString().isEmpty()){
            btnPost.setEnabled(true); 
           // do action
                btnPost.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            postMessage();
                            return;
                        }
                    }
              );

        }else{
             btnPost.setEnabled(false); 
             edtStatusMessage.setFocus(true);
          }

      }

};

for more information follow the link
http://stackoverflow.com/questions/8543449/how-to-use-the-textwatcher-class-in-android
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
  • Hi, thanks fir the response. I tried this one but I seem to be getting errors most of which are of illegal starts of expression and cannot resolve. Also, for the TextWatcher, it requires me to declare it as abstract or implement the methods. – lexus Mar 05 '17 at 04:07
  • I have updated my post. Added to unimplemented override method for Textwatcher – Mohammod Hossain Mar 05 '17 at 04:13