I'm trying to follow this: https://developer.android.com/guide/navigation/navigation-ui along with a review of https://codelabs.developers.google.com/codelabs/android-navigation/#8 (although the second link is in Kotlin and I'm coding in Javascript) to implement an App in Android with navigation drawer on the left and a settings button (preferences) on the top right.
I'm using the NavigationUI with fragments for each option from the drawer and the settings.
My problem is that my settings fragment is not appearing. I think I'm nearly there, but despite reviewing several articles and questions cannot get it to work.
This code allows me to switch fragment from the nav drawer:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
//NavigationUI.setupWithNavController(toolbar, navController, mAppBarConfiguration);
}
If I switch the commented code on the last two lines (so I execute the last line but not second to last), to match the guide then the switching of fragment fails.
My code to handle the Settings menu is:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.onNavDestinationSelected(item, navController)
|| super.onOptionsItemSelected(item);
/*
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
// TODO: Do something to accept settings
View contextView = findViewById(R.id.nav_host_fragment); // Apparently any layout can be used
Snackbar.make(contextView, "Settings", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
//changeFragment(new SettingsFragment());
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.nav_host_fragment, new SettingsFragment(), "nh")
.addToBackStack("nh")
.commit();
return true;
}
return super.onOptionsItemSelected(item);
*/
}
The active portion matches the guide above, is getting called (proved by setting a breakpoint) but doing nothing.
If I alternatively switch this for the commented out code instead, then my settings fragment does appear but overlays the current fragment. I understand from another answer that this is because I am using two different methods to handle fragment visibility, which makes sense and is why I am trying to get my settings fragment to be handled by the NavigationUI framework.
I feel that it must be quite simple to cure, but having read and re-read the guide (and lots of answers cannot make it work)
You have different options:
Just define the SettingsFragment in your navigation graph:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--Your fragments-->
<!--Settings fragment-->
<fragment
android:id="#+id/settingsFragment"
android:name=".SettingsFragment"/>
</navigation>
Then inflate your menu with:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
In your menu use the same id used in the graph:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/settingsFragment"
.../>
</menu>
Finally override the onOptionsItemSelected method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.onNavDestinationSelected(item, navController)
|| super.onOptionsItemSelected(item);
}
You can also define a Global Action.
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--Global action-->
<action android:id="#+id/action_global_open_settings_fragment"
app:destination="#id/settingsFragment"/>
</navigation>
Then in your code to navigate to a destination just use:
Navigation.findNavController(view).navigate(R.id.action_global_open_settings_fragment);
I'm not sure if I understand your problem well. But here is my solution and I hope it helps you.
Here is my Navigation Graph
And this is the Code. As you see I've an action in SettingsFragment:
<fragment
android:id="#+id/settingsFragment"
android:name="packageName.SettingsFragment"
android:label="Settings Fragment">
<action
android:id="#+id/actionSettingsToPassword"
app:destination="#id/passwordDialogFragment"
app:enterAnim="#anim/fragment_open_enter"
app:exitAnim="#anim/fragment_open_exit" />
And this is my bottomNavigationMenu's XML:
<item
android:id="#+id/settingsFragment"
android:icon="#drawable/ic_settings"
android:title="Settings" />
Remember that the ID in the Navigation & Menu should be the same. As you see in the codes, the ID of "Settings Fragment" is the same:
android:id="#+id/settingsFragment"
I don't know if you know how to add preferences.xml in your resources. Just for sure I write it too:
Right Click on "res" New > Android Resource Directory
In the Resource Type field, pickup XML. Now you gonna have xml folder under "res". Again:
Right Click on "xml" New > XML Recourse File
Now, Here is my "preferences.xml":
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Weather Location">
<SwitchPreference
android:defaultValue="true"
android:disableDependentsState="true"
android:key="USE_DEVICE_LOCATION"
android:summary="Allow The App To Get Your Location"
android:title="Use Device Location" />
<EditTextPreference
android:defaultValue="Tehran"
android:dependency="USE_DEVICE_LOCATION"
android:key="CUSTOM_LOCATION"
android:summary="The Location For Which The Weather Is Displayed"
android:title="Location" />
</PreferenceCategory>
<PreferenceCategory android:title="Units">
<ListPreference
android:defaultValue="METRIC"
android:entries="#array/unitSystemEntries"
android:key="UNIT_SYSTEM"
android:summary="%s"
android:title="Unit System"
android:entryValues="#array/unitSystemValues"/>
</PreferenceCategory>
</PreferenceScreen>
Then create a "Kotlin" or "Java" class and set the XML to your class like this (depend on which language are you writing):
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.preferences)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
(activity as? AppCompatActivity)?.supportActionBar?.title = "Settings"
(activity as? AppCompatActivity)?.supportActionBar?.subtitle = null
}
}
And here is my Application Class:
class ForecastApplication : Application(){
override fun onCreate() {
super.onCreate()
PreferenceManager.setDefaultValues(this, R.xml.preferences, false)
}
}
and in your application's Manifest, inside the application tag, define the Application class you already created.
android:name=".ForecastApplication"
I hope this is what you needed.
Related
I am working with Android Studio and I have a side menu, most of the options are working because I configure them in "mobile_navigation".
But I need one of my items to open a URL and I don't know how to do it.
My menu xml is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_profile"
android:icon="#drawable/profile"
android:title="#string/menu_profile" />
<item
android:id="#+id/nav_web"
android:icon="#drawable/web"
android:title="#string/menu_web" />
</group>
I handle the actions of the items like this:
<fragment
android:id="#+id/nav_profile"
android:name="com.myApp.myApp.ui.profile.ProfileFragment"
android:label="#string/menu_profile"
tools:layout="#layout/fragment_profile">
<action
android:id="#+id/action_nav_profile_to_nav_fragment2"
app:destination="#id/nav_correccion" />
</fragment>
<fragment
android:id="#+id/nav_web">
</fragment>
Tried using onOptionsItemSelected (), but it still doesn't work for me. Please help
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private AppBarConfiguration mAppBarConfiguration;
private SharedPreferences preferences;
private DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
//navigationView.setNavigationItemSelectedListener(this);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_perfil, R.id.nav_web
)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main_drawer, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id= item.getItemId();
FragmentManager fragmentManager= getSupportFragmentManager();
if(id==R.id.nav_web){
Uri uri=Uri.parse("https://google.com.co");
Intent intent=new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
DrawerLayout drawerLayout =(DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
As per the Tie destinations to menu items section, Navigation uses the IDs you add to your menu xml, matching them to destinations in your navigation graph xml file.
So if you had a menu item such as
<item
android:id="#+id/nav_web"
android:icon="#drawable/web"
android:title="#string/menu_web" />
and wanted it to start a different activity, you could add an <activity> destination to your navigation graph:
<activity
android:id="#+id/nav_web"
app:data="https://google.com.co"
app:action="android.intent.action.VIEW" />
And because they have the same ID, your activity would be started when you click that item in your menu.
Android newbe here, I have am trying to get a menu option in the inflated menu in MainActivity to trigger a navigation to another screen, the issue I am having is finding the view of MainActivity for findNavController() ,I need it where I have put the ????, can anyone help ?
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar))
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
when (item.itemId) {
R.id.menu_settings -> {
val controller: NavController = Navigation.findNavController(????)
controller.navigate(R.id.settingsFragment)
}
else -> super.onOptionsItemSelected(item)
}
return true
}
}
<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.ebookfrenzy.sos.MainActivity">
<item
android:id="#+id/menu_settings"
android:orderInCategory="11"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/menu_removeads"
android:orderInCategory="20"
android:title="#string/remove_ads"
app:showAsAction="never" />
<item
android:id="#+id/menu_about"
android:orderInCategory="20"
android:title="#string/about"
app:showAsAction="never"/>
As advised in the documentation to do this you require one of the following:
Fragment.findNavController()
View.findNavController()
Activity.findNavController(viewId: Int)
How to use the above information:
Your code is located in MainActivity so the function you have to call is the 3rd Activity.findNavController(viewId: Int) the ???? is the viewId.
What is the viewId:
viewId is R.id.theIdentifierYouSetInYourXML so you have to navigate in your code to the layout you set in method setContentView this layout is called R.layout.activity_main or in xml words activity_main.xml. Within this file there should be something like the following:
<?xml version="1.0" encoding="utf-8"?>
<!-- irrelevant code -->
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
...
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
<!-- irrelevant code -->
The FragmentContainerView is important here. We require its id. In my example case above the answer will be R.id.nav_host_fragment.
Putting all together:
Navigation.findNavController(????) will be Activity.findNavController(R.id.nav_host_fragment) but you have to check your code in activity_main.xml to find your own id.
If you don't have this code in your activity_main.xml then I would suggest to start from here.
I am trying to make logout button in the navigation section in Navigation Drawer Activity. In this case I do not need to open fragment. When the button is clicked Login page needs to be show up. But when I click the button it doesn't do anything. Do I need a fragment? like start activity in the fragment?
if fragment is mandatory is there anyway app goes back to main fragment when I push "back" button on the phone? MainFragment (first fragment -> logout fragment -> signin activity and when I click "back" button can I make it goes to first fragment instead going to logout (2nd) fragment.
So this is how I coded but did not work.
Under menu folder activity_drawer
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_menu"
android:icon="#drawable/ic_restaurant_black_24dp"
android:title="#string/menu_Menu" />
<item
android:id="#+id/nav_cart"
android:icon="#drawable/ic_shopping_cart_black_24dp"
android:title="#string/menu_cart" />
<item
android:id="#+id/nav_orders"
android:icon="#drawable/ic_assignment_turned_in_black_24dp"
android:title="#string/menu_orders" />
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_exit_to_app_black_24dp"
android:title="#string/menu_Logout" />
</group>
</menu>
Since I do not need to use fragment I did not define it in mobile_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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"
android:id="#+id/mobile_navigation"
app:startDestination="#+id/nav_menu">
<fragment
android:id="#+id/nav_menu"
android:name="com.dav2020.orderforme.ui.menu.MenuFragment"
android:label="#string/menu_Menu"
tools:layout="#layout/fragment_memu">
</fragment>
<fragment
android:id="#+id/nav_cart"
android:name="com.dav2020.orderforme.ui.cart.CartFragment"
android:label="#string/menu_cart"
tools:layout="#layout/fragment_cart" />
<fragment
android:id="#+id/nav_orders"
android:name="com.dav2020.orderforme.ui.orders.OrdersFragment"
android:label="#string/menu_orders"
tools:layout="#layout/fragment_orders" />
</navigation>
And this is the code
DrawerLayout drawer = findViewById(R.id.drawer_layout);
final NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_menu, R.id.nav_cart, R.id.nav_orders, R.id.nav_logout)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
#Override
public void onDestinationChanged(#NonNull NavController controller, #NonNull NavDestination destination, #Nullable Bundle arguments) {
int menuId = destination.getId();
switch (menuId){
case R.id.nav_menu:
fab.show();
break;
case R.id.nav_cart:
//Intent cartIntent = new Intent (Home.this, Cart.class);
//startActivity(cartIntent);
fab.hide();
break;
case R.id.nav_orders:
//Intent orderIntent = new Intent (Home.this, OrderStatus.class);
//startActivity(orderIntent);
fab.hide();
break;
case R.id.nav_logout:
Intent signIn = new Intent(Home.this, SignInActivity.class);
signIn.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(signIn);
fab.hide();
break;
default:
fab.show();
}
}
});
I've been trying to figure out how the default Navigation Drawer Activity template came with Android Studio navigates between different fragments. I understand that this menu is an implementation using AndroidX navigation component and navigation graph, but I just cannot understand how each menu item is mapped to its corresponding fragment. I don't see any listener or onNavigationItemSelected() etc. Can someone explain how the mapping between menuItem and corresponding fragment was achieved?
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
mAppBarConfiguration = new AppBarConfiguration.Builder(
navController.getGraph())
.setDrawerLayout(drawer)
.build();
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_menu_camera"
android:title="#string/menu_home" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="#string/menu_gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="#string/menu_slideshow" />
</group>
</menu>
nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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"
android:id="#+id/mobile_navigation"
app:startDestination="#+id/nav_home">
<fragment
android:id="#+id/nav_home"
android:name="com.buzzz.myapplication.ui.home.HomeFragment"
android:label="#string/menu_home"
tools:layout="#layout/fragment_home">
<action
android:id="#+id/action_HomeFragment_to_HomeSecondFragment"
app:destination="#id/nav_home_second" />
</fragment>
<fragment
android:id="#+id/nav_home_second"
android:name="com.buzzz.myapplication.ui.home.HomeSecondFragment"
android:label="#string/home_second"
tools:layout="#layout/fragment_home_second">
<action
android:id="#+id/action_HomeSecondFragment_to_HomeFragment"
app:destination="#id/nav_home" />
<argument
android:name="myArg"
app:argType="string" />
</fragment>
<fragment
android:id="#+id/nav_gallery"
android:name="com.buzzz.myapplication.ui.gallery.GalleryFragment"
android:label="#string/menu_gallery"
tools:layout="#layout/fragment_gallery" />
<fragment
android:id="#+id/nav_slideshow"
android:name="com.buzzz.myapplication.ui.slideshow.SlideshowFragment"
android:label="#string/menu_slideshow"
tools:layout="#layout/fragment_slideshow" />
</navigation>
Thank you very much.
As per the Update UI components with NavigationUI documentation, the setupWithNavController() methods hook up UI elements (such as your NavigationView) with the NavController.
Looking at the setupWithNavController() Javadoc:
Sets up a NavigationView for use with a NavController. This will call onNavDestinationSelected when a menu item is selected. The selected item in the NavigationView will automatically be updated when the destination changes.
So internally, this is setting up the appropriate listeners - both on the NavigationView to handle menu selections and on the NavController to update the selected item when the current destination changes.
Looking at the onNavDestinationSelected() Javadoc, it becomes clear how the menu items and navigation graph destinations are matched:
Importantly, it assumes the menu item id matches a valid action id or destination id to be navigated to.
So clicking on a menu item with android:id="#+id/nav_home" will navigate to the destination with android:id="#+id/nav_home".
I'm trying to use Android ActionBar in my app, and have an option that's hidden away in the overflow menu.
There's a lot of documentation out there, but it's confusing because most of it is only relevant to very old versions of Android, and when you try applying the same concepts, they don't work anymore or work differently.
This is in my Activity layout
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:titleTextColor="#android:color/white"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
And this is in my Activity's onCreate() method
// sets up activity toolbar
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
myToolbar.showOverflowMenu();
myToolbar.setTitleTextColor(R.color.lightPrimaryText);
I've also tried inflating a menu xml file from the onCreateOptionsMenu(), but that also didn't give me the results I wanted.
Define a Menu for your Toolbar in the res/menu resource folder, for example:
toolbar_menu.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activity.MainActivity">
<item
android:id="#+id/action_sign_out"
android:title="#string/toolbar_sign_out"
app:showAsAction="never"/>
</menu>
Setting app:showAsAction="never" ensures that this MenuItem will not be shown in the Toolbar, but placed in the overflow menu instead.
The theme of your Activity should be (or derive from) one of the NoActionBar themes (Theme.AppCompat.NoActionBar for example, or Theme.MaterialComponents.NoActionBar if you're using Material Components).
In your Activity, set up your Toolbar:
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
And override onCreateOptionsMenu() to inflate your previously defined menu resource:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return true;
}
You can override onOptionsItemSelected() to define the onClick behaviour of your MenuItem(s):
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_sign_out: {
// do your sign-out stuff
break;
}
// case blocks for other MenuItems (if any)
}
return true;
}
in manifest file declare
android:theme="#style/AppTheme.NoActionBar"
like this :
<activity
android:name=".ActivityName"
android:label="#string/label"
android:theme="#style/AppTheme.NoActionBar" />
and add this to your style :
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and call this in Activity onCreate() :
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
override this method in activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.product_list, menu);
//U can find item set icon and stuff...
MenuItem item= menu.findItem(R.id.action_search);
return true;
}
and declare your menu like this for overflow menu:
<?xml version="1.0" encoding="utf-8"?>
<menu >
<group>
<item
android:id="#+id/sign_out"
android:title="#string/sign_out" />
<item
android:id="#+id/about"
android:title="#string/about" />
</group>
</menu>
and for handle item selection call this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.sign_out:
//do stuff
break;
}
return super.onOptionsItemSelected(item);
}
done :)
Simple do This copy this code on your MainActivet`
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Group gp=(Group)findViewById(R.id.order);
return true;
}
return super.onOptionsItemSelected(item);
}`
Now Make Directory file for menu name for this go on Android_Studio->app Folder->Right_Click->New->Directory-> Enter name menu now Create a xml file in there with menu2.xml name
and past this code on menu2.xml file
<?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/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"
android:icon="#android:drawable/ic_input_add"
/>
</menu>
if any Query Please text me