Open custom menu on tap of option menu - android

I have activity with custom actionbar(Basically custom layout xml), therefore I have created a custom menu button as well in layout file and showing a popup,
I am usingthis callback to showing same popup on tap of hardware option menu, but give a weird behaviour e.g this will open custom menu whenvery onresume is run.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//return super.onCreateOptionsMenu(menu);
showHideMenu();
return false;
}
can you please suggest where I can placeshowHideMenu(); will which work like custom option menu
//showHideMenu() code
private void showHideMenu() {
if (mPopUpMenu.isShowing())
dismissPopUpMenu();
else
showMenu();
}
private void dismissPopUpMenu() {
// dismiss menu
if (mPopUpMenu != null)
mPopUpMenu.dismiss();
}

You should look for an up keyevent:
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
showHideMenu();
return true;
}
return super.onKeyUp(keyCode, event);
}
Include this for PopUp:
mPopUpMenu.getContentView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// ... payload action here. e.g. popupMenu.dismiss();
return true;
}
return false;
}
});
Reference:
Detecting physical Menu key press in Android

you can use PopupMenu for showing custom menu.
for this you have to find a view to attach menu.and then you can wrote
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu,popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
for more information you can see http://www.androidhive.info/2011/09/how-to-create-android-menus/

Related

How to disable actionbar back button in ActionMode

I use ActionMode to edit some content, those three buttons on the right do everything needed, and the back/home/up button on the left becomes redundant.
Here's the ActionMode
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.editormenu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_cancel:
selectedView.onCancelEditing();
selectedView.enableEditMode(selectedView,false);
mode.finish();
return true;
case R.id.action_remove:
selectedView.enableEditMode(selectedView,false);
relContainer.removeView(selectedView);
mode.finish();
return true;
case R.id.action_done:
selectedView.enableEditMode(selectedView,false);
mode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
I tried to use that back/home/up button as "cancel" button but it seems that it's id is not android.R.id.home nor R.id.home
so question is: How do I remove/disable this button, OR How do I make use of it?
Just call dispatchKeyEvent method and perform action on it:
Have a look on the following example:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(mActionModeIsActive) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
// handle your back button code here
return true; // consumes the back key event - ActionMode is not finished
}
}
return super.dispatchKeyEvent(event);
}
For further info:
Prevent to cancel Action Mode by press back button
This answer is what worked for me:
remove back/home button from action mode on long press in android
I added these lines to my styles.xml
<item name="actionModeCloseDrawable">#color/colorAccent</item>
<item name="actionModeCloseButtonStyle">#style/Widget.AppCompat.ActionMode</item>
<item name="actionModeBackground">#color/colorAccent</item>
I am using #color/colorAccent as the background colour for the ActionBar in ActionMode.

submit a query when click on search button in keyboard

I have search view in my fragment. when I click on it , keyboard is open and I can type text. I want when I click on search button in keyboard , my query send to my server and get result but I don't know how get search event. any solution?
You have to extend OnQueryTextListener, attach the listener and implement onQueryTextSubmit.
Example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
searchView = (SearchView) menu.findItem(R.id.mActionSearch).getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
//Do something here
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
Pozzo Apps Answer is right
but for api below 11 and compat library you can use something like this :
MenuItem search_menu=(MenuItem)menu.findItem(R.id.action_search);
SearchView searchView =(SearchView)MenuItemCompat.getActionView(search_menu);
You can also apply setOnKeyListener on search view like as below:
searchview.setOnKeyListener(new View.OnKeyListener(
{
Public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
Case KeyEvent.KECODE_ENTER:
// Apply action which you want on search key press on keypad
return true;
default:
break;
}
} return false;
}
});
You have to add new OnQueryTextListener, and implement onQueryTextSubmit. This also works in a fragment.
Example:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main_search, menu);
SearchView sv = (SearchView) menu.findItem(R.id.action_search).getActionView();
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
//Do something here
Toast.makeText(getActivity(), "Search: " + query, Toast.LENGTH_SHORT ).show();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
super.onCreateOptionsMenu(menu,inflater);
}

Android don't want to see the OptionMenu

