Get the ActionBar to redraw itself? - android

I'd like to replace the icon used for an ActionBar item at runtime (it appears as an action item on the ActionBar). I'm trying the following:
private Map<Integer, Integer> mPendingReplacements =
new HashMap<Integer, Integer>();
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
for (Map.Entry<Integer, Integer> it : mPendingReplacements.entrySet()) {
MenuItem item = menu.findItem(it.getKey().intValue());
if (item != null) {
item.setIcon(getResources().getDrawable(it.getValue().intValue()));
}
}
mPendingReplacements.clear();
return true;
}
private void replaceMenuItemIcon(int menuItemId, int drawable) {
mPendingReplacements.put(menuItemId, drawable);
invalidateOptionsMenu();
}
private void onUserClickedSomeButton() {
replaceMenuItemIcon(R.id.foo, R.drawable.grok);
}
------------ Update -----------
Updated the above sample to use mPendingReplacements as suggested by Selvin. I can see it execute as excepted, but the actionbar icon still doesn't change immediately. Will continue digging into this, but can anyone spot if I'm missing something?
Thank you

Related

How to set the menu bar icon initially according to the server response

I am having list of Colleges to display and click the perticular college it goes to the detail page. In there there is a menu bar with notification and Favourite_icon.Here i am having the Favourite_icon and Favourite_icon1. If users clcik the favourite_icon it stored as favourited in server and the icon changed as Favourite_icon1. After do some process i have visted the Favourited college. That time it should show the Favourite_icon1 in menu bar. I have tried the following method but nothing happend . I have added the the code which i have tried
menu_clg.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appmunu="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".UserDashBoardFragment">
<item
android:id="#+id/action_notify"
android:icon="#drawable/mail_icon"
appmunu:showAsAction="always"
android:title="Notification" />
<item
android:id="#+id/action_favourite"
android:icon="#drawable/icon_selector"
appmunu:showAsAction="always"
android:title="Favourite" />
</menu>
this Activity code
private boolean canAddItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_clg, menu);
mMenu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notify:
navigatetoNotification();
return true;
case R.id.action_favourite:
if (item.getItemId() == R.id.action_favourite) {
invalidateOptionsMenu();
favouriteClg();
}
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (canAddItem) {
menu.getItem(1).setIcon(R.drawable.vijay);
canAddItem = false;
favouriteClg();
} else {
menu.getItem(1).setIcon(R.drawable.favourite_icon);
canAddItem = true;
favouriteClg();
}
return super.onPrepareOptionsMenu(menu);
}
this is the code for check wheather the college is already favourited or not in onCreate() method
public void chechFavourite() {
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... arg0) {
//method name changed here
//getAllEventFromUser method used for getting all previously send events of current user
return favouriteDelegates.getAllCollegeDetails(userMO, context);
}
#Override
protected void onPostExecute(String collegelists) {
if (collegelists != "null") {
initCollegeMO = gson.fromJson(collegelists, new TypeToken<InitCollegeMO>() {
}.getType());
collegeMOs = initCollegeMO.getCollegeMOs();
for (CollegeMO collegeMO1 : collegeMOs) {
//here the list of college has eceived from server so i checked all the college id with current college id
collegeId = collegeMO1.getCollegeId(); //here collegeMO isthe object which is accessed by parcelable from another activity
if (collegeMO.getCollegeId() == collegeId) {
canAddItem = true;
} else {
canAddItem = false;
}
}
} else {
canAddItem = false;
}
}
}.execute(null, null, null);
}
}
Hope below code would be helpful for you.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.actionbar, menu );
userItem = menu.findItem(R.id.userItem);
return menu;
}
Edit 1:
Method which will update menu programatically.
private void updateMenuTitles() {
if(isFavorite){
userItem.setIcon(new BitmapDrawable(getResources(), favoriteBitmap));
}else{
userItem.setIcon(new BitmapDrawable(getResources(), unFavoriteBitmap));
}
invalidateOptionsMenu();
}
Hope this would help you.
You can change the title of your MenuItem runtime and manage the click event according to their title.
See this code, hope it will help you.
You can call updateMenuTitles() after getting response from API and change the title.
private Menu menu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Create your menu...
this.menu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_favourite:
if (item.getTitle().equals("favourite")) {
//Alerady Favourite, make it unfavourite
MenuItem.setTitle("un-favourite");
// do what you want
}
else if(item.getTitle().equals("un-favourite")){
// un-favourite, make it favourite
MenuItem.setTitle("favourite");
// do what you want
}
return true;
}
return super.onOptionsItemSelected(item);
}
/*
Update title of your menuItem whenever you want
*/
private void updateMenuTitles() {
MenuItem MenuItem = menu.findItem(R.id.action_favourite);
if (MenuItem.getTitle().equals("favourite")) {
//Alerady Favourite, make it unfavourite
MenuItem.setTitle("un-favourite");
}
else if(MenuItem.getTitle().equals("un-favourite")){
// un-favourite, make it favourite
MenuItem.setTitle("favourite");
}
}

