0

I have the following code:

public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.layout.menu, menu);
}

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.settings:
            Intent i=new Intent(class1.this, clas2.class);
            startActivity(i);
            return true;
    }
    return false;
}

My issues is following:
I have an search icon in the ActionBar. When I tap on the search icon an edittext is opened and cancel button shown. The search is working properly. Now, when I click the cancel button I want to hide the edittext and cancel button. How can I achieve this?

Tomik
  • 23,857
  • 8
  • 121
  • 100
ADT
  • 255
  • 1
  • 4
  • 14
  • Check out similar post http://stackoverflow.com/questions/10692755/how-do-i-hide-a-menu-item-in-the-actionbar – GrIsHu Oct 14 '13 at 11:09

2 Answers2

1

I solve my problem... using

item.collapseActionView();

Thanks for reply and hope it this will help others..

ADT
  • 255
  • 1
  • 4
  • 14
0

Here is my code where i have used it.....

private EditText search;
private Button search_cancle;
@Override
public boolean onOptionsItemSelected(final MenuItem item) {

    int id = item.getItemId();

    if(id==R.id.action_search){
        View v = (View) item.getActionView();
        search = ( EditText ) v.findViewById(R.id.txt_search);
        search.requestFocus();
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

        search_cancle=(Button)v.findViewById(R.id.search_cancel);
        search_cancle.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                   item.collapseActionView(); // Here I have set it.....
                search.setText("");
            }
        });
        return true;
    }
    else 
    {
        return super.onOptionsItemSelected(item);
    }
}
ADT
  • 255
  • 1
  • 4
  • 14