Actionbarsherlock: Menu buttons work in Froyo but not ICS? - android

I have made an app OB Nyt that works well on Froyo and below - but on ICS and above nothing happens when I click the menu buttons (the link to update, the link to images activity and the link to search activity). When I click a button in ICS the buttons on my phone light - but the activities don't open as they do on Froyo. I have modified ActionBarSherlock very little. I know from a toast message that the click is registered in ICS. But the activity does not start. In the LogCat I get a
window already focused ignoring focus gain of com.android.internal.view.iinputmethodclient
every time I click one of the buttons. I have a guess that maybe it's around here that the problem might be:
public boolean onCreateOptionsMenu(Menu menu) {
//Used to put dark icons on light action bar
boolean isLight = true;
menu.add("Save")
.setIcon(isLight ? R.drawable.ic_stilling1 : R.drawable.ic_stilling1)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Search")
.setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.ic_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Opdatér")
.setIcon(isLight ? R.drawable.ic_refresh_inverse : R.drawable.ic_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
//This uses the imported MenuItem from ActionBarSherlock
Toast.makeText(this, "Got click: " + item.toString(), Toast.LENGTH_SHORT).show();
if (item.toString()=="Save"){
startActivity (new Intent(getApplicationContext(), F3Activity.class));
return true;
}
if (item.toString()=="Search"){
startActivity (new Intent(getApplicationContext(), SearchActivity.class));
return true;
}
if (item.toString()=="Opdatér"){
startActivity (new Intent(getApplicationContext(), ABSTabsViewPagerActivity.class));
return true;
}
return true;
}
As you can see I'm a noob in programming :-) Does anyone have a clue on how I can get the buttons to react in ICS? I've tested on Jelly Bean on my own old HTC Desire and ICS on my friends' Samsung Galaxy II and III with the exact same result.
[EDIT] : This made it work:
if (item.getTitle()=="Save")
instead of
if (item.toString()=="Save")
A beginner's mistake, I knew it ;-)

Use String.equals(String) for string comparisons. The right approach would be
if ("Save".equals(item.getTitle()))
Consider giving your menu items an ID, and using that for figuring out which is clicked instead.

This made it work:
if (item.getTitle()=="Save")
instead of
if (item.toString()=="Save")

Related

Names in action bar not changing?

A few days ago, I decided to make an Android app with a Navigation Drawer, in Eclipse (NOT Android Studio) using Material Design. It came preloaded with 3 default items in the Navigation Drawer. Since I needed more, I added some in strings.xml and added them in the array in NavigationDrawerFrament.java, and they started appearing in the Navigation Drawer.
The problem is, the names in the Action Bar didn't change. Let's assume that they came preloaded as 'thingOne', 'thingTwo' and 'thingThree', and I added 'thingFour' and 'thingFive'. If I click on thingOne, the text in the Action Bar changes to 'thingOne'. Same with thingTwo and thingThree. But if I clicked on thingFour after thingOne, then the text in the Action Bar remains as thingOne.
I need to change the text in the Action Bar. Please help soon.
Edit:
The code in that is executed on item select is this:
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
if (item.getItemId() == R.id.action_example) {
Toast.makeText(getActivity(), "Coming soon.", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
Note: R.id.action_example refers to a search function I am currently working on; hence, the 'Coming soon.'
By "Action bar name", you mean Activity name displayed in the Action Bar ? If so you might want to check out :
getActionBar().setTitle("Title");
Or if you're targeting API <= 10 :
getSupportActionBar().setTitle("Title");

add action / item to custom titlebar in android

I have been trying for 2 days to add actions to the title/action bar in my android app. I have started the app to learn android and familiarize myself
I have read a couple of question on stackoverflow and some other tutorials on google to try and find a answer, I couldn't however find one for a custom title bar but rather a title bar in general.
What I have tried is adding the following in my activity.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//boolean result = super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case ADD_NEW_FRIEND_ID: {
Intent i = new Intent(FriendList.this, AddFriend.class);
startActivity(i);
But nothing happens. Any suggestions on where to go from here or what to do?
Since there is no onMenuItemSelected() in Android, perhaps try onOptionsItemSelected(), to line up with your onCreateOptionsMenu().
Also, since you are trying to "learn android and familiarize myself", I would recommend getting rid of the "class that was created to style the titlebar/actionbar to be dynamic per user that's logged in to the app". While that may be a nice feature, it will add unnecessary complexity to your app and may interfere with your learning experience. Focus on learning Android first, then fret about customizable title bars later.

How to add actions to Overflow menu on Galaxy S3?

I'm facing a problem when actions intended to be shown in overflow menu in action bar are not there on Galaxy S3. As a consequence the UX is somewhat confusing - my action bar on Galaxy S3 is there to only display app logo and name but offering no extra functionality.
I'd like to have an identical UX on all devices running on Android 4.x with actions in the overflow menu. Is this possible without using third-party components such as ActionBarSherlock?
Thanls
This is a decision made by some manufacturers that requires some "bad" solutions if you really want to do this. The overflow menu is just the "regular" old menu button that all android devices used to have. When the menu button got removed by Google in Honeycomb and ICS some manufacturers decided to keep the menu button. This has lead to great confusion about what the menu button is and does.
You should keep in mind though that the user using a S3 would expect to have a functional menu button as they would not be used to seeing a 3-dot menu. All apps using the built in menu system should appear in a way to the user that they expect. Therefor I would strongly recommend against the urge to have your app look exactly the same on all devices in this matter since it would most likely confuse users more then help them. It should be possible, to both implement the "proper" menu system and a "custom/fake" 3-dot menu if you wish however.
This post seems to have some good guidelines:
https://stackoverflow.com/a/10713860/1068167
There is a quick and dirty way to fake the absence of a hardware menu button using reflection to set a field in your app's ViewConfiguration instance.
The following code snip can be added to your activity and called during onCreate().
private void enableActionBarOverflow() {
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) {
e.printStackTrace();
}
}
Not a clean solution as the implementation of ViewConfiguration could change at some point in the future, and since the sHasPermanentMenuKey field is private, there's no guarantee that the field will always be there.
However, I would only use this as a last resort if you absolutely must have an overflow menu on devices that have a menu key.
Assuming you're minimum API is 11 (Honeycomb) or greater, a better solution would be to make your own overflow menu like so:
Add a menu item for the overflow in your menu.xml, setting it to always show and inflate in your onCreateOptionsMenu()
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
...
<item
android:id="#+id/action_overflow"
android:icon="#drawable/ic_action_settings"
android:title="#string/settings"
android:showAsAction="always">
</item>
</menu>
,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
Create a separate overflow_menu.xml resource for your choices you want in the overflow menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/overflow_action1"
android:title="#string/overflow_action1">
</item>
<item
android:id="#+id/overflow_action2"
android:title="#string/overflow_action2">
</item>
</menu>
In your onOptionsItemSelected() method, handle the selection of your overflow menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
...
case R.id.action_overflow:
PopupMenu popup = new PopupMenu(
this, findViewById(R.id.action_overflow));
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.overflow_menu, popup.getMenu());
popup.setOnMenuItemClickListener(this);
popup.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Implement the PopupMenu.OnMenuItemClickListener interface in your activity to handle the clicks of the overflow items
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.overflow_action1:
//do stuff
return true;
case R.id.overflow_action2:
//do stuff
return true;
default:
return false;
}
}