Getting error: Unable to Instantiate Activity

I have been following this tutorial: http://developer.android.com/guide/topics/ui/menus.html#CAB. However, when I run the application, I get an error:
java.lang.RuntimeException:
Unable to instantiate activity
ComponentInfo{com.example.ayushi.chaseyourdream/com.example.ayushi.chaseyourdream.MainActivity}:
java.lang.InstantiationException: can't instantiate class
com.example.ayushi.chaseyourdream.MainActivity
Here's my MainActivity. My application was working completely fine before I added code between label 1 and `label 2'.
public abstract class MainActivity extends ActionBarActivity implements AbsListView.MultiChoiceModeListener{
DbHelper db;
ListView myList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DbHelper(this);
myList = (ListView) findViewById(R.id.newList);
loadData();
// LABEL 1
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
myList.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
// deleteSelectedItems();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_main, menu);
return true;
}
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}
});
// LABEL 2
}
public void onResume()
{
super.onResume();
loadData();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void loadData()
{
Cursor cursor = null;
try {
cursor = db.fetchData();
}
catch(NullPointerException e)
{
e.printStackTrace();
}
ListAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.tasks,
cursor,
new String[]{db._ID, db.COLUMN_1, db.COLUMN_2},
new int[]{R.id.idnum, R.id.c1, R.id.c2}, 0);
myList.setAdapter(myAdapter);
}
public void addNew(View v)
{
Intent intent = new Intent(this,AddActivity.class);
startActivity(intent);
}
}
EDIT 1: this line
public class MainActivity extends ActionBarActivity implements AbsListView.MultiChoiceModeListener{
showed me an error in Android Studio, and when I clicked Alt + Enter, it showed me two options: Make the class abstract, or implement its methods.
The first option gave me the initial problem, so now I went for option 2. Here are the additional methods implemented :
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
Now, my doubt is: these are exactly the same methods in between LABEL 1 and LABEL 2, and erasing any one set will lead to errors. What should I do?
You are getting this error: Unable to instantiate activity because you made your activity abstract. Remove that and implement the methods of AbsListView.MultiChoiceModeListenerinterface.
When you're implementing an Interface so you have to implement all the methods you have listed. If you don't know what to do with method you don't use just leave them as they are.
Now, my doubt is: these are exactly the same methods in between LABEL 1 and LABEL 2, and erasing any one set will lead to errors. What should I do?
You shouldn't have two of the same. Try using #Override annotation on the methods you implemented.

Check ActionBar's overflow popup is showing

How to check that the action overflow PopupMenu is now showing?
It is need for me when I trying to auto-hide ActionBar, and don't need to hide when ActionBar's overflow popup is showing.
You can override Activity.onMenuOpened and Activity.onPanelClosed to determine when the overflow menu or other sub-menus are showing, such as the ShareActionProvider. Here's an example:
/** True if an options menu has been opened, false otherwise */
private boolean mMenuOpened;
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
mMenuOpened = true;
return super.onMenuOpened(featureId, menu);
}
#Override
public void onPanelClosed(int featureId, Menu menu) {
super.onPanelClosed(featureId, menu);
mMenuOpened = false;
}
Alternatively
ActionBarView subclasses AbsActionBarView which contains AbsActionBarView.isOverflowMenuShowing. Since these class are internal and hidden, you'll need to access it via reflection.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final int actionBarViewId = getResources().getIdentifier("action_bar", "id", "android");
final View actionBarView = findViewById(actionBarViewId);
actionBarView.postDelayed(new Runnable() {
#Override
public void run() {
System.out.println(isOverflowMenuShowing(actionBarView));
}
}, 2500);
}
private static boolean isOverflowMenuShowing(View actionBarView) {
try {
final Class<?> abv = Class.forName("com.android.internal.widget.ActionBarView");
final Method isOverflowShowing = abv.getMethod("isOverflowMenuShowing", new Class[] {});
return (boolean) isOverflowShowing.invoke(actionBarView, new Object[] {});
} catch (final Exception ignored) {
// Nothing to do
}
return false;
}

