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.
Related
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");
Does any one know how to display title text(in the nav bar) above TabGroup in Android in Titanium?
I know I can set title to display at each tab, but how about the one on top of the tab group?
It only has an application icon at left and rather strange there if it's android. I tried to set navBarHidden : true so that nav bar won't show, but the icon inside tab also goes hidden...
Please kindly advise me here...thanks!
You can set title/icon and handle icon click events of the action bar with the following code
// Set activity title and back functionality
if( Ti.Platform.Android.API_LEVEL >= 11 ) {
var activity = tabGroup.getActivity();
activity.actionBar.icon = "/images/icon.png";
activity.actionBar.title = "Title";
activity.actionBar.onHomeIconItemSelected = function() {
doSomething();
};
}
This example assumes that your tab group is called tabGroup. You could also do this with a window activity var activity = win.getActivity();
You can find more on this topic in the Titanium docs.
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
The new Android design docs mention use of an Up caret: http://developer.android.com/design/patterns/actionbar.html
The up caret seems to be proper for navigation throughout Android - is there a proper way to add this caret? It seems unintuitive that I should just modify the icon image to include it.
Any help would be much appreciated!
You can also add the caret by setting setDisplayHomeAsUpEnabled(true) on the ActionBar. Be sure to react to the menu item when selected.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch( item.getItemId() ) {
case android.R.id.home:
Log.d(TAG, "Home Icon Item Selected");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I'm not exactly 100% sure what you're asking, but if you're asking where you can find the images for the "caret", they're in the platform/data/res/drawable-hdpi folder (in your SDK location). The files are called ic_ab_back_holo_[dark|light].png. You can see it in the github repo too. The standard procedure of copying to your local resources folder applies here, since you can't reference them (they're not exposed).
Is there action bar for Android Level 7 or something else that I can use as action bar?
I need to build an app which is using action bar for Android 2.1
JohanNilsson has actually created a ActionBar library which is available on GitHub.
Direct link.
here you go. The officle android article is called: ActionBarCompat - Action Bar Compatibility. (just in case the link wont work for ever)
Well, you should try this open source project http://actionbarsherlock.com , you will find it's full definition on this website
In the newest version it will have support for features provided by ICS.
TypedArray a = mActivity.getTheme().obtainStyledAttributes(R.style.AppTheme, new int[] {
R.attr.actionbarCompatItemHomeStyle,
R.attr.actionbarCompatItemStyle
});
int actionbarCompatItemHomeStyle = a.getResourceId(0, 0);
int actionbarCompatItemStyle = a.getResourceId(1, 0);
ImageButton actionButton = new ImageButton(mActivity, null,
itemId == android.R.id.home ? actionbarCompatItemHomeStyle : actionbarCompatItemStyle
);
The equivalent of action bar in pre-Honeycomb versions of Android is the options menu, accessible under Menu button.
In July 2013 Google added action bar compatibility back to 2.1 (7+) in New "v7 appcompat library"
http://developer.android.com/tools/support-library/index.html