Search code examples
androidactionbarsherlockandroid-actionbar

Disable ActionBar Menu Item but still clickable


I would like to disable the ActionBar Menu Item but make it clickable. Is there any way I can do this?

Right now, it's just greyed out if I set setEnabled as false which is normal behavior but it's not clickable.


Solution

  • This doesn't really answer the question as the ActionBar Menu Item cannot be disabled and clickable at the same time. What I ended up doing was enabling the Menu Item and setting the text color to grey to make it look pseudo-disabled. And if the condition was fulfilled it would change to default color.

    Credit: Nicolai Buch Andersen

    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.menu, menu);
    
        SpannableStringBuilder text = new SpannableStringBuilder();
        text.append(getString(R.string.menu_title));
    
        if (someCondition){
             text.setSpan(new ForegroundColorSpan(Color.BLACK), 
                     0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        else {
            text.setSpan(new ForegroundColorSpan(Color.LTGRAY), 
                    0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    
    
        MenuItem item = menu.findItem(R.id.some_menu_id);
        item.setTitle(text);
    
        return true;
    }