I use ShowcaseView like this:
ActionItemTarget target = new ActionItemTarget(this, R.id.menu_search);
ShowcaseView sv = new ShowcaseView.Builder(this, true)
.setTarget(target)
.setContentText("Press this to search")
.setStyle(R.style.CustomShowcaseTheme)
.build();
But when I start app the first, ShowcaseView highlights home button. When I try to start app again, correct ActionBar item is showcased.
My app doesn't use compatibility libraries like ActionbarSherlock.
Any idea why this may be happening?
As requested:
The problem is that you are trying to retrieve an id for a view that is not in the hierarchy yet. According to the activity view cycle the menu is only created after onCreate. The best solution is to use onCreateOptionsMenu but even here R.id.menu_search will not return a valid id because the menu hasn't been created yet. Since the order of events has changed in API lvl 16 I suggest you hack it a little bit. You can create a handler on your onCreate() and execute a postDelayed runable. The code will be executed after the menu has been designed. On that runable you can retrieve the view for R.id.menu_search and highlight it.
I will try to present sample code here:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
new ShowcaseView.Builder(this)
.withMaterialShowcase()
.setTarget(new ToolbarActionItemTarget(this.toolbar, R.id.menu_search))
.setContentText("Here's how to highlight items on a toolbar")
.build()
.show();
return true;
}
Note: toolbar declare as member of the class.
Related
I search on google and stackoverflow this topic.I ve found a few way to implement.
one of these is actionbarsherlock , but actually I do not understand how can implement this to my project. Is there any simple way? I mean a few classes or just add a library I do not know but I have a huge project and I want to implement this .Could you show me how can do it easily?
thanks
If you want to use ActionbarCompat library.
1) Import the ActionbarCompat library project into your workspace first and add the library to your project
https://developer.android.com/tools/support-library/setup.html#libs-with-res
2) Extend your Activity Class with ActionBarActivity
3) set your theme in manifest as
android:theme="#style/Theme.AppCompat"
Please check this link.
You can use android support library for this. No need of any other library.
Example also there in side link.
If You want to use ActionBar that supports devices with lower api..
you can do two things ...
1)Use the support Library(ActionbarCompat)
2)Use ActionBarSherlock
I use ActionBarsherlock
Steps to Use
1)YOURACTIVITY extends SherlockActivity
2) Use onCreateOptionsMenu to get the menu
`
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
SubMenu subMenu1 = menu.addSubMenu("");
subMenu1.add(0,2,Menu.NONE,"Rate Us").setIcon(R.drawable.ic_action_good);
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(R.drawable.ic_action_overflow);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}
3) Use onOptionsItemSelected to get the item selected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 2:
//rate app
break;
return super.onOptionsItemSelected(item);
}
4)Finally in your AndroidManifest File, add this under your activity
android:theme="#style/Theme.Sherlock"
`
5) and you are Done ...:)
For setup support library see-
https://developer.android.com/tools/support-library/setup.html
And for implementing action bar using support library see this-
http://antonioleiva.com/actionbarcompat-how-to-use/
So in my project I am using a showcaseview to create a tutorial for the user. When I point the ShowcaseView to any action bar id, the result is this:
My question is is it possible to have the showcaseview target the specific items in the action bar rather than just a general overview like the one shown above. The code is being run in the overridden onCreateOptionsMenu method and is in the context of an activity. Here is the code that I am currently using:
actionBarViews = new ShowcaseViews(activity);
ShowcaseView.ConfigOptions co = new ShowcaseView.ConfigOptions();
co.hideOnClickOutside = true;
actionBarViews.addView(new ItemViewProperties(R.id.sendmessage,
R.string.send_title, R.string.send_tutorial,
ShowcaseView.ITEM_ACTION_ITEM, 0, co));
actionBarViews.addView(new ItemViewProperties(R.id.tutorial_menu,
R.string.tutorial_title, R.string.tutorial_tutorial,
ShowcaseView.ITEM_ACTION_OVERFLOW, 0, co));
actionBarViews.show();
If it is impossible to currently target actionbaritems, is there another way to go about implementing a tutorial.
This may be a little late, but this may be useful for other users who find this thread. I used the following code to highlight my action button with ShowCaseView
//for action button with id "ACTION_BUTTON_ID"
ShowcaseView.ConfigOptions co = new ShowcaseView.ConfigOptions();
co.showcaseId = ShowcaseView.ITEM_ACTION_ITEM;
co.hideOnClickOutside = true;
ActionItemTarget target = new ActionItemTarget(this.getActivity(),R.id.ACTION_BUTTON_ID);
final ShowcaseView sv = ShowcaseView.insertShowcaseView(target,getActivity(),R.string.instruction_title_text,R.string.instruction_details_text);
sv.show();
use it like this
new ShowcaseView.Builder(activity)
// .withMaterialShowcase()
// .setStyle(R.style.CustomShowcaseTheme3)
.setTarget(Target.NONE)
.setOnClickListener(this)
//.withMaterialShowcase()
.blockAllTouches()
.useDecorViewAsParent() //this is the difference
.build();
then target your view in action bar
have you tried to use ActionBarSherlock? It supports old compatibility and has quite a lot of available examples.
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.
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
In my onCreateOptionsMenu() I have basically the following:
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, MENU_ITEM_INSERT, Menu.NONE, R.string.item_menu_insert).setShortcut('3',
'a').setIcon(android.R.drawable.ic_menu_add);
PackageManager pm = getPackageManager();
if(pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) && pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS)){
menu.add(Menu.NONE, MENU_ITEM_SCAN_ADD, Menu.NONE, ((Collectionista.DEBUG)?"DEBUG Scan and add item":getString(R.string.item_menu_scan_add))).setShortcut('4',
'a').setIcon(android.R.drawable.ic_menu_add);
}
...
}
And in onPrepareOptionsMenu among others the following:
final boolean scanAvailable = ScanIntent.isInstalled(this);
final MusicCDItemScanAddTask task = new MusicCDItemScanAddTask(this);
menu.findItem(MENU_ITEM_SCAN_ADD).setEnabled(scanAvailable && (tasks == null || !existsTask(task)));
As you see, two options items have the same drawable set (android.R.drawable.ic_menu_add). Now, if in onPrepareOptionsMenu the second menu item gets disabled, its label and icon become gray, but also the icon of the first menu item becomes gray, while the label of that first menu item stays black and it remains clickable. What is causing this crosstalk between the two icons/drawables? Shouldn't the system handle things like mutate() in this case?
I've included a screenshot:
http://www.curious-creature.org/2009/05/02/drawable-mutations/
The above article by Romain Guy explains this very situation and provides a work around.
Yes, this looks odd. I can not explain why this is as it is, however I can propose a workaround - instead of using internal drawable resourse, you could put the same image in your app drawable resourse dir AND you could duplicate the add image, so you have 2 images - add_for_menu_item_1.png and add_for_menu_item_2.png named differently, but having the same visual representation. I am sure this would do the trick.
Could it be that both menu items are sharing the same alphaChar which is causing the second menuItem to be disabled?