ActionBar menu not works in ActionMode - android

I am newbie to Android Development, I am looking to get an one more menu on ActionMode, that is along with cut,copy,selectAll. I want to add "mark" to this menu.
So I added an item in main.xml below is the following code of it.
<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.example.fn.MainActivity" >
<item
android:id="#+id/action_settings1"
android:orderInCategory="100"
android:onClick="onContextualMenuItemClicked"
android:title="Mark"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
This was tested and works fine in most of the phones, but in few phones it long pressing on texts, it shows the message
unfortunately, app has stopped
there is no other process and my app was closed suddenly.
I am not sure why? can someone assist me here?
If I remove this following line from that item, it will works.
android:onClick="onContextualMenuItemClicked"
but I am triggering that onclick in Activity,
public void onContextualMenuItemClicked(MenuItem item) {
/*switch (item.getItemId()) {
case R.id.mark:
webView.loadUrl("javascript:doMouseUp();");
break;
default:
System.out.println("default");
break;
}
if (mActionMode != null) {
mActionMode.finish();
}*/
}
Update
#Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
mActionMode = mode;
Menu menu = mode.getMenu();
menu.clear();
mode.getMenuInflater().inflate(R.menu.main, menu);
}
super.onActionModeStarted(mode);
}

It looks like you are extending the MainActivity with ActionBarActivity. This is supports only after the Android KitKat. In version 5 and later it doesn't make problem.
You need to extend the MainActivity with Activity, then it will works for you.
public class MainActivity extends Activity {
Then, You need to have separate xml file (myMenu.xml) under /res/menu in your project structure.
myMenu.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="com.example.fn.MainActivity" >
<item
android:id="#+id/mark"
android:showAsAction="always"
android:onClick="onContextualMenuItemClicked"
android:title="Mark">
</item>
</menu>
Copy this method and change as per your need and replace in Activity.
#Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
mActionMode = mode;
Menu menu = mode.getMenu();
menu.clear();
mode.getMenuInflater().inflate(myMenu, menu);
}
super.onActionModeStarted(mode);
}
That's all, hope now it works for you.

