Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

have menuitem only appear in action bar if ParseUser is viewing own post

Basically I want only a menuitem icon for Edit to show up in the action bar if the current ParseUser is viewing their own post.

I figure I could check if their viewing their own post simply by grabbing the current parse user like so (postedBy simply being a string from intent passed from listview activity):

ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser.username == postedBy)
{

}

Problem is now how would I make it so only the a icon for Edit in the ActionBar shows up if this is the case, if not then they don't show up. (so others can't edit) Possibly in onOptionsItemSelected? But that wouldn't make sense to me, what would make sense is having it where it's displayed which would be in XML?? Or possibly making two menu XMLS and calling one or the other depending if it's the current users post?

like image 485
user3628894 Avatar asked Nov 01 '22 23:11

user3628894


1 Answers

Well, you could have two menu xmls but that would be overkill (and harder to maintain). A much simpler solution would be to hide the menu option programmatically, i.e.

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.main, menu);

    // Show option depending on condition.
    MenuItem item = menu.getItem(R.id.menu_item_edit_post);
    item.setVisible(isUserPost());

    return true;
}
like image 161
matiash Avatar answered Nov 12 '22 13:11

matiash