In my app i have one activity with two fragment, the app has a toolbar with search action, that search action must be visible only in the second fragment.
So how could i hide the search button and show it only when i'm in fragment2?
My menu.xml looks like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="it.gabtamagnini.visualstock.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/app_bar_search"
android:icon="#drawable/ic_search_black_24dp"
android:title="#string/cerca"
app:showAsAction="ifRoom|withText"
app:actionViewClass="android.widget.SearchView" />
</menu>
I'm using kotlin
In your second fragment:
#Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.app_bar_search);
if(item != null)
{
item.setVisible(false);
}
}
I solved by adding the following code in my onViewCreated
setHasOptionsMenu(true)
And i had to override the onPrepareOptionsMenu like this:
override fun onPrepareOptionsMenu(menu: Menu) {
super.onPrepareOptionsMenu(menu)
val item: MenuItem = menu.findItem(R.id.app_bar_search)
item.isVisible = true
}
Related
I have created a menu icon on action bar. When it is clicked, I expect it will show something like this
But it display on the left hand side instead.
What could be this issue ?
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
inflater!!.inflate(R.menu.fragment_menu, menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.getItemId()
if (id == R.id.more) {
var popup: PopupMenu? = PopupMenu(context!!, view!!)
popup?.menuInflater?.inflate(R.menu.pop_up_menu, popup.menu)
popup?.show()
popup?.setOnMenuItemClickListener({ item: MenuItem? ->
when (item!!.itemId) {
R.id.logOut -> {
}
}
true
})
} else
super.onOptionsItemSelected(item)
return true
}
fragment_menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/more"
android:title="menu"
app:showAsAction="always"
android:icon="#drawable/ic_more"/>
</menu>
pop_up_menu
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/logOut"
app:showAsAction="always"
android:title="Log Out"
/>
</menu>
The view I passing is this
#Nullable
public View getView() {
return this.mView;
}
You are passing the wrong view as what #CodeRed mentioned.
Replace
var popup: PopupMenu? = PopupMenu(context!!, view!!)
to
val vItem = getActivity().findViewById<View>(R.id.more)
var popup: PopupMenu? = PopupMenu(context!!, vItem)
Menu in ActionBar is populated automatically by system, you don't need to write code to populate it.
To achieve the menu like figure 1, fragment_menu should be like:
<menu>
<item android:title="Search" />
<item android:title="Locate" />
<item android:title="Refresh" />
<item android:title="Help" />
<item android:title="Check for update" />
</menu>
onOptionItemSelected is used to handle when above menu is clicked, not used to show the menu.
popup menu is free position menu like right click menus in windows, not the one you expect in figure 1.
I have a list of menu items in my actionbar. Each item click should trigger a different method. But onOptionsItemSelected is never called.
This is how the actionbar is defined in MainActivity:
public class MainActivity extends AppCompatActivity {
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings_1) {
//do something
return true;
} else if (id == R.id.action_settings_2) {
//do something
return true;
} else if (id == R.id.action_settings_1) {
//do something
return true;
}
return super.onOptionsItemSelected(item);
}
...
}
This is the actionbar menu layout menu_main:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.MainActivity">
<item
android:id="#+id/action_settings_1"
android:orderInCategory="1"
android:title="Item 1"
app:showAsAction="never" />
<item
android:id="#+id/action_settings_2"
android:orderInCategory="2"
android:title="Item 2"
app:showAsAction="never" />
<item
android:id="#+id/action_settings_3"
android:orderInCategory="3"
android:title="Item 3"
app:showAsAction="never" />
</menu>
How can I set up the actionbar so that onOptionsItemSelected is called when an actionbar item is clicked?
Inside your onCreateOptionsMenu, return true instead of calling super. That should do it
In the onCreate(), call setSupportActionbar(), like so
toolbar = (Toolbar)findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
Just do the change as below :
#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;
}
This problem can potentially also happen if using the menu attribute in XML:
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:menu="#menu/your_menu"
app:navigationIcon="#drawable/..."
app:title="#string/..." />
So app:menu="#menu/your_menu" doesn't work with the overridden fragment method onOptionsItemSelected. The menu item click listener has to be set to the Toolbar
private fun setMenuClickListener(toolbar: MaterialToolbar) = with(toolbar) {
setOnMenuItemClickListener { menuItem ->
if (menuItem.itemId == R.id.yourId) {
//do something
return#setOnMenuItemClickListener true
}
//this is a lambda so it can be just false,
//added return to make it explicit
return#setOnMenuItemClickListener false
}
}
So what we are looking for is the setOnMenuItemClickListener from MaterialToolbar
Try this:
Instead of implement
#Override
public boolean onCreateOptionsMenu
...
In activity_main.xml add:
<androidx.appcompat.widget.Toolbar
android:id="#+id/myToolbar"
app:title="#string/myTitle"
app:menu="#menu/menu_main"
>
</androidx.appcompat.widget.Toolbar>
Next, in menu_main.xml add in each item:
android:onClick="onOptionsItemSelected"
Like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.MainActivity">
<item
android:id="#+id/action_settings_1"
android:orderInCategory="1"
android:title="Item 1"
app:showAsAction="never"
android:onClick="onOptionsItemSelected" />
<item
android:id="#+id/action_settings_2"
android:orderInCategory="2"
android:title="Item 2"
app:showAsAction="never"
android:onClick="onOptionsItemSelected" />
<item
android:id="#+id/action_settings_3"
android:orderInCategory="3"
android:title="Item 3"
app:showAsAction="never"
android:onClick="onOptionsItemSelected" />
</menu>
I have a navigation menu which looks like this -
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home"
android:title="Home" />
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_logout"
android:title="Logout" />
<item
android:id="#+id/choose_bus"
android:title="Choose a bus">
<menu>
<group
android:id="#+id/group_select_bus"
android:checkableBehavior="single">
<!--<item-->
<!--android:id="#+id/menu_option1"-->
<!--android:title="Bus 1" />-->
<!--<item-->
<!--android:id="#+id/menu_option2"-->
<!--android:title="Bus 2" />-->
</group>
</menu>
</item>
I want to add menu_option1 and menu_option2 dynamically from the activity. I tried this but nothing adding -
#Override
public boolean onNavigationItemSelected(MenuItem item) {
NavigationView navView = (NavigationView) findViewById(R.id.nav_view);
Menu menu = navView.getMenu();
MenuItem item1 = menu.getItem(2);
SubMenu subMenu = item1.getSubMenu();
subMenu.add("Bus 1");
subMenu.add("Bus 2");
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Try some thing like this
Menu menu = navView.getMenu();
menu.add(R.id.group_select_bus,Menu.NONE,Menu.NONE,Youritem);
Try this:
MenuItem item = mNavigationView.getMenu().getItem(2);
item.getSubMenu().add(R.id.group_select_bus, id, Menu.NONE, "Youritem");
Another approach is to add all items in navigation menu and then change their visibility on specific activity or fragment, like
MenuItem item = mNavigationView.getMenu().getItem(5);
item.setVisible(false);
Seems a little late to answer. There is workaround/hack for this. No need to add list. Just add this little line above your code. It surely works.
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.candidates -> {
binding.navSideView.post {
navSideView.menu.findItem(R.id.candidateManagement).isVisible = !navSideView.menu.findItem(R.id.candidateManagement).isVisible
}
}
}
return true
}
You need to add navigationview.post Runnable before changing visibility. It will update the menu. It works with group too. :)
I have an action bar that puts everything in a menu in the top right, which the user clicks and the menu options open up.
I inflate the action bar menu with this on each activity I use it:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
And my xml for main2.xml is:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Seach"/>
</menu>
My question is do I put an onclick in the item in the xml and if so where do I put the onclick method it calls? Do I need to put it in every activity I launch this action bar in?
If you add an onClick attribute on your menu item like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:onClick="doThis"
android:title="Seach"/>
</menu>
Then in your activity:
public void doThis(MenuItem item){
Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
}
Note:
ActionBarSherlock is deprecated. Unless you are developing an app for Android 4.0 or older, please don't use it. But if you are using the library, you will have to import
import com.actionbarsherlock.view.MenuItem;
and not
import com.android.view.MenuItem;
In addition, you could do something like this: ActionBar Sherlock Menu Item OnClick
which #adneal mentions.
In my opinion
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onCreateDialog(getTaskId());
}
});
}
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/add_text_id" android:title="Add"
android:icon="#drawable/ic_add_btn"
android:orderInCategory="100" app:showAsAction="ifRoom" />
I have menu file like
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_select_all"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_select_all"/>
<item
android:id="#+id/action_deselect_all"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_deselect_all"
android:visible="false"/>
</menu>
and i want to show only one at a time, when i click on one, other show be invisible. can we not get activity's menu by any method of activity.
In onCreateOptionsMenu(Menu menu)
after inflate do this
if (CONDITION) {
MenuItem item = menu.findItem(R.id.action_select_all);
item.setVisible(false);
}
else
{
MenuItem item = menu.findItem(R.id.action_deselect_all);
item.setVisible(false);
}
Make sure that you call invalidateOptionsMenu(); when you need to refresh menu