Action bar for Android 2.1 - android

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

Related

Showcaseview not displaying action bar items

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.

menu button doesn't show on nexus 7

so I am facing this problem for long time. I've got Nexus 4 and Nexus 7 both running Android 4.3, and i've got application with targetSdkVersion="11"("I use 11 because any target sdk below 11 doesn't support multitouch for me). And the problem is that 3-dot menu shows on Nexus 4 but doesnt show on Nexus 7. 3 dot menu button on nexus 7 works only if I put targetSdkVersion="8" but then multitouch doesnt work
Nexus 4:
Nexus 7 :
code :
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="11" />
screenshots :
nexus 7
nexus 4:
In case you are specifically wondering why the button is not being shown the following rules apply when Android determines if a legacy menu button is needed:
If target API version is less than 11 it is shown on all devices
If target version is 11, 12, or 13 (i.e. tablet-only Honeycomb) Android assumes that your app has been designed for tablets and won't show a legacy button on tablets, but will on phones
If target is 14 or above (ICS and above), Android assumes your app is designed for tablets and phones and so the legacy button isn't shown.
But like the other answers say, you shouldn't be using this menu button. If you don't want an entire ActionBar, another option would to have a three-dot button in your activity which shows a menu using PopupMenu.
You should not be using that menu anymore. From the Menus documentation:
On Android 3.0 and higher, items from the options menu are presented by the action bar as a combination of on-screen action items and overflow options. Beginning with Android 3.0, the Menu button is deprecated (some devices don't have one), so you should migrate toward using the action bar to provide access to actions and other options.
Use an ActionBar.
The correct solution is to use an ActionBar but there may be some hacks to get the overflow menu to appear.
Specifically, there's a hidden window flag FLAG_NEEDS_MENU_KEY you can access via reflection. Here's a code snippet (from this blog):
public static void addLegacyOverflowButton(Window window) {
if (window.peekDecorView() == null) {
throw new RuntimeException("Must call addLegacyOverflowButton() after setContentView()");
}
try {
window.addFlags(WindowManager.LayoutParams.class.getField("FLAG_NEEDS_MENU_KEY").getInt(null));
}
catch (NoSuchFieldException e) {
// Ignore since this field won't exist in most versions of Android
}
catch (IllegalAccessException e) {
Log.w(TAG, "Could not access FLAG_NEEDS_MENU_KEY in addLegacyOverflowButton()", e);
}
}
I tested this on a couple of Nexus devices and it works. Comments on the blog state that there are devices where it doesn't work. Use with caution. And use an ActionBar, really.
There's a simple way to force a menu option to stay in the menu overflow. If you're creating a menu with XML, you can force this using the "showAsAction" attribute.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_option"
android:showAsAction="never"
android:title="#string/option_name" />
</menu>
If you set "showAsAction" to "never", it will be forced to don't show on the ActionBar, so the option will remain on the menu overflow.
There's more info (like how to vinculate the XML menu file to an Activity) on the official Android documentation webpage: http://developer.android.com/guide/topics/resources/menu-resource.html
I wish this can be helpful!
I wouldnt always recommend using this, since its a hack which breaks the consistency of the phone, but if you want the "3 dots" menu, which is called the overflow menu you need to add this method
//Hack to display overflowMenu on devices with a menu button
private void getOverflowMenu() {
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();
}
}
And in your onCreate() call this method.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Activity);
getOverflowMenu();
}

Show/Hide ProgresBar in ActionBar on android API lower than 11

I want to show/hide ProgressBar in ActionBar on all android devices.
I am using android support library (android-support-v7-appcompat).
My activity extends ActionBarActivity and in onCreate it requests window feature supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); (before setting content).
On my button click I show/hide ProgressBar.
public void onClick(View v) {
if(v.getId() == R.id.buttonProgress) {
if(progress) {
setProgressBarIndeterminateVisibility(false);
progress = false;
} else {
setProgressBarIndeterminateVisibility(true);
progress = true;
}
}
}
This code works fine on android API higher than 11. But I have proglem with API lower than 11. The ProgressBar is not showing up. There is no error in LogCat.
I have noticed that when I show ProgressBar in onCreate it works. I also can hide it from onCreate.
Do you have solution for this problem?
Thank you!
call
setSupportProgressBarIndeterminateVisibility(true)
if call it from fragment cast the activity for example:
ActionBarActivity ac =(ActionBarActivity) getActivity();
ac.setSupportProgressBarIndeterminateVisibility(true);
There is no action bar for apps lower than API 11.. If you use ProgressBar for Less than API 11 (Honeycomb) it does show up but it will tiny circle revolving on the top right of the title bar (thin bar on top of the app). Well, that depends on the theme.
If you want an actionbar you may want to look into an external library : ActionBarSherlock
The ActionBar APIs were first added in Android 3.0 (API level 11) but they are also available in the Support Library for compatibility with Android 2.1 (API level 7) and above.
And ActionBar is added in support liabrary to allow implementation of the action bar user interface design pattern back to Android 2.1 (API level 7) and higher. Use of this class requires that you implement your activity by extending the new ActionBarActivity class.
Have a look at the official document here
However you can achieve this by using ActionBarSherlock library.
Check out this
Site for ABS Here you can get the sample programs ABS Library

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.

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

Categories

Resources