You can try something dynamic!!
Override this method
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return true;
}
Make this Method
private void CreateMenu(Menu menu) {
MenuItem mnu1 = menu.add(0, 0, 0, "Delete");
{
//mnu1.setIcon(R..ic_menu_delete);
//R.drawable.ic_menu_delete
mnu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
MenuItem mnu2 = menu.add(0, 1, 1, "Edit");
{
//mnu2.setIcon();
mnu2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
}
And for onClick override this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return MenuChoice(item);
}
and create this method
private boolean MenuChoice(MenuItem item) {
switch (item.getItemId()) {
case 0:
//your method here
return true;
case 1:
//your method here
return true;
}
return false;
}
My code creates 2 options

Related

open a menu from OnClickListener

I want to open a menu from a OnClickListener
without using the method onCreateOptionsMenu
My code:
toolbar.setNavigationIcon(R.drawable.week); //your icon
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v ) {
}
});
Thanks in advance!
I think you want to show/hide a menu item based on actions from users. To do that you must use onCreateOptionsMenu and whenever you want to show/hide the menu item, then call invalidateOptionsMenu (this method will call onCreateOptionsMenu again).
boolean mShowMenu = false;
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.your_menu, menu);
MenuItem item = menu.findItem(R.id.your_menu_item);
item.setVisible(showMenu);
return true;
}
And in your code, when you want to show menu item.
toolbar.setNavigationIcon(R.drawable.week); //your icon
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v ) {
mShowMenu = true;
invalidateOptionsMenu();
}
});
And give it a try.
You should need to create menu Interface in xml File Like this
<item
android:id="#+id/settings"
android:title="Setting"
app:showAsAction="never" />
<item
android:id="#+id/my_activity"
android:title="My Activity"
app:showAsAction="always"
android:icon="#android:drawable/btn_radio"/>
After that in the Java code of a particular class You need to create the code like this;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.my_activity) {
Intent intent1 = new Intent(this,MyActivity.class);
this.startActivity(intent1);
return true;
}
if (id == R.id.settings) {
Toast.makeText(this, "Setting", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
Hopefully this may resolve your problem

Android not showing menu

I'm a new one in Android. I have some problems with showing menu. I don't see three dots in right corner in my screen. Please, help me to understand my mistake. THANK YOU A LOT!
Activity:
public class MainActivity extends AppCompatActivity {
private EditText numb1;
private EditText numb2;
private Button btn_sum;
private Button btn_extr;
private Button btn_mult;
private Button btn_div;
private TextView result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*some code*/
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.reset:
numb1.setText("");
numb2.setText("");
break;
case R.id.exit:
fileList();
break;
}
return super.onOptionsItemSelected(item);
}
}
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">
<item android:id="#+id/reset"
android:title="#string/reset"
app:showAsAction="never"/>
<item android:id="#+id/exit"
android:title="#string/exit"
app:showAsAction="never"/>
</menu>
Menu Items are not showing on Action Bar
Check this answer
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"
android:title="#string/action_option1"/>
<item
android:id="#+id/action_settings34"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"
android:title="#string/action_option2"/>
<item
android:id="#+id/action_settings3"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"
android:title="#string/action_option3"/>
</menu>
Use this code in your activity, but you should have action bar in it.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
return true;
}
if you run your application on oldest version of samsung or other the three DOTS is not appear on ActionBar
so try to click on Option Key on mobile
The solution to appear Three DOTS
call this method in your application class' onCreate method
private void makeActionOverflowMenuShown() {
//devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
}
i am also new to android i guess u wrote onCreateOptionsMenu(Menu menu) inside on create
try this
public class MainActivity extends AppCompatActivity {
private EditText numb1;
private EditText numb2;
private Button btn_sum;
private Button btn_extr;
private Button btn_mult;
private Button btn_div;
private TextView result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*some code*/
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.reset:
numb1.setText("");
numb2.setText("");
break;
case R.id.exit:
fileList();
break;
}
return super.onOptionsItemSelected(item);
}
}
You have done the minor mistake while creating the option menu .you should call the onCreateOptionsMenu() and onOptionsItemSelected() method outside the onCreate(Bundle savedInstanceState) method. you may check the following example :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.reset:
break;
case R.id.exit:
fileList();
break;
}
return super.onOptionsItemSelected(item);
} }
Help me to understand my mistake - Sure
The Main culprit is in your menu XML file app:showAsAction="never" this line replace this line with app:showAsAction="ifRoom"
here showAsAction is set to never means you tell that don't show my menu in action bar if you replace with "ifRoom" means you said show my all menu in action bar and if there is space for all my menus

How to add search and checkbox icon with their functions in android action bar?

I want to add a searchview and checkbox into action bar menu. And this checkbox will be visible if searchview is opened. And in it's opposite case it will be hidden. How I can do this?
I do something below . But it doesnt work correctly. I want hide checkbox (In my notes) when searchview is closed.
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">
<item
android:id="#+id/search_button"
android:icon="#drawable/ic_icon_search"
android:title="Arama"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView">
</item>
<item
android:id="#+id/search_in_my_notes_checkbox"
app:showAsAction="ifRoom"
android:title="#string/search_in_my_notes"
android:checkable="true"
android:visible="false"
/>
</menu>
HomeActivity.java
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.search_button){
MenuItem searchInMyNotesCheckbox = (MenuItem)menu.findItem(R.id.search_in_my_notes_checkbox);
searchInMyNotesCheckbox.setVisible(true);
}
return super.onOptionsItemSelected(item);
}
I found an alternative solution. I will use two checkbox image with checked and unchecked. And I will override onCreateOptionsMenu like this :
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem search = (MenuItem)menu.findItem(R.id.search_button2);
final MenuItem searchInMyNotesCheckbox = (MenuItem) menu.findItem(R.id.search_in_my_notes_checkbox);
MenuItemCompat.setOnActionExpandListener(search,
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// Return true to allow the action view to expand
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
MenuItem searchInMyNotesCheckbox = (MenuItem) menu.findItem(R.id.search_in_my_notes_checkbox);
searchInMyNotesCheckbox.setVisible(false);
return true;
}
});
searchInMyNotesCheckbox.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getIcon() == R.drawable.checked_checkbox){
item.setIcon(R.drawable.unchecked_checkbox);
}
else {
item.setIcon(R.drawable.checked_checkbox);
}
return false;
}
});
return super.onCreateOptionsMenu(menu);
}

