Search code examples
androidactionbarsherlock

seticon on sherlockactionbar while loading from another methode?


I have layout java content map,I've already added new items in the right side of the action bar,the item show loading while map loading when oncreate..

enter image description here

i want to change menuitem to icon when map finish loaded.. here my code to make items loading.

 public boolean onCreateOptionsMenu(Menu menu) {
        boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;
        menu.add("Icon").setActionView(R.layout.loading_lingkaran_kecil)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        return true;
    }



@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapv2_main_activity);
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                public void onMapLoaded() {
                    Toast.makeText(getApplicationContext(), "map has loaded", Toast.LENGTH_SHORT).show();
        //here to make items stop show loading and change icon,but nothing happend with this code
                    getSupportActionBar().getTabAt(0).setIcon(R.drawable.ico_member);


                }
             });
}

Thanks in advance


Solution

  • You need to have a boolean flag to check if it is done loading or not and call invalidateOptionsMenu to refresh your onCreateOptionsMenu while removing the old icon and replacing it with new Icon in the OptionMenu.

    sample:

    boolean doneLoading = false; //a global instance of the flag
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if(!doneLoading)
            menu.add("Icon").setActionView(R.layout.loading_lingkaran_kecil).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        else
        {
            menu.removeItem(R.layout.loading_lingkaran_kecil); //will remove the old icon after the loading is done
            menu.add("Icon").setActionView(R.layout.you_new_layout_after_loading).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        }
        return true;
    }
    

    And in your onMapLoadedCallback you need to refresh the menu items in the actionbar setting the flag to true.

    googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    
    googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                    public void onMapLoaded() {
                        Toast.makeText(getApplicationContext(), "map has loaded", Toast.LENGTH_SHORT).show();
    
                        doneLoading = true; //set to true means it is done loading
                        invalidateOptionsMenu(); //refresh the optionsmenu
    
                    }
                 });