From Command to Android - java

I'm used to working in J2ME. I wanted to do something on Android, but I just can't get how to replace the command class in the events Android scenario.

Do you want to add commands and want them appear in a built-in menu? When user selects one of them you want a predefined function to be called? If I understand your question right and your problem is the same as above here is the solution:
Android provides a built-in menu and let you create your own menu options. Just like in J2ME as you create some menu options(commands in J2ME) they appear in menu automatically.(The menu that appears when the user hits the menu key on the handset)
To add menu to your Activity override onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_ITEM_ID, 0, "First Menu Option");
return true;
}
Then when user selects an item in the menu onMenuItemSelected(int featureId, MenuItem item) callback is invoked. So you can catch your menu option as follows:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case MENU_ITEM_ID:
yourFunction(); //your function for this particular item
return true;
}
return super.onMenuItemSelected(featureId, item);
}
If there were more items(options) in menu, you would catch them in switch by their ids.
hope that helps

Related

Android - Align options menu to the left side and change its default icon

When I included following code in my Activity file, I got the options menu on the right side with three vertical dots:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
//code
}
}
I need to do following things:
Shift the options menu to the left side
Show 3 horizontal lines instead of 3 dots for options menu
In short, I need options menu like PAYTM app.
http://images.indianexpress.com/2016/12/paytm_big_new.jpg
What is the way to shift the options menu to left and change its appearance?
What you need is a Navigation Drawer.
You can find how to implement it here: https://developer.android.com/training/implementing-navigation/nav-drawer.html

Android: onOptionsItemSelected() called in SherlockActivity, but not in SherlockFragmentActivity

I have problems with the onOptionsItemSelected() in SherlockFragmentActivity.
The onCreateOptionsMenu() is called and the option menu is built perfectly, but when I click on an Options item, the onOptionsItemSelected() isn't called.
I tried the same following code in a test Project using SherlockActivity and there it works:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 1, 0, "hi").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(0, 1, 0, "ha").setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_NEVER);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == 1) {
// Do someting!
}
return true;
}
In both cases, the following imports are done:
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
Does anyone know help? Thank you!
I think that this happens because you don't set MenuItemClickListener (you create items in code).
But if you define menu from xml Android will set listener and fire onOptionsItemSelected on click.
Thank you! Now I copied the Project and rebuilt my SherlockFragmentActivity and found the problem. My class already contained public boolean onMenuItemSelected(int featureId, MenuItem item), and this will be called instead of onOptionsItemSelected. I need this because of the back button at the top left site of an actionbar.

Change another MenuItem onClick of one

My problem is that I want to change the icon from a MenuItem from Visible to not being Visible, but this ofcourse needs to go via the onOptionsItemSelected. If I call on menu, it gets the MenuItem where is clicked on, while another one needs to be hidden. And I also checked on defining the MenuItem and findViewById, which didn't work because it's no view.
Let me show you a part of my code to make it more clear:
#Override
public boolean onOptionsItemSelected(MenuItem menu) {
switch (menu.getItemId()) {
case R.id.menu_refresh:
// Stuff
case R.id.menu_settings:
(Somehow point to R.id.menu_refresh).setVisible(false);
}
return super.onOptionsItemSelected(menu);
}
Any ideas?
What I read from the docs, there is a special method for this case: onPrepareOptionsMenu(Menu menu).
Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
So that makes it more like this:
#Override
public boolean onOptionsItemSelected(MenuItem menu) {
switch (menu.getItemId()) {
case R.id.menu_refresh:
// Stuff
case R.id.menu_settings:
showRefresh = false;
//Stuff
}
return super.onOptionsItemSelected(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.menu_refresh).setVisible(showRefresh);
return super.onPrepareOptionsMenu(menu);
}
If you're doing a 2.3 style menu, you don't change it in onOptionsItemSelected. You do it in onPrepareOptionsMenu the next time the menu is launched (by saving whatever data is appropriate to save).
If you have an actionbar, you'll need to call invalidateOptionsMenu on the activity to reload the menu, and do the disabling in the create function.

Android 4.2 setOnMenuItemClickListener is not called

I have an issue with the action bar on Android 4.2 with ActionBarSherlock. I have added an action bar menu item and it uses setOnMenuItemClickListener to set the buttons action.
MenuItem mapButton = menu.findItem(R.id.show_map);
mapButton.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
//Button implementation goes here
}
});
This works fine on Android 2.2 up to Android 4.1 however on Android 4.2 onMenuItemClick does not get called at all.
I have tried adding this
//Handles the map and route button in the options bar
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.show_map:
return false;
case R.id.show_route:
return false;
default:
return super.onOptionsItemSelected(item);
}
}
Which does get called on button press but appears to have no effect. I could move the code the button implements into this function but it would require me to make drastic alterations to other parts of the program. Is there some way to get setOnMenuItemClickListener working on Android 4.2?

Android option menu

In my project i have activity with options menu. I override onOptionsItemSelected method and add my handlers to menu items (switch-case block). But in one handler i need access to another menu item, how i can do that? findViewById doesn't work
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.visit:
//how to access another MenuItem from here?
return true;
}
...
When creating your menu items, you could put the MenuItem you want to check in an attribute (i.e. one of the private fields of your class). This way, when you go into your method, you'll be able toacces the other menu item.

Categories

Resources