registerForContextMenu for menuItems

Ok I'm sure this is a dumb question but I couldn't find the answer online. I want to register one of the menu Items for a context Menu, but I don't know how to can't figure out how to access the MenuItem as a view. So when I click one of the buttons on the ActionBar of my application, I want a context menu to pop up. I'm guessing this has to be done in OnCreateOptionsMenu?
Edit: Update... Adding this code works partially but overrides my Drawable.
XML
<item android:id="#+id/Favorites"
android:title="favorite_label"
android:icon="#android:drawable/ic_menu_myplaces"
android:actionViewClass="android.widget.ImageButton"
android:showAsAction="always"
/>
Main Activity
FavoriteButton = (ImageButton) menu.findItem(R.id.Favorites).getActionView();
FavoriteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
registerForContextMenu(v);
}
});
Hi follow below link exmple link
ListView list = (ListView)findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, Countries);
list.setAdapter(adapter);
registerForContextMenu(list);
change registerForContextMenu(list); to registerForContextMenu(buttonname);
i hope it useful to you.
In the resource/menu/main.xml add the below code:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<menu>
<item
android:id="#+id/gray"
android:title="#string/gray" />
<item
android:id="#+id/green"
android:title="#string/green" />
<item
android:id="#+id/red"
android:title="#string/red" />
<item
android:id="#+id/orange"
android:title="#string/orange" />
<item
android:id="#+id/purple"
android:title="#string/dark_blue" />
</menu>
</item>
</menu>
and in the main activity you can access this by overridding the belo method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.gray:
color = Color.parseColor("#FF666666");
return true;
case R.id.green:
color = Color.parseColor("#FF96AA39");
return true;
case R.id.orange:
color = Color.parseColor("#FFF4842D");
return true;
case R.id.purple:
color = Color.parseColor("#FF5161BC");
return true;
}
return super.onOptionsItemSelected(item);
}
Here is what I have made:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// "menu_main" is the menubar of my actionbar menu
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// "item" is the menu button I have pressed
int id = item.getItemId();
// "Settings" button
if (id == R.id.action_settings) {
return true;
}
// Difficulty button to change the difficulty of my game
else if (id == R.id.action_difficulty) {
View view = findViewById(R.id.action_difficulty);
registerForContextMenu(view);
openContextMenu(view);
}
return super.onOptionsItemSelected(item);
}
This works fine for me!
BUT! If your menubar button is behind the "three dots" button, the line
registerForContextMenu(view);
will crash your application. I'm figuring out why..

Android, How to create option Menu