Overflow button forces Action Mode to finish

I have an EditText and I want the user to be able to select some text and apply some basic formatting to the selected text (bold, italic, etc). I still want the standard copy, cut, paste options to show, though. I read somewhere in the Android documentation that to do this, you should call setCustomSelectionActionModeCallback() on the EditText and pass it an ActionModeCallback(), so that's what I did. Here's my code:
In my activity's onCreate() method:
myEditText.setCustomSelectionActionModeCallback(new TextSelectionActionMode());
Callback declaration:
private class TextSelectionActionMode implements ActionMode.Callback {
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.add("Bold");
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
}
The problem I'm having is that when I click on the overflow button (to access my "Bold" menu item), the ActionMode gets closed immediately. If I set it to always show as an action, using this:
MenuItem bold = menu.add("Bold");
bold.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
It works fine and I can click on it (though it obviously does nothing). What am I missing here?
Edit: Just wanted to add that I run into the exact same problem if I actually inflate a menu instead of adding menu items programmatically. Once again, though, the problem goes away if I force it to always show as an action.
It's frameworks issue. If textview receive 'focus changed' event, then textview stop the action mode. When overflow popup is shown, textview miss focus.
This issue has been solved in Android 6.0. However you should use ActionMode.Callback2 as described here in Android 6.0.
For Android 5.x and below, I recommend this workaround: add a button to Toolbar or ActionBar which records the current selection and then open another context menu.
this.inputText_selectionStart = inputText.getSelectionStart();
this.inputText_selectionEnd = inputText.getSelectionEnd();
registerForContextMenu(inputText);
openContextMenu(inputText);
unregisterForContextMenu(inputText);
It is a filed Android bug: https://code.google.com/p/android/issues/detail?id=82640.
That link contains a workaround. Fortunately this has been fixed in Android 6.0.

Android Option Menu not working properly; what am I missing?

I've been away from Java for some time --functional programming has been my muse-- and recently decided to jump back in with an android application. Things are going well. Javas syntax is mostly back in my brain, OO design principles are a little rusty, but I'm not afraid of re-factoring.
One problem I hit has been with the option menus in the platform. I load them from an XML file through a menu-inflator in my main activity (below), and I can see them! But, when I press them things get weird --but not like seeing your grandmother make-out with your best friend, much less weird.
For some reason, when I press the first button, I get the friendly default message in the code sample below, "That's not an option, moron!". And when I press the second, the message is "Adding One". I'm off by one somehow! But, but how!? but why!?
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/add_single_id"
android:title="#string/add_one" />
<item android:id="#+id/add_multi_id"
android:title="#string/add_multi" />
</menu>
... which is loaded by the menu inflator...
public boolean onCreateOptionsMenu( Menu menu ){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu);
return true;
}
... and finally the listener for items selected.
public boolean onOptionsItemSelected( MenuItem item ){
switch( item.getItemId() ){
case R.id.add_single_id:
Toast.makeText(this, "Adding One", Toast.LENGTH_LONG).show();
add_single();
break;
case R.id.add_multi_id:
Toast.makeText(this, "Adding n", Toast.LENGTH_LONG).show();
Intent i = new Intent(this, SelectMulti.class);
startActivityForResult(i, ACTIVITY_LOADMULTI);
break;
default:
Toast.makeText(this, "That's not an option, moron!", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
This happen to me many times when I am developing android on eclipse, and clean and rebuilding a project fixes it as it will recreate android Resource file and correctly map to your UI id's.
In your XML you have ID = add_one_id but in the code you use R.id.add_single_id

Categories

Resources