ADT
ADT

Reputation: 255

android actionbar sherlock collospe and expand

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?

Upvotes: 0

Views: 126

Answers (2)

ADT
ADT

Reputation: 255

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);
    }
}

Upvotes: 0

ADT
ADT

Reputation: 255

I solve my problem... using

item.collapseActionView();

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

Upvotes: 1

Related Questions