Here I tried to make option menu, but menu is not displaying on screen, so please guide me where am I doing mistake...
MenuTest.java
public class MenuTest extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.more_tab_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.feeds:
break;
case R.id.friends:
break;
case R.id.about:
break;
}
return true;
}
}
And my XML file is more_tab_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/feeds"
android:title="Feeds"/>
<item
android:id="#+id/friends"
android:title="Friends"/>
<item
android:id="#+id/about"
android:title="About"/>
</menu>
Please guide me,
public class MenuTest extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.more_tab_menu, menu);
// return true so that the menu pop up is opened
return true;
}
}
and don't forget to press the menu button or icon on Emulator or device
please see :==
private int group1Id = 1;
int homeId = Menu.FIRST;
int profileId = Menu.FIRST +1;
int searchId = Menu.FIRST +2;
int dealsId = Menu.FIRST +3;
int helpId = Menu.FIRST +4;
int contactusId = Menu.FIRST +5;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(group1Id, homeId, homeId, "").setIcon(R.drawable.home_menu);
menu.add(group1Id, profileId, profileId, "").setIcon(R.drawable.profile_menu);
menu.add(group1Id, searchId, searchId, "").setIcon(R.drawable.search_menu);
menu.add(group1Id, dealsId, dealsId, "").setIcon(R.drawable.deals_menu);
menu.add(group1Id, helpId, helpId, "").setIcon(R.drawable.help_menu);
menu.add(group1Id, contactusId, contactusId, "").setIcon(R.drawable.contactus_menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
// write your code here
Toast msg = Toast.makeText(MainHomeScreen.this, "Menu 1", Toast.LENGTH_LONG);
msg.show();
return true;
case 2:
// write your code here
return true;
case 3:
// write your code here
return true;
case 4:
// write your code here
return true;
case 5:
// write your code here
return true;
case 6:
// write your code here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Change your onCreateOptionsMenu method to return true. To quote the docs:
You must return true for the menu to be displayed; if you return false it will not be shown.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.folderview_options, menu);
return (super.onCreateOptionsMenu(menu));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.locationListRefreshLocations) {
Cursor temp = helper.getEmployee(active_employeeId);
String[] matches = new String[1];
if (temp.moveToFirst()) {
matches[0] = helper.getEmployerID(temp);
}
temp.close();
startRosterReceiveBackgroundTask(matches);
} else if (item.getItemId()==R.id.locationListPrefs) {
startActivity(new Intent(this, PreferencesUnlockScreen.class));
return true;
}
return super.onOptionsItemSelected(item);
}
you can create options menu like below:
Menu XML code:
<?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/Menu_AboutUs"
android:icon="#drawable/ic_about_us_over_black"
android:title="About US"/>
<item
android:id="#+id/Menu_LogOutMenu"
android:icon="#drawable/ic_arrow_forward_black"
android:title="Logout"/>
</menu>
How you can get the menu from MENU XML(Convert menu XML to java):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_options_menu,menu);
return super.onCreateOptionsMenu(menu);
}
How to get Selected Item from Menu:
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.Menu_AboutUs:
//About US
break;
case R.id.Menu_LogOutMenu:
//Do Logout
break;
}
return super.onOptionsItemSelected(item);
}
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
public class AndroidWalkthroughApp2 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// show menu when menu button is pressed
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// display a message when a button was pressed
String message = "";
if (item.getItemId() == R.id.option1) {
message = "You selected option 1!";
}
else if (item.getItemId() == R.id.option2) {
message = "You selected option 2!";
}
else {
message = "Why would you select that!?";
}
// show message via toast
Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
toast.show();
return true;
}
}
Replace return super.onCreateOptionsMenu(menu); with return true; in your onCreateOptionsMenu method
This will help
And you should also have the onCreate method in your activity
The previous answers have covered the traditional menu used in android. Their is another option you can use if you are looking for an alternative
https://github.com/AnshulBansal/Android-Pulley-Menu
Pulley menu is an alternate to the traditional Menu which allows user to select any option for an activity intuitively. The menu is revealed by dragging the screen downwards and in that gesture user can also select any of the options.
Android UI programming is a little bit tricky. To enable the Options menu, in addition to the code you wrote, we also need to call setHasOptionsMenu(true) in your overriden method OnCreate().
Hope this will help you out.
IF your Device is running Android v.4.1.2 or before,
the menu is not displayed in the action-bar.
But it can be accessed through the Menu-(hardware)-Button.
Good Day
I was checked
And if You choose Empty Activity
You Don't have build in Menu functions
For Build in You must choose Basic Activity
In this way You Activity will run onCreateOptionsMenu
Or if You work in Empty Activity from start
Chenge in styles.xml the

Categories

Resources