*Is it possible to show both the name as well as a icon of a item in the option menu?* An excellent video by the way. I really learned a lot. Thank You!
The android documentation is recommending the use of xml files, etc and not toolbar, is the a special case, where its more appropriate to use the toolbar?
Yes and i'm creating the menu items inside my_menu.xml file as shown in the video, once you set those menu item they will show up in the toolbar. You can also put a spinner (Dropdown menu) inside the toolbar as i did in this video: th-cam.com/video/CvO0Ng-_C6A/w-d-xo.html
@@CodingDemos If we want no app title on the toolbar we just need this code, right? Toolbar mtoolbaar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mtoolbaar); *getSupportActionBar().setDisplayShowTitleEnabled(false);*
That is the correct behavior, when you open the sidebar navigation and select a menu item it will close sidebar navigation in order to show the right page in full screen. For example you can open the Play Store application and you will notice the same thing
Tnx , thisis very good guid. but i have a little problem : when i try to do the if statment i cant find the item that i creat in my_menu.xml Code: MainActivity| @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_login){ } main_menu.xml|
Hi, based on the code given i don't see the part where you have override the onCreateOptionsMenu..You need to override that method so you can inflate the menu layout which is main_menu.xml..like this: @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater mMenuInflater = getMenuInflater(); mMenuInflater.inflate(R.menu.my_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_login) { Toast.makeText(MainActivity.this, "You have clicked on login menu", Toast.LENGTH_SHORT) .show(); } return super.onOptionsItemSelected(item); } Note: Please remove "android2:title="Login" from menu item Please do let me know if need help with the code. Happy coding :)
Hi, i'm sorry for the late reply...To open a webpage you will need to use an Intent and here is how to do it: - First declare a variable that will hold the webpage URL that you want to open: String website = "www.google.com"; - Now if you use that URL that doesn't start with http or https then you will not be able to open the webpage, that's why we need to handle it like this: if (!website.startsWith("") && !website.startsWith("")) { website = "" + website; } - Next we add an Intent that will open the webpage like this: Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(website)); - Finally we need to check whether the user's device have an app installed that will handle this type of action like this, otherwise show a toast message to the user with an explanation: if (mIntent.resolveActivity(getPackageManager()) != null) { startActivity(mIntent); }else{ Toast.makeText(MainActivity.this,"There is no app that will handle this action", Toast.LENGTH_SHORT).show(); } Note: It's recommended to perform the last step because sometimes the user's device may not have an app that could handle that action, which will later lead to app crash! The final code looks like this: if (item.getItemId() == R.id.action_setting) { String website = "www.google.com"; if (!website.startsWith("") && !website.startsWith("")) { website = "" + website; } Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(website)); if (mIntent.resolveActivity(getPackageManager()) != null) { startActivity(mIntent); } } I hope that helps and happy coding :)
It depends on what type of data that you want retrieve from the json object that you want share when the user tap on the share button. Let's say if you have a food app, you can allow the user who taps on share button to share dish name and the instructions on how to cook that dish
Hi, Inside onOptionsItemSelected method whenever you tap on about us menu option instead of showing a toast message like in the video you change that to something like this: if(Item.getItemId() == R.id.action_about_us){ startActivity(new Intent(MainActivity.this, AboutUsActivity.class)); }
Yeah that text actually represent the name of the app. If you want to change the title of a particular screen/page you will do something like this: after you have initialized the toolbar with it's ID, you do toolbar.setTitle("screen name");
Yeah I run the app and it says "Name of App has stopped" and closes the app. The error I get in Logcat is " Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor"
*Is it possible to show both the name as well as a icon of a item in the option menu?*
An excellent video by the way. I really learned a lot. Thank You!
Hi, yeah it's possible. I wrote a tutorial that shows you how to do it. Here is the link: www.codingdemos.com/android-options-menu-icon/
Bravo bravo j'attends d'autres tutoriels utiles pour des fonctions ou des développements d'applications
which android studio version was used in this tutorial?
The android documentation is recommending the use of xml files, etc and not toolbar, is the a special case, where its more appropriate to use the toolbar?
Yes and i'm creating the menu items inside my_menu.xml file as shown in the video, once you set those menu item they will show up in the toolbar.
You can also put a spinner (Dropdown menu) inside the toolbar as i did in this video: th-cam.com/video/CvO0Ng-_C6A/w-d-xo.html
Great tutorial!You just earn a subscriber!
How to change the text size of the menu items?
Thanks for the great tutorial! Learned all I needed to know about menus.
I have one question.
why should someone create a *toolbar* when we can display option menu in the action bar?
Hi, creating a toolbar will give you the ability to customize it by adding more functions and animations
@@CodingDemos If we want no app title on the toolbar we just need this code, right?
Toolbar mtoolbaar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mtoolbaar);
*getSupportActionBar().setDisplayShowTitleEnabled(false);*
Very well presented
it's awesome! may i ask u a question? how to hide the app_name "OptionsMenuTutorial"? thanks a lot!
Many Thanks, I have added new Menu Items to my App.
Awesome :)
Glad to come to your tutorial, good explanation. THUMBS UP!
Thanks for watching :)
Hey, do you know why my sidebar navigation is always close when i click to 1 of the menu ??
That is the correct behavior, when you open the sidebar navigation and select a menu item it will close sidebar navigation in order to show the right page in full screen. For example you can open the Play Store application and you will notice the same thing
Tnx , thisis very good guid.
but i have a little problem :
when i try to do the if statment i cant find the item that i creat in my_menu.xml
Code:
MainActivity|
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_login){
}
main_menu.xml|
Hi, based on the code given i don't see the part where you have override the onCreateOptionsMenu..You need to override that method so you can inflate the menu layout which is main_menu.xml..like this:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mMenuInflater = getMenuInflater();
mMenuInflater.inflate(R.menu.my_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_login) {
Toast.makeText(MainActivity.this,
"You have clicked on login menu",
Toast.LENGTH_SHORT)
.show();
}
return super.onOptionsItemSelected(item);
}
Note: Please remove "android2:title="Login" from menu item
Please do let me know if need help with the code.
Happy coding :)
thank you so much.
I do not know the exact English, but I can understand what you did with your codes.
Briefly as the subtitle dials: D
Great tutorial. Appreciate it.
Do you have database tutorial and all about continuously ?
I'm sorry i don't have
Creating Contextual Menus tutorial have?
Sorry to bother but what if I wanted to get a redirect to a webpage instead of just showing a message? what would I have to change?
Hi, i'm sorry for the late reply...To open a webpage you will need to use an Intent and here is how to do it:
- First declare a variable that will hold the webpage URL that you want to open: String website = "www.google.com";
- Now if you use that URL that doesn't start with http or https then you will not be able to open the webpage, that's why we need to handle it like this:
if (!website.startsWith("") && !website.startsWith("")) {
website = "" + website;
}
- Next we add an Intent that will open the webpage like this:
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(website));
- Finally we need to check whether the user's device have an app installed that will handle this type of action like this, otherwise show a toast message to the user with an explanation:
if (mIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mIntent);
}else{
Toast.makeText(MainActivity.this,"There is no app that will handle this action",
Toast.LENGTH_SHORT).show();
}
Note: It's recommended to perform the last step because sometimes the user's device may not have an app that could handle that action, which will later lead to app crash!
The final code looks like this:
if (item.getItemId() == R.id.action_setting) {
String website = "www.google.com";
if (!website.startsWith("") && !website.startsWith("")) {
website = "" + website;
}
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(website));
if (mIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mIntent);
}
}
I hope that helps and happy coding :)
I have an error in setsupportAction(m toolbar)
Thank you man, keep going good work
Thanks for watching :)
how to create something like that but no use of tool bar? by action bar default?
Just skip the part where i changed the parent theme in styles.xml, the toolbar initialization in xml file, and follow the rest of the tutorial
There is "android.support.v7.widget.Toolbar" and android.widget.Toolbar". The latter uses "setActionBar(mToolbar)".
Hi, the v7 widget toolbar is used for backwards compatibility with older versions of Android
say I have a share button on toolbar which subject and extra text are need to fetch from json object. can you tell me how to do it?
It depends on what type of data that you want retrieve from the json object that you want share when the user tap on the share button. Let's say if you have a food app, you can allow the user who taps on share button to share dish name and the instructions on how to cook that dish
This was really helpful.
Error:(8) No resource identifier found for attribute 'orderInCategory' in package 'android', I keep getting this error :(
well detailed, nice
Thanks for watching :)
Thx was really helpful tutorial
how can i resolve this it says error: incompatible types: View cannot be converted to Toolbar
Hi, can you show me code for the Toolbar?
thank man you have been a great help!
Your welcome :)
"(R.menu.stats_menu, menu)"
the first "menu" of this line is underlined, could you help me plz ???
Can you tell me what is the error message that you get?
Please can you share with me part of the code that you currently have the issue! You can find my email in the about section of this channel
Yeah very very Great tutorail !!
thanks
Your welcome :)
thnxx buddy
Thank you very much for the video, it was what I really needed (Y)
perfect tutorial
Very helpful :D
good work bro!
Thanks for watching :)
really helpful
how to create change activity when click About US ?
Hi, Inside onOptionsItemSelected method whenever you tap on about us menu option instead of showing a toast message like in the video you change that to something like this:
if(Item.getItemId() == R.id.action_about_us){
startActivity(new Intent(MainActivity.this, AboutUsActivity.class));
}
Thanks Coding Demos!
now it create problem Error:Error: Expected resource of type menu [ResourceType]
solved
App instantly closes after running and no error message is displayed.
Hi, there is no error message in the logcat? Try to clean and build your project
Is it possible to delete or change the text where it says "OptionMenuTutorial"?
and thanks for the video
Hi, yeah you can. Just open up strings.xml file, in the file you will see value for app name where you can change it to anything that you like :)
It works, but that change the name of the app too, so the text is always that name?
Yeah that text actually represent the name of the app. If you want to change the title of a particular screen/page you will do something like this: after you have initialized the toolbar with it's ID, you do toolbar.setTitle("screen name");
thx friend
why m not able to click om menu
Hi, do you see any errors?
Thanks for your help, this tutorial is good :)
Your welcome, happy coding :)
doesn't work in latest android (edit: fixed)
Does the app crash or do you see any errors in the logcat?
Yeah I run the app and it says "Name of App has stopped" and closes the app. The error I get in Logcat is " Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor"
Did you change the parent theme inside styles.xml file to this (Theme.AppCompat.Light.NoActionBar)?
Thanks! :D
Your welcome :)
with your tutorial android studio get error : R doesn't exist...
Hi, try to restart Android Studio by clicking on File\Invalidate caches/restart and then click on (Just Restart) button