I am trying to get my app to work to open my activity to click on the navigation drawer but I am not able to get the menu id.
.java
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.home_id:
Toast.makeText(MainActivity.this, "tarun", Toast.LENGTH_SHORT).show();
Intent home_Intent = new Intent(MainActivity.this,MainActivity.class);
startActivity(home_Intent);
break;
return true;
}
});
This is my menu item xml here is also provide the id and the name which will show me in navigation view
<?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/home_id"
android:title="Home"
android:icon="#drawable/home"/>
</group>
</menu>
Related
For some reason, in my bottom navigation bar, my second item doesn't work
But the first one does, I can't figure out why.
when I press the navigation_building button , I don't reach the corresponding case.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_home"
android:title="•"
android:enabled="true"
android:icon="#drawable/ic_home_black_18dp"/>
<item
android:id="#+id/navigation_building"
android:title="•"
android:enabled="true"
android:icon="#drawable/ic_domain_black_18dp"/>
<item
android:id="#+id/navigation_transfert"
android:title="•"
android:enabled="true"
android:icon="#drawable/ic_import_export_black_18dp"/>
<item
android:id="#+id/navigation_settings"
android:title="•"
android:enabled="true"
android:icon="#drawable/ic_settings_black_18dp"/>
</menu>
bottomNavigationView.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
#Override
public void onNavigationItemReselected(#NonNull MenuItem item) {
Toolbar toolbar = findViewById(R.id.toolbar);
// Handle navigation view item clicks here.
switch (item.getItemId()) {
case R.id.action_logout: {
finish();
}
case R.id.navigation_home: {
loadFragment(new HomeFragment());
break;
}
case R.id.navigation_building: {
loadFragment(new BuildingFragment());
break;
}
}
You are setting NavigationItemReselectedListener. This is only triggered when the current selected navigation item is reselected. You are probably looking for NavigationItemSelectedListener.
I have used bottom app bar and the problem is when i used a custom layout for my menus using app:actionLayout in menu, the click is not working for these menus i.e setOnMenuItemClickListener for bottomappbar not working.?
It is working fine when actionLayout not used.
BottomAppBar bottomAppBar;
bottomAppBar = findViewById(R.id.bar);
bottomAppBar.replaceMenu(R.menu.announcement_menu);
bottomAppBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_all:
Log.d("Announcement", "All clicked");
break;
case R.id.action_rec:
Log.d("Announcement", "Received clicked");
break;
case R.id.action_sen:
Log.d("Announcement", "Sent clicked");
break;
}
return false;
}
});
And the menu 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_all"
app:actionLayout="#layout/custom_menu_row_all"
android:title="All"
app:showAsAction="always" />
<item
android:id="#+id/action_rec"
app:actionLayout="#layout/custom_menu_row_received"
android:title="Received"
app:showAsAction="always" />
<item
android:id="#+id/action_sen"
app:actionLayout="#layout/custom_menu_row_sent"
android:title="Sent"
app:showAsAction="always" />
</menu>
Got working with following:
View v = bottomAppBar.getMenu().findItem(R.id.action_rec).getActionView().findViewById(R.id.tvTitle);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Announcement", "Received clicked");
}
});
I tried my level of best but I couldnt find it.I am working in navigation drawer activty where I can see a overflow Icon in top right when I click it, a Settings button like thing pops out when I click it(Settings) nothing happens
I dont no how to assing an XML to this so that when it is clicked a new activty should open
I know to create an xml and also to assign a onClickListner to the button but i am unable to proceed further since I dont no where to call the setting activty when that button(Settings button in overflow Icon) is pressed
Try this -
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
Intent intent = new Intent(this, YourSettingfActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
menu_main.xml -
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
</menu>
You can use find these below methods in activity and there you can inflate the menu and do want you want
Menu xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
<item android:id="#+id/action_search"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
<item android:id="#+id/action_logout"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
</menu>
and you have to write the code like below in your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(this, "Search", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_search:
Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I use navigation drawer in my app, set from right to left. I want it to only open when I press the options menu button is pressed(3 dots).
I tried this thread
Open navigation drawer when options menu button is pressed
but not working.
menu.xml
<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" />
</menu>
When you are pressing the 3 dots android will reveal the menu items that are collapsed.
If you want to open the drawer from a menu item, I suggest you find an icon representing the 3 dots (or keep the drawer like icon) and:
Menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ic_options"
android:icon="#drawable/ic_menu_dots"
android:showAsAction="always" />
</menu>
Code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.ic_options:
mDrawerLayout.openDrawer(your view);
return true;
}
return super.onOptionsItemSelected(item);
}
Like Surrender Kumar said, you have to open the drawer manually but from onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
mDrawerLayout.openDrawer(your view);
return true;
}
return super.onOptionsItemSelected(item);
}
You need to put showAsAction=ifRoom on your menu and an icon:
<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:icon="#drawable/action_settings"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
</menu>
I have created a menu which is activated on clicking the menu button on the device however it is showing as a list
My in res/menu/menu.xml code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menuRefresh"
android:icon="#drawable/ic_menu_refresh"
android:title="Refresh"/>
<item android:id="#+id/menuAbout"
android:icon="#drawable/ic_menu_info_details"
android:title="About"/>
</menu>
and in my main activity i have:
//Initiate Menu XMl file
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
/**
* Even handling for individual menu items selected
* Identity single menu item by its id
*/
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menuRefresh:
Toast.makeText(MainActivity.this, "Refresh Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuAbout:
Toast.makeText(MainActivity.this, "About Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If i select an item from the list, i get the notification as expected.
How do i make the menu look like the Options menu on the android dev site?
That is what the options menu looks like on new devices, a list. If you want your options to be part of the top bar (called the Action Bar), add android:showAsAction to your xml.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menuRefresh"
android:icon="#drawable/ic_menu_refresh"
android:title="Refresh"
android:showAsAction = "always"/>
<item android:id="#+id/menuAbout"
android:icon="#drawable/ic_menu_info_details"
android:title="About"
android:showAsAction = "always"/>
</menu>
For demo purposes, I chose the attribute to be "always", but you have more options:
"ifRoom"
"never"
"withText"
"always"
"collapseActionView"
Even in new devices, you can set the theme to an old one and the menu will display in the old 6-item table layout:
<style name="AppBaseTheme" parent="android:Theme">