Issue
I have a toolbar with some actionbar buttons. I would like the camera icon button NOT to appear if the Build.VERSION.SDK_INT < 21.
in the following menu file I define the actionbar buttons:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="@+id/addons"
android:icon="@drawable/ic_add_box_black_24dp"
android:title="camera"
app:showAsAction="always"/>
<item
android:id="@+id/hellosearch"
android:icon="@drawable/ic_search_black_24dp"
android:title="camera"
app:showAsAction="always"/>
//inside the MainActivity I override the method:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.addons:
//we are requesting to purchase something:
Log.d("billing009","trying to start a purchase!! user has accesss(subs): "+user_has_autorenew_subs+
" didFind :: " + didFindTotalAccess);
showPurchaseDialog(styled_title);
//inHouseMethod_StartPurchase();
break;
case R.id.hellosearch:
mDrawer.openDrawer(GravityCompat.START);
return true;
HOWEVER, this method gets called when the user clicks on one of the actionbar buttons.
How can I use the toolbar object created inside onCreate: to get a hold of the Camera icon(ActionBar button), so I can set its visibility to GONE if the Build.VERSION.SDK_INT < 21 ?
//inside onCreate
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Solution
You can do that inside onCreateOptionsMenu like
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu_xml, menu);
if(Build.VERSION.SDK_INT < 21) {
MenuItem item = menu.findItem(R.id.idOfYourMenuItem);
item.setVisible(false);
}
return true;
}
Answered By - Android Killer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.