Names in action bar not changing? - android

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");

Related

Using Sherlock Action Bar but default actionbar still exists

I've installed successfully Sherlock's Action Bar and the 2 items i've added are placed well!
But it appears that Sherlock has not replaced the default android action bar, and i always get an android image on the left :/
How can i remove it? Or replace it?
I want to place on the left my app's name - with some action - but i can't and don't know how, if Sherlock does not work well.
Please help! :(
Here is how you can set an icon and a title, and listen for the item click:
// in onResume
ActionBar bar = getSupportActionBar();
bar.setTitle( "My App Name" );
bar.setHomeButtonEnabled( true );
bar.setIcon( R.drawable.your_icon );
// later
#Override
public boolean onMenuItemSelected( int featureId, MenuItem item )
{
if( android.R.id.home == item.getItemId() )
{
// do your stuff here
return true;
}
return super.onMenuItemSelected( featureId, item );
}
You can change the icon with actionbar.setIcon() or you can remove it completely by using actionbar.setDisplayShowHomeEnabled(false).
Actionbarsherlock is only a wrapper. On Android versions where the native Actionbar is available, you will see the native one, on lower android versions you will see the actionbar provided by ABS.

Actionbarsherlock: Menu buttons work in Froyo but not ICS?

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")

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;
}
}

Show soft keyboard on search actionbar activation with SearchView

I see this question sets focus on the SearchView EditText when I activate a search from the ActionBar. However the keyboard does not come up when it gains focus. Shouldn't it, as it is just a normal EditText? (Is it a normal EditText?) This behaviour is seen on Android SDK level 11. (Samsung Galax Tab 7.7 with stock Android.)
I have a workaround at the moment that hooks in on the onOptionsItemSelected(MenuItem item) method of my Activity, showing the keyboard.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean menuSelectionHandeled = super.onOptionsItemSelected(item);
// menu_search is the id of the menu item in the ActionBar
if (item.getItemId() == R.id.menu_search) {
mInputManager.showSoftInput(null, InputMethodManager.SHOW_IMPLICIT);
}
return menuSelectionHandeled;
}
Where mInputManager is an instance of InputMethodManager.
The ActionBar is built with ActionBarSherlock, and since the target device is Android 3.x could this be the cause of the symptoms? As per ActionBarSherlock's FAQ :
The action bar on Android 3.x (also known as Honeycomb) does not
implement all of the features of the one in Android 4.x (Ice Cream
Sandwich). In order to provide a full action bar API on all platforms
as well as unify the styling across all versions of Android the custom
implementation is used.
This should work :
SearchView searchView = new SearchView(getContext());
searchView.setInputType(InputType.TYPE_CLASS_TEXT);
searchView.setBackgroundColor(Color.WHITE);

Android compatibility contextual action bar

In trying to follow the Android Design Guidelines, I'm running into a small quandary.
I want to have a list of items that I can long-press several of (multi-select), and then perform bulk actions on them.
The Design Guidelines suggest using the Contextual Action Bar for this, and it sounds perfectly like what I had in mind. Problem is, I'm trying to maintain compatibility backwards to API 7 (due to my phone being 2.3.3 currently).
I'm using ActionBarSherlock to get other actionbar stuff, but I can't seem to figure out how to get it to either fire up a contextual action bar, nor have I figured out how to add buttons arbitrarily to the ActionBar in ABS. I see you can do tabs, so maybe that's the answer, but since I'm trying to allow multi-select, I don't want to have the normal modal context menu.
This is a late answer, but I think would help people stuck.
Opening the contextual action bar is actually pretty simple, at any point in your activity you just have to call:
startActionMode(mActionModeCallback);
If you are not in your main activity, like in fragments, you can get a reference with
getSherlockActivity().startActionMode(mActionModeCallback);
and this is the callback
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback(){
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.actionbar_context_menu, menu);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item1:
return true;
case R.id.menu_item2:
//close the action mode
//mode.finish();
return true;
default:
mode.finish();
return false;
}
}
};
The xml is a simple menu like the actionbar one:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_item1"
android:icon="#drawable/ic_item1"
android:title="#string/ITEM1"
android:showAsAction="always|withText" />
<item android:id="#+id/menu_item2"
android:icon="#drawable/ic_item2"
android:title="#string/ITEM2"
android:showAsAction="always|withText" />
Setting up contextual actionbar is the same to setting up the 'regular' ActionBar items as far as the XML is concerned. This example in the developer's guide explains it all.
In order to use ActionBarSherlock, replace the default Android-callbacks to the ActionBarSherlock-edited callbacks (e.g. instead of Android.View.ActionMode, use com.actionbarsherlock.view.ActionMode).
ActionBarSherlock has its own implementation of ActionMode, but you'll have to manualy controll its lifesycle, I wrote a tutorial about this.
For long click sample please refer to below links. First one is java code required for sample. And second one is how to define the layout;
Java source
Layout xml
I will answer second part of your question. Here is an example how to add any View instance (button in the code below) actionbar with ActionBarSherlock library:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
refreshButton = (RotatingButton) LayoutInflater.from(this).inflate(R.layout.actionbar_customview_refresh, null);
refreshButton.setOnClickListener(refreshButtonListener);
MenuItem item = menu.add(0, android.R.id.copy, 0, getString(R.string.actionbar_refresh));
item.setActionView(refreshButton);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_action_bar, menu);
}
I was facing the same issue. It was solved when I found this link. Basically, you have to create a callback class that implements ActionMode.Callback. In this class, you inflate the Action Bar with your contextual Action Bar. At each selection (or long click), you start the callback using the startActionMode method. See the link for an working code =]
EDIT: There is also an example on Sherlock's samples under /samples/demos/src/com/actionbarsherlock/sample/demos/ActionModes.java

Categories

Resources