I want to add a a menu item to the top (Where the class title is). I added the item to menu.xml and also for made an Intent to open a new class after clicking but it does not open my whole app anymore. And when I remove android:onClick:"history" it starts working again with out the menu function of course.
Have a look at the picture please: http://snag.gy/udxXc.jpg
That 'History' is my button that needs to open a new class. and this is my code:
xml:
<item
android:id="#+id/history"
android:showAsAction="always"
android:title="History"
android:onClick="history"/>
Java:
public void history(View view){
Intent historyIntent = new Intent(this, History.class);
startActivity(historyIntent);
}
Am i doing it correctly? or any better solution?
I want to add a a menu item to the top (Where the class title is)
That is called the action bar.
And when I remove android:onClick:"history" it starts working again with out the menu function of course.
I am not aware that android:onClick is valid for <item> elements.
Am i doing it correctly?
Not as far as I am aware.
or any better solution?
Override onOptionsItemSelected() and respond to the item click there.
Related
I am having problems getting my in-app menu navigation to work. Unfortunately, I have not been able to find a solution here so far. Since I'm quite a newbie to Android development, I'm not sure what info you guys need for better assistance, so bear with me.... ;]
Problem:
The "back to main menu" submenu option triggers the menu.performIdentifierAction(R.id.back_to_main_menu, 0) action. This works fine in principle, but only once. So, suppose the user navigates from the main menu to submenu A, he can return to the main menu and also go to submenu A again. But if he now wants to return to the main menu for the second time, the entire menu will be completely closed. Then the user can still click on all items, but the "Return to main menu" option does not work and instead simply closes the entire menu.
All other methods/actions associated with the menu items will be invoked properly.
What I tried so far:
I tried closing and inflating the menu again. But it lead to different other problems mostely due to not being compatible with MaterialToolbar and using an optionsMenu and related functionalities instead.
Also the solutions I found for selecting a specific menu element programmatically were about using the above mentioned method "menu.performIdentifierAction()".
App structure:
My app consists of three activities and one background service. In all activities I use the same xml menu. And the behavior is the same in all activities. The in-app menu consists of several items and submenus. In each submenu, I added a "back option" so that the user can return to the main menu without closing and reopening the menu.
The menu is included in the activity xmls for each activity, like (simplified):
\<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout_mainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"\>
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/materialToolbar_mainActivity"
style="#style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="#menu/material_toolbar_settings"
app:title="#string/page_title_mainActivity_text" />
</com.google.android.material.appbar.AppBarLayout>
Referenced like:
materialToolbar_mainActivity = findViewById(R.id.materialToolbar_mainActivity);
Clicked item handling like (simplified):
materialToolbar_mainActivity.setOnMenuItemClickListener(menuItem -\> {
// handle item selection
switch (menuItem.getItemId()) {
// ...
case (R.id.item_mainMenu_app_info):
showInfo();
return true;
// ...
case (R.id.item_submenu_backToMainMenu):
materialToolbar.getMenu().performIdentifierAction(R.id.item_main_menu, 0);
return true;
// ...
default:
return false;
}
});
If you have any suggestions or solutions, please don't hesitate and let me know. If you need any further information, please let me know as well.
With kind regards
Kind people! Tell me how to make such an elementary thing as a disabled menu item with the appropriate appearance, as in the example from material design? Programmatically. Thank you in advance
EDIT: The following examples will disable click, but do not change the appearance according to the material design:
Code example:
#Override
public void onPrepareOptionsMenu(#NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem menuItem = menu.findItem(R.id.someItem);
menuItem.setEnable(false);
}
Xml example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/someItem"
android:enabled="false"
android:title="Some title" />
</menu>
You can do Add, Remove, or Disable menu items and change text color of menu item at Runtime like that::
If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the onPrepareOptionsMenu() method. This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items. (Fragments also provide an onPrepareOptionsMenu() callback.)
On Android 2.3.x and lower, the system calls onPrepareOptionsMenu() each time the user opens the options menu (presses the Menu button).
On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the app bar. When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu().
You can see the Documentation
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/someItem"
android:enabled="false"
android:title="Some title"
app:actionViewClass="android.support.v7.widget.TextView"
// if using androidX
//app:actionViewClass="androidx.appcompat.widget.AppCompatTextView"/>
</menu>
And in java code
//sample code
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
//disable menu item and change text color
MenuItem menu1 = menu.findItem(R.id.yourmenuid);
menu1.setEnabled(false);
TextView textView = (TextView) menu1.getActionView();
textView.setTextColor(Color.BLACK);
return true;
}
you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu().
I found a workaround on how to change the text color in the menu item. It is extremely sad that such an elementary thing is not provided out of the box. Perhaps someone has a better solution, I'll be glad to see!
MenuItem menuItem = menu.findItem(R.id.some_item);
menuItem.setEnabled(false);
SpannableString s = new SpannableString(menuItem.getTitle());
s.setSpan(new ForegroundColorSpan(Color.GRAY), 0, s.length(), 0);
menuItem.setTitle(s);
Result:
I have the following question: when I return from one activity to another, I need certain element of menu to be opened. For example:
Now, when I click "Up" in my activity the first element of menu is always opened (where the running man is drawed). And I need to make such element opened, which was opened before opening new activity. For example I choose an article in Articles menu element and after clicking Up in new activity I want to be opened Articles element again. I tried to realize it as clicking on Back button:
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
But it didn't help. What's the matter?
I think you have to keep the selected item and after returning manually select it.
See the example here: https://stackoverflow.com/a/43117486/7469860
In your Manifest file, search for your Activity node and add the following line:
android:launchMode="singleTask"
This will define how your activity will be launched.
Here is an official documentation about launchMode property https://developer.android.com/guide/topics/manifest/activity-element#lmode
How will it be possible to have a menu button which call option-menus with the following condistions ( must be ):
App hides the titlebar :
requestWindowFeature(Window.FEATURE_NO_TITLE);
the targetversion in Manifest is set to 16 :
android:targetSdkVersion="16"
Some Infos and researches from my side:
Setting targetSdkVersion="10" shows the menu still in the bottom, which I would like to achieve.
Showing the titlebar shows the menubutton in top ( 3 points icon ) and the menu is also callable. But I need to hide the titlebar.
any hints , suggestions ?
thanks & regards
Andreas
Another option is to use slider menu and have it on left or right. You shouldn't include button on action bar (like facebook), you can open menu after item click or onFling event.
There is nice and easy tutorial that I tried:
http://oodlestechnologies.com/blogs/Facebook-Style-Slide-Menu-In-Android
I love this site all you guys are awesome! but here is another problem I have:
In my app I have a webview that displays a website in the entire screen, I have made a code to show a menu through pushing the phone's menu button from where i want 2 things to happen 1st menu item Go back to main screen of the app, 2nd menu item quit the app or exit the app.
First problem:
after pressing the menu button it shows the menu... if I press it again it shows the two choices twice, if I press it again now both items shows 3 times and so on!
Second Problem:
after choosing any of the two choices nothing happens!
here is my code please tell me what I'm doing wrong!
Thanks
menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menuToMenu"
android:title="Menu Principal"
/>
<item
android:id="#+id/menuToSalir"
android:title="Salir"
/>
</menu>
Backtomain.java
import android.app.Activity;
import android.os.Bundle;
public class Backtomain extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
and where I call the menu:
public boolean onPrepareOptionsMenu (Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater mostrar = getMenuInflater();
mostrar.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menuToMenu:
startActivity (new Intent("my.app.BACKTOMAIN"));
return true;
case R.id.menuToSalir:
finish();
System.exit(0);
return true;
}
return false;
}
You are calling super.onCreateOptionsMenu() from onPrepareOptionsMenu(). And, you are inflating the same options into the menu in onPrepareOptionsMenu(). Rename onPrepareOptionsMenu() to onCreateOptionsMenu(), and it will probably behave better.
Also:
If you think the my.app.BACKTOMAIN activity is running, you probably want to add FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_CLEAR_TOP to the Intent.
Get rid of the menuToSalir menu choice. No well-written Android application will call System.exit(0). Users leave your application by pressing the HOME button, no different than they might in a Web app.
To fix your first problem try onCreateOptionsMenu(), rather than onPrepareOptionsMenu().
I am not positive how to fix second problem, I usually create my menus all in java instead of using xml like you have.
I found the problem it works! I was missing the "s" at...
public boolean onOptionItemSelected(MenuItem item){
the right way is
public boolean onOptionsItemSelected(MenuItem item){
thanks for your help Tim and CommonsWare
I ran into this problem. In my case I had set the background color of the activity screen to black. When the menu popped up it had a transparent background and black text so I didn't see it working.