I have added a button in the action bar, it works fine but if I press the physical menu button, the menu appears action_settings. Instead I want to use only the menu in the ActionBar and not that of the physical menu button.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
getActivity().getMenuInflater().inflate(R.menu.menu_scegli, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_c:
Intent intent = null;
intent = new Intent(getActivity(), Inserisci_.class);
startActivity(intent);
return true;
case R.id.action_settings:
return false;
default:
return super.onOptionsItemSelected(item);
}
}
You can try overriding onKeyDown method in your activity and returning true if the menu key was pressed - this will effectively prevent android from internally handling the menu key:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_MENU) {
return true;
}
else {
return false;
}
}

Detect keyup when PopupMenu is visible

I want to be able to select an option from a PopupMenu using keys (i.e. the numbers 1-4). The device I'm writing an app for has a built in numeric keypad, making them always accessible (not using soft keyboard).
The aim is for the user to be able to press a number/button and the menu will be shown, which is working, here (this is in my activity):
#Override
public boolean onKeyUp(int keyCode, KeyEvent event){
Log.d("onKeyUp", KeyEvent.keyCodeToString(keyCode));
if (event.getKeyCode() == KeyEvent.KEYCODE_5) {
showPopupMenu((Button) findViewById(R.id.btnMenu));
}
return true;
}
I would like to be able to then select one of the options from the menu using the key pad, however when the menu is visible, the onKeyUp event isn't fired whenever a key is pressed.
Here's the menu code:
public void showPopupMenu(View v){
PopupMenu popupMenu = new PopupMenu(this, v);
final MenuInflater menuInflator = popupMenu.getMenuInflater();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
//Do something
return true;
}
});
menuInflator.inflate(R.menu.current_screen_menu, popupMenu.getMenu());
popupMenu.show();
}
This is using Android 4.1 (API 16). Thanks for any help! Happy to provide more code where needed.
You need to set keyevent listener in popupwindow object like this
public void showPopupMenu(View v){
PopupMenu popupMenu = new PopupMenu(this, v);
final MenuInflater menuInflator = popupMenu.getMenuInflater();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
//Do something
return true;
}
});
menuInflator.inflate(R.menu.current_screen_menu, popupMenu.getMenu());
popupMenu.show();
// You have to implement following listner
popupMenu.getContentView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU &&
event.getRepeatCount() == 0 &&
event.getAction() == KeyEvent.ACTION_DOWN) {
// ... payload action here. e.g. popupMenu.dismiss();
return true;
}
return false;
}
});
}

android openPanel menu is empty

I have a tabActivity with multiple activities in one tab.
The following code work on android 2.3 but it's not working on android 4.2
ActivityStack.java
public class ActivityStack extends ActivityGroup {
..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// // what is the current activity?
menu.add(0, 0, 0, "holder");
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// start a new
menu.clear();
// add some menu options
getLocalActivityManager().getCurrentActivity().onPrepareOptionsMenu(menu);
return super.onPrepareOptionsMenu(menu);
}
..
Activity1Tab1.java
here I have a button from where I am calling Activity2Tab1.java onClickListener
Intent acIntent = new Intent();
acIntent.setClass(getParent(),
Activity2Tab1.class);
ActivityStack activityStack = (ActivityStack) getParent();
activityStack.push("SecondActivity", acIntent);
Activity2Tab1.java
..
here I have multiple layouts...defined
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{ //add menus or inflate here
Log.d(TAG, "onPrepareOptionMenu");
if (!isMainMenuVisible) {
pushMainMenuUp();
} else {
pushMainMenuDown();
}
return true;
}
Need some help!!!
Neither in Activity1Tab1 or in Activity2Tab1 the override method onKeyUp() IS NEVER CALLED. The only called methods are from StackActivity. WHY?
After some research done and some thinking I manage to make this code work.
Instead of using onPrepareOptionMenu(menu) and onCreateOptionMenu(menu) I've override the following method in StackActivity:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.d(TAG, keyCode+"");
getLocalActivityManager().getCurrentActivity().onKeyUp(keyCode, event);
return super.onKeyUp(keyCode, event);
}
and in Activity1Tab1 and Activity2Tab1 I had the method:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.d(TAG, ""+event.getAction());
if (keyCode == KeyEvent.KEYCODE_MENU) {
Log.d(TAG, "MENU_BUTTON_PRESSED");
if (!isMainMenuVisible) {
pushMainMenuUp();
} else {
pushMainMenuDown();
}
}
return super.onKeyUp(keyCode, event);
}

Categories

Resources