Toolbar menu item programmatic click - android

I have this code
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
openSearch();
return true;
}
});
into onCreate( ) of my Activity.
OpenSearch function call opens up google now like search view. This only happens when the user clicks on the search action item in the toolbar. In my case I want the search view to open up automatically when the activity starts. How can this menu item click be done programmatically.
I can't call openSearch directly because it needs the menu to be created.
Is there there any callbacks informing that action menus have been created ?

You can try something like this :
tollbarLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
tollbarLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
tollbar.performclick();
}
}
});

Related

Click on Menu Item on toolbar don't work with Navigationview

I have a problem with an "onClick" event, with NavigationView and Menu item.
My explanation : I have an activity, which is a child of my "root" activity.
In this, I have a menu item on the top_right in my toolbar, which can allow me to take a picture.
Also, I use a NavigationView, so I override it to return at my "root" activity when I click on it (With setDrawerIndicatorEnabled(false), it shows me a "back" button).
Problem: When I click on my menu item which is the "camera" item, it has the same behaviour when I click on my NavigationView (Which is a "back" button), it returns on my "root" activity.
In my opinion, the problem comes to the "setDrawerIndicatorEnabled" function, but I didn't find any correct answer.
Any idea?
Thanks for our help !
Code for my menu item :
#Override
public void onCreateOptionsMenu(#NonNull Menu menu, #NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_mission_detail, menu);
}
#Override
protected void onChangeStarted(#NonNull ControllerChangeHandler changeHandler, #NonNull ControllerChangeType changeType) {
setOptionsMenuHidden(!changeType.isEnter);
if (changeType.isEnter) {
setTitle();
}
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (item.getItemId() == R.id.add_photo) {
onCameraClicked();
return true;
}
return super.onOptionsItemSelected(item);
}

LayoutInflater Onclick Not working

I have 2 xml one main xml and one menu xml. I want to get the click event from menu xml to main xml activity. is there is any way to solve this problem.
Main Home Page
Menu Page
Now click on the menu page button event i want in my main home activity page.
I done Like this
View otherLayout = LayoutInflater.from(this).inflate(R.layout.menu_layout,null);
Button tstclick = (Button) otherLayout.findViewById(R.id.textclick);
tstclick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Your thing
System.exit(0);
}
});
The Activity class provides us two methods to inflate/show menu items and work with them or assign some tasks to them. They are:
onCreateOptionsMenu(Menu menu)
onOptionsItemSelected(MenuItem item)
The onCreateOptionsMenu() method is responsible for creating and inflating the menu by help of MenuInflater class.
The onOptionsItemSelected() method is responsible for assigning tasks to each menu item. Each menu item is identified by help of its unique ID.
In order to show and work with menu's in any Activity, both the methods needs to be overridden as shown below:
public class MainActivity extends AppCompatActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
// All your menu items will come here. Each menu item ID will become a separate case in the Switch case
default:
return super.onOptionsItemSelected(menuItem);
}
}
}
As soon as the user clicks or taps on any menu item, the onOptionsItemSelected() method is called by the Android OS and then the ID for that particular menu item will be matched in the method. The group of statements specified in the respective case will then be executed.
For more info visit the following links:
http://developer.android.com/guide/topics/ui/menus.html
http://developer.android.com/reference/android/view/MenuInflater.html

Actionbar's overflow menu open/close listener

