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/
Related
I am implementing "Read Aloud" or "Talkback" for an app. Everything is working with contentDescription text, but with option menu, I found nothing related to contentDescription, I want system read "Menu "+ item's name.
EX: My menu has 2 items: "Create New Folder" and "Delete current folder", currently, when I focus a menu item (Support trackball and bluetooth key), system can talk exactly the menu's text. But I want it talks more like "1: Menu Create New Folder" and "2: Menu Delete current folder".
So, How can I change the read text? How can I get the focused menu item when bluetooth keyboard press UP/DOWN key?
MenuItemCompat in the v4 support libraries has a
android.support.v4.view.MenuItemCompat.setContentDescription(MenuItem menuItem, CharSequence contentDescription)
method for backwards compatibility on pre-Oreo devices.
For AndroidX see this answer:
https://stackoverflow.com/a/57950952/1236327
As my investigation, in Android internal source code, class ActionMenuItemView.java method setTitle(CharSequence title), the source code also sets setContentDescription(title), so Android will read your MenuItem's text as default. I don't know why the core has so inflexible in this case.
Updated:
Thanks for #sofakingforever answer.
Seem Google just added the setContentDescription(CharSequence contentDescription) method to the MenuItem class on API 26 (Android O).
Updated:
Thanks for new #tim.paetz answer .
Look like all versions are now supported setContentDescription for menu item using android support v4 libraries.
this answer post AndroidX
androidx.core.view.MenuItemCompat.setContentDescription(menuItem, contentDescription)
It seems they just added the setContentDescription(CharSequence contentDescription) method to the MenuItem class on API 26 (Android O)
Full sample:
#Override
public void onCreateOptionsMenu(#NonNull Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.client_menu_close, menu);
super.onCreateOptionsMenu(menu, inflater);
MenuItem closeMenu = menu.findItem(R.id.client_menu_close_action);
androidx.core.view.MenuItemCompat.setContentDescription(closeMenu, R.string.str_accessibility_client_screen_close);
}
Is it possible to have POPUP menu like play store attached to each row of adapter
what i done so far
holder.rl_overflow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(context, holder.rl_overflow);
popup.getMenuInflater().inflate(R.menu.overflow, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(context,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return false;
}
});
popup.show();
}
});
but constructor for POPUP menu says its available from API 11. I go through the developers.android.com and i found, its can be added using SUPPORT V7 library "https://developer.android.com/reference/android/support/v7/widget/PopupMenu.html" but i'm not able to implement this using ABS, please help someone.
Use like this PopupMenu in ActionBarSherlock.
Styling of the PopupMenu -
<item name="popupMenuStyle">#style/PopupMenu.MyAppTheme</item>
<style name="PopupMenu.MyAppTheme" parent="#style/Widget.Sherlock.ListPopupWindow">
<item name="android:popupBackground">#android:color/white</item>
</style>
If you are using the support library you should discard using the ABS. Instead, import the support library into your workspace which can be found in ~/adt-bundle-linux-x86_64-20130729/sdk/extras/android/support/v7/appcompact and use is in your project. And also don't forget to add the support library which can be brought up right clicking into your project and entering Android Tools -> Add Support Library
Using appcompact you will have to extend your activity class with ActionBarActivity. And also using the appcompact you have to make changes in your styles folder. You could refer to this. Do not also forget to update the values-v11 and values-v14 file. Doing all of this will make your application compatible.
P.S. If any error occurs in your appcompact library. Don't panic look at the error logs and open the file that seems to contain the error. Most probably you will have to refresh the file and after that you just fix project properties, and the error goes away.
Hope this helps :)
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.
This question has also been asked by someone on the Android Developers Google Group (link), but it does not have an answer...
I recently removed the v4 support library from my Android project in Eclipse, because my application only targets Android 4.0 (Ice Cream Sandwich) and up. I'm now going through all the support library references that are giving me errors.
One in particular is NavUtils, which seems to be a support library class used in Activity navigation. Example:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
What is the equivalent of NavUtils when not using the support library?
Look at the source code of NavUtils, if necessary just copy it to your project. It just gets the parent activity set in the manifest and starts it.
I found #Nikolay's approach to be the best if you don't want to have the whole android v4 support library in your project.
This is the list of classes you should copy into your project:
NavUtils
NavUtilsJB
IntentCompat
IntentCompatHoneycomb
IntentCompatIcsMr1
Android version from Jelly Beans have the NavUtils incorporated into the Activity class.
There is no need to handle the case : android.R.id.home in onOptionsItemSelected().
OR you can just return false for this case in onOptionsItemSelected().
Here is this simplest answer I could find, and what I did in my code:
Simply replace:
NavUtils.navigateUpFromSameTask(this);
with:
navigateUpTo(getParentActivityIntent());
Hope this helps!
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