how to add options menu to listview in android? - java

I am trying an option menu in a class that extends ListAcitivity. How do I add an option menu to this activity, so when I click on the option button an options menu appears at the bottom of the screen.
I searched online but most tutorials refer to this function onCreateOptionsMenu() which is part of Activity class and it does not seem to work in the subclass of ListActivity.

It is Java. ListActivity (and anything that extends it) inherits all of the features and functions of Activity.
Therefore, just overriding onCreateOptionsMenu will work the same.
Boilerplate code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_xml_name, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_id:
// action
break;
}
return true;
}
:)

Related

Activity not opening when clicking menu item

I am trying to tap on the "logout" option whose id is logout but nothing happens. Below is my code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.requestpayment,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.logout:
Intent intent=new Intent(this,Logout.class);
this.startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
please help
in onCreateOptionsMenu method replace
return super.onCreateOptionsMenu(menu);
by
return true;
and replace super.onOptionsItemSelected(item); in onOptionsItemSelected() by return true
You have to return true in onCreateOptionsMenu() and in onOptionsItemSelected() after starting the activity.
The correct way would be like this.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.requestpayment,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.logout:
Intent intent=new Intent(this,Logout.class);
this.startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
If you chain a overridden method to its superclass(i.e call super.someMethod()), it means you are requesting the superclass to handle the implementation on its own.
You should return true here to tell that you successfully inflated the menu in this child Activity OR succesfully handled the onClick() event on the option items.
NOTE: Mostly, you need to chain a superclass method call, where superclass is reponsible for doing some critical stuffs. For eg:
public class ChildActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
//request superclass to do its critical stuffs
super.onCreate(savedInstanceState);
//continue with your logics here
}
Restart Android studio. Let me know if that still persists

Update ActionBar menu depending on navigation destination

I would like to change the menu items displayed in my ActionBar depending on the current destination of my NavController. By "change" I mean inflate a specified menu-xml for each destination.
The behavior could be compared to the integrated way the new navigation system changes the ActionBar title, depending on the given android:label for the destination fragment in the navigation-xml.
So far I setup a basic activity with ActionBar and DrawerLayout using the new Android navigation. Also I created all necessary XML files and Fragments to navigate between.
...
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
this._drawerLayout = this.findViewById(R.id.drawer_layout);
Toolbar toolbar = this.findViewById(R.id.action_bar);
this.setSupportActionBar(toolbar);
ActionBar actionbar = Objects.requireNonNull( this.getSupportActionBar() );
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);
NavigationView navigationView = this.findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(menuItem -> {
menuItem.setChecked(true);
this._drawerLayout.closeDrawers();
return true;
});
NavController navController = Navigation.findNavController(this, R.id.navigation_host);
NavigationUI.setupWithNavController(toolbar, navController, this._drawerLayout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Here I inflate the ActionBar menu that is kept between all destinations.
// Instead of keeping the same menu between all destinations I want to display different menus depending on the destination fragment.
this.getMenuInflater().inflate(R.menu.actionbar_items, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case android.R.id.home:
this._drawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.appbar_search:
return true;
}
return super.onOptionsItemSelected(item);
}
I thought about using an individual toolbar in each destination fragment
but I discarded the idea, because I would lose the transition animation between hamburger icon and back-arrow icon.
Is there any way to achieve this with the new navigation system or in any other way?
I just tested this. With the current version (2.2.2) of the navigation library, menu items inflated by each fragment will be added and removed as you navigate around.
But onCreateOptionsMenu is not called by default! To make things work, you have to sprinkle the magic dust and call setHasOptionsMenu(true) during initialisation.
Beware, any menu items inflated by the containing activity will always appear alongside the ones from the current fragment, so if you don't want any common menu items, it's probably easiest to not inflate a menu in the Activity.
Side note: in the Fragment class, onCreateOptionsMenu(menu, inflater) is an empty method, so the call to super.onCreateOptionsMenu(menu, inflater) in your code is a no-op.
I found a way to achieve the desired behavior by calling menu.clear(); before inflating the updated layout. I am still wondering if there is a built-in way to achieve this with the new navigation system.
In the destination Fragment I am using this now:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.toolbar_items_idle, menu);
}

Whenever I try to add a button to my actionbar, the items are added under the overflow menu

I am trying to add a simple button to my action bar, but when I try to use the following code, the items are added as items under the overflow menu, not on the actionbar itself. Any help would be appreciated,
Jacob
Here is my Java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_review, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
and my XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/seda"
android:icon="#drawable/redpinreal"
android:title="Hello Worldh"
android:showAsAction="withText|always"/>
</menu>
I also have this enabled:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Part of your code (android:showAsAction, instead of yourapp:showAsAction) is attempting to use the native action bar. Part of your code (getSupportActionBar(), instead of getActionBar()) is attempting to use the appcompat-v7 action bar backport. Choose one and stick with it.

Acessing the 'options' menu from a fragment? Android

I currently have an app which has 4 pages, these pages are being added using fragments by a pageViewer, so that I can swipe between them.
What I want, is to be able to access the 'options' menu (the one which has a 'settings' item in it by default) from each page, so that I can perform commands specifically for one page, such as a 'refresh' item, which refreshes the data in the current page.
Could anyone point me in the right direction?
Thanks!
Each Fragment can declare its own menu, which will be automatically merged with the current activity's menu.
class YourPage extends Fragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Signal that this fragment has proper actions
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
// The menu will be added to the action bar
inflater.inflate(R.menu.fragment_page_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_refresh:
{
// Handle the action...
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
}
Here's a complete example that I made, and also a relevant tutorial.

Action Bar Buttons only for one activity

So I have a Search Button in my Action Bar, But I only want that button in the first Activity, but when I open the seconde activity, the Action button is still there. How to get rid of that button in the seconde activity without removing the action bar, because I nee it from my Navigation Drawer.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
Activity1:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Activity2:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//I thought removing the INFLATER part would help, but it didn't...
return super.onCreateOptionsMenu(menu);
}
You can try hiding the search menu item in your second activity.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.action_search).setVisible(false);
return true;
}
You can refer to this question with multiple answers.
You can make the Item in your 2nd Activity invisible like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.action_search);
item.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
Another approach would be to load a second menu.xml with no item in it.
if you return false it will not be shown. as android docment saying http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu%28android.view.Menu%29
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}

Categories