Android:Get View Reference to a Menu Item

Get the view from the action bar menu item

You can get the view using the menu item id, by getting it in onOptionsItemSelected ..

findViewById(R.id.menu_item);

Android : Get view Reference to a Menu Item

You can achieve this by providing your menu item with an actionViewClass property in xml and then you will be able to get the pivot view u wanted. The code would be something like this

<item
android:id="@+id/menu_find"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.ImageButton"
/>

In your OnCreateOptionsMenu do this

public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_search, menu);
locButton = (ImageButton) menu.findItem(R.id.menu_find).getActionView();
locButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
createPopup();
mQuickAction.show(v);
}
});
return true;
}

Android get a View reference to action bar item?

You can do menu.findItem(R.id.youritemid) to get the menu item, and you can get reference to menu object in your onCreateOptionsMenu(Menu menu) method then you can initialize a global variable with that menu object to use it anywhere.

Here is some code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
_menu = menu;
return true;
}

I am initialize my global variable Menu _menu; with menu object and then i can do like this MenuItem mi = _menu.findItem(R.id.itemid); any where i want.

Edit:
Please take care that you are not calling anything on menuitem before the menu gets created, you can schedule a thread to wait for 3 to 5 secs or you can do it some other way, all you should worry about is whether the menu has been initialized or not.

How to get view reference from menu item?

Ended up:

@Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
getActivity().getMenuInflater().inflate(R.menu.boat_accept_menu, menu);

MenuItem menuItem = menu.getItem(0);

TextView doneTv = new TextView (getActivity());
doneTv.setTextView("Ok")

boatClassSelectedBtn = menuItem.getActionView();

boatClassSelectedBtn.setOnClickListener(this);

}

Get MenuItem's View reference for TapTargetView

Use TapTarget.forToolbarMenuItem insted of TapTarget.forView

Change the code like this...

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

new TapTargetSequence(this)
.targets(
TapTarget.forToolbarMenuItem(toolbar,R.id.add, "Gonna"))

.listener(new TapTargetSequence.Listener() {
// This listener will tell us when interesting(tm) events happen in regards
// to the sequence
@Override
public void onSequenceFinish() {
// Yay
}

@Override
public void onSequenceStep(TapTarget lastTarget, boolean targetClicked) {

}

@Override
public void onSequenceCanceled(TapTarget lastTarget) {
// Boo
}
});

return true;
}

How to get the reference of the menu item in onCreate() method

You'd typically set initial value when menu is inflated in onCreateOptionsMenu (assuming below your menu is R.manu.main_menu)

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

MenuItem wifiMenuItem = menu.findItem(R.id.wifi_status);

// set wifiMenuItem state here based on wifiManager.isWifiEnabled()

return true;
}

can't get a reference to menu item

You need to add setNavigationItemSelectedListener on your navigation view and get the callbacks on click on there.
Check out answer on this StackOverflow question

Get view from MenuItem which property showAsAction never

I found the solution on this reference . JarredRummler answer get the OverflowbMenuButton as a ImageView. Use that method and cast the OverflowMenuButton as View and use it.

Referencing a menu itemId in android studio

Declare in your activity:

public class MainActivity extends AppCompatActivity {
MenuItem mi;

The add method returns the menuitem:

mi = menu.add(0,0,0, "Slet denne regning");

and in onContextItemSelected:

if (item == mi) {
//your code here
}


Related Topics



Leave a reply



Submit