How do I delete a default item in the contextual action bar (CAB)?

There are some questions like this already made in Stack Overflow but none of the selected solutions has worked for me. These links appear to make different choices, but I tried and I could not remove the first option which is by default.
Link 1 Link 2 Link 3 Link 4
Ignore the item icons.
I need to delete the item in the circle.
private class ModoAccion implements ActionMode.Callback {
private ArrayList<Integer> listaOpciones = new ArrayList<Integer>();
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
MenuInflater inflater = actionMode.getMenuInflater();
menu.clear();
inflater.inflate(R.menu.admin_detalle_emp_action_mode, menu);
actionMode.invalidate();
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
listaOpciones.add(R.id.accion_cancelar);
listaOpciones.add(R.id.accion_guardar);
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (!listaOpciones.contains(item.getItemId()))
item.setVisible(false);
}
return false;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.accion_cancelar:
actionMode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode actionMode) {
mActionMode = null;
}
}
You can't actually get rid of that area of the context action bar. However, you can change its drawable (even to nothing if you like) by styling your app. Just override item name android:actionModeCloseDrawable and insert your own drawable.

How to use contextual action mode with SherlockListFragment

I want use a Contextual Action Bar (CAB) in my app but is not compatible with old versions of Android so I'm using this tutorial: http://www.miximum.fr/tutos/849-porting-the-contextual-anction-mode-for-pre-honeycomb-android-apps
My code is:
public class SongsFragment extends SherlockListFragment implements
LoaderManager.LoaderCallbacks<Cursor>, OnLongClickListener{
...
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
activity = this.getActivity();
...
mMode = null;
mListView = getListView();
mListView.setItemsCanFocus(false);
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mListView.setOnLongClickListener(this);
}
#Override
public boolean onLongClick(View v) {
SparseBooleanArray checked = mListView.getCheckedItemPositions();
boolean hasCheckedElement = false;
for (int i = 0; i < checked.size() && !hasCheckedElement; i++) {
hasCheckedElement = checked.valueAt(i);
}
if (hasCheckedElement) {
if (mMode == null) {
mMode = activity.startActionMode(mActionModeCallback);
}
} else {
if (mMode != null) {
mMode.finish();
}
}
return false;
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Create the menu from the xml file
activity.getSupportMenuInflater().inflate(R.menu.cab_songs, menu);
return true;
}
...
I have errors in:
-"activity.startActionMode(mActionModeCallback);": The method startActionMode(ActionMode.Callback) in the type Activity is not
applicable for the arguments (ActionMode.Callback)
-activity.getSupportMenuInflater().inflate(R.menu.cab_songs, menu);": The method getSupportMenuInflater() is undefined for the
type FragmentActivity
Any idea? is there another solution for CAB using sherlock?
Change your imports for ActionMode and MenuInflater to their ActionBarSherlock equivalents (com.actionbarsherlock.view.ActionMode and com.actionbarsherlock.view.MenuInflater).
I solved using:
getSherlockActivity().startActionMode(mActionModeCallback);
...
mode.getMenuInflater().inflate(R.menu.cab_songs, menu);
Now, I want do an action when the user does a "click" and another action when the user does a "long click". I've "onItemLongClick" and "onListItemClick" but sometimes longClick is not called and when it is called if I release the "onListItemClick" is called. How can I do this actions?

Categories

Resources