Clearing text of autoCompleteTextView(Menu Item) on toolbar in Android

  • Replies:1
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Mar 24, 2016 3:36:38 PM via Website

MainActivity.java

{
private AutocompleteTextView actv;
A)

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item=menu.findItem(R.id.autoCompleteTextView);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        actv.setText("");

//getting Null Pointer Exception here

        return false;
    }
});

}
B)

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id){

        case R.id.autocompletetextview:
        break;
        case R.id.reset_button:
        break;
        default:
    }
    return super.onOptionsItemSelected(item);
}

}
1st Question: Am I doing it right to clear text inside autoCompleteTextView by pressing button? If not, rectify it

Another follow-up question,we can also add a listener in onOptionsItemSelected?

So what is OnCreateOptionsMenu vs onOptionsItemSelected?

What should I put inside exactly ?

  • Forum posts: 269

Mar 24, 2016 4:58:22 PM via Website

Hi, in onCreateOptionsMenu you need to replace actv.setText("");with item.setText("");. String private AutocompleteTextView actv; is redundant.
There is no onOptionsItemSelected() for the View class but only for Activity.

— modified on Mar 24, 2016 5:02:02 PM