0

What I'm trying to accomplish is when my EditText is empty, the MenuItem in Action Menu should be disabled. I'm inflating the Menu in onCreateOptionsMenu method, then I disable the Send button in onPrepareOptionsMenu method and save the reference to the Menu so I can use it in my TextWatcher which is set in onCreate method.

Menu myMenu;

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

    // Set up TextWatcher
    EditText ed = (EditText) findViewById(R.id.new_task_title);
    ed.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.d("MYLOG", "This is onTextChanged event");

            if (s == null || s.length() == 0) {
                myMenu.findItem(R.id.action_send).setEnabled(false);
            }
            else {
                myMenu.findItem(R.id.action_send).setEnabled(true);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            Log.d("MYLOG", "This is beforeTextChanged event");
        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.d("MYLOG", "This is afterTextChanged event");
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.new_task, menu);

    Log.d("MYLOG", "This is onCreateOptionsMenu event");
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    Log.d("MYLOG", "This is onPrepareOptionsMenu event");

    // Save reference to the menu
    myMenu = menu;

    // Disable Send Button
    myMenu.findItem(R.id.action_send).setEnabled(false);

    return true;
}

This code works fine and does exactly what it should do until the screen orientation is changed. I understand that when this happens, the activity is destroyed and created again. The problem is, that as the activity is recreated, the onTextChanged, beforeTextChanged and afterTextChanged methods are called as if user changed the text (which he didn't). Moreover, all of these three methods are called before the onCreateOptionsMenu and onPrepareOptionsMenu methods are called, which basically means that in onTextChanged I'm trying to access menu object that doesn't even exist at that moment. Which, of course, results in null pointer exception and crash of the application.

To illustrate this behavior, here's what the CatLog looks like when the screen orientation is changed.

11-06 11:55:39.142: D/MYLOG(32527): This is beforeTextChanged event
11-06 11:55:39.147: D/MYLOG(32527): This is onTextChanged event
11-06 11:55:39.147: D/MYLOG(32527): This is afterTextChanged event
11-06 11:55:39.207: D/MYLOG(32527): This is onCreateOptionsMenu event
11-06 11:55:39.207: D/MYLOG(32527): This is onPrepareOptionsMenu event
11-06 11:55:39.232: D/MYLOG(32527): This is onPrepareOptionsMenu event

So my question is, am I missing something here? Is there any other, better approach to what I'm trying to accomplish? Or is there any way how I can make Android not to call onTextChanged when screen orientation changes?

1 Answer 1

0

What is the behaviour of the application if you change the orientation with layout defined for landscape mode? Try creating a layout-land folder under /res folder and copy paste your layout file to that folder.

2
  • @Jakkub00: How about a boolean flag set to false in the beginning and set it to true only when you inflate the menu. In the listeners, check for the flag and then execute mymenu.finditem statement Nov 6, 2013 at 12:19
  • @Jakkub00: Were you able to find a solution to your problem? Please refer to the below link if not stackoverflow.com/questions/15992598/… Nov 12, 2013 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.