I want to listen when user opens/closes the overflow menu (three dots) of ActionBar, someway like this:
void onOverflowMenu(boolean expanded) {
}
To handle open cases, I've tried onPrepareOptionsMenu(), but it's triggered when ActionBar is constructed or when invalidateOptionsMenu() is called. This is not what I want.
I was able to detect overflow menu is closed if user selects a menu item in onMenuItemSelected(). But I also want to detect it if user closes overflow menu by tapping outside of it, by pressing back key, and all other cases.
Is there a way to implement that?
To catch open action in the Activity:
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
...
return super.onMenuOpened(featureId, menu);
}
To catch closed action, also if user touch outside of Menu view:
#Override
public void onPanelClosed(int featureId, Menu menu) {
...
}
IMHO the simplest way is to set ActionBar.OnMenuVisibilityListener
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.addOnMenuVisibilityListener(new ActionBar.OnMenuVisibilityListener() {
#Override
public void onMenuVisibilityChanged(boolean isVisible) {
if (isVisible) {
// menu expanded
} else {
// menu collapsed
}
}
});
}

Menu button works just ONE time

I want to show a Menu when click a specific image :
popup_but = (ImageView) findViewById(R.id.imageView2);
popup_but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showmen();
}
});
And the menu :
public void showmen() {
PopupMenu popup = new PopupMenu(First.this, popup_but);
popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId())
{
case R.id.men1:
//do something
return true;
case R.id.men5:
finish();
return true;
}
return true;
}
});
popup.show();
}
It. works. now i want to do the same when click the hardware menu button. so i use this code :
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
showmen();
}
The problem is here : when menu button clicked it just show menu for the FIRST time
Take a look in the onPrepareOptionsMenu:
On Android 2.3.x and lower, the system calls onPrepareOptionsMenu()
each time the user opens the options menu (presses the Menu button).
On Android 3.0 and higher, the options menu is considered to always be
open when menu items are presented in the action bar. When an event
occurs and you want to perform a menu update, you must call
invalidateOptionsMenu() to request that the system call
onPrepareOptionsMenu().
onCreateOptionsMenu() is called every time the hardware menu button is clicked, and if it returns true each time. What I do is, style the menu items using XML and then inflate the XML inside onCreateOptionsMenu() . Every time I click the hardware menu, the menu is inflated from XML. onPrepareOptionsMenu() is only for when you want to change the menu items during the lifecycle of the app. The problem, I think is in your implementation of showmen(). I think the PopupMenu.OnMenuItemClickListener is called only the first time, not every time.

android: Update the ActionBar items depending on each Fragment in a ViewPager?

I have a flag/bookmark button in the Action Bar that I want to be toggled on or off depending which Fragment is in view of the ViewPager.
If the user flags a fragment in the ViewPager, it will be set to enabled. When the user swipes to the next one, I want the action bar button to update to unflagged. Now, I'm able to change the state of the button by having two menu items and hide one, show one when clicked.
Here's the code in my Activity to toggle the Action Bar button:
public boolean onOptionsItemSelected(MenuItem item) {
int currentItem = mPager.getCurrentItem();
switch (item.getItemId()) {
case R.id.menu_flag:
mFlagged = true;
supportInvalidateOptionsMenu();
return true;
case R.id.menu_unflag:
mFlagged = false;
supportInvalidateOptionsMenu();
return true;
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item_flag = menu.findItem(R.id.menu_flag);
MenuItem item_unflag = menu.findItem(R.id.menu_unflag);
if (mFlagged) {
//If flagged
//Show flagged item
item_flag.setVisible(false).setEnabled(false);
item_unflag.setVisible(true).setEnabled(true);
item_flag.isVisible();
} else {
//If not flagged
//Show unflagged icon
item_flag.setVisible(true).setEnabled(true);
item_unflag.setVisible(false).setEnabled(false);
}
return super.onPrepareOptionsMenu(menu);
}
The problem I'm having is that I can't access the menu item, save & restore the state of the button, i.e. if it's flagged or not from the fragment or the FragmentPagerAdapter.
How can I do this? Which level should I be accessing the Action Bar; Activity, PagerAdapter or the Fragments?
Registering a ViewPager.OnPageChangeListener on your ViewPager should do the trick. Then, override onPageSelected(int pageNum) to receive callbacks when a page changes.
public void onPageSelected(int pageNum) {
supportInvalidateOptionsMenu();
}

Categories

Resources