Search code examples
androidactionbarsherlockandroid-actionbarandroid-spinner

Close ActionBar Spinner programmatically or avoid opening it


I'm trying to use the spinner in the ActionBar to provide account context in my app. There is also a "Add account" element at last position in the list. I'm using ABS and configure spinner appearance with .setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

So far so good.

The first time the user launch the app, this spinner will be empty, the only choice will be "Add account", it's also this text that is visible in the spinner current selection. What I'm trying to achieve is to redirect the user to the new account activity when the user clicks on the spinner (not in the opened list, but on the spinner itself). In my adapter, I could detect it and open the screen like that:

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        ...
    }

    if (parent.getClass() == Spinner.class && ((Spinner) parent).getCount() == 1) { //"Hack" for initial click on "Add Account" in the spinner when it's empty
        Intent intent = new Intent(context, AccountPreferenceActivity.class);
        ((HomeActivity) context).startActivityForResult(intent, HomeActivity.ACTIVITY_PREFERENCE_ACCOUNT);
    }

    ...

    return convertView;
}

My problem is that after filling the new form and getting back to the home screen, the spinner will be open and it's kind of ugly user experience.

My first attempt was to avoid opening the spinner in the getDropDownView method, but I failed to achieve it. I've then tried all kind of dirty workaround (make spinner disappear/reappear, focus on another field in the screen, simulate back button...), but nothing seems to work.

Do you have any idea how to achieve that ?

Thanks


Solution

  • Why you just don't use actionbar.setNavigationCallBacks function?! here is an example that I'm using, maybe it helps:

    Context context = getSupportActionBar().getThemedContext();
    ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.navigations,
            R.layout.sherlock_spinner_item);
    list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
    
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    
    mActionBar.setListNavigationCallbacks(list, new OnNavigationListener() {
    
        @Override
        public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    
        if (itemPosition == 0) {
    
    
        } else if (itemPosition == 1) {
            Intent intent = new Intent(MainActivity.this, SomeActivity.class);
            startActivity(intent);
    
        }
    
        return true;
        }
    });
    

    then in your string.xml define elements array that you wants to display in navigation list

    <string-array name="navigations">
            <item>create account</item>
        </string-array>
    

    EDITED:

    Ok I find your answer, To achieve that, you have to just return new View() as a result of getDropDownView(...)

        @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
    
    if (! someSharedPreferencedObject.getBoolean("isAlreadyCreated", false)) {
        if (parent.getClass() == Spinner.class && ((Spinner) parent).getCount() == 1) {
    
        Intent intent = new Intent(mContext, UserSettingActivity.class);
        mContext.startActivity(intent);
        return new View(mContext);
        }
    } else {
        return super.getDropDownView(position, convertView, parent);
    }
    }