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
Related
Previously, I'm using SherlockActionBar library. The following code will work, across Android 2.3 till Android 5.
this.searchMenuItem.collapseActionView();
However, after migrating to AppCompat, we need to migrate to the following code
MenuItemCompat.collapseActionView(JStockFragmentActivity.this.searchMenuItem);
When I look at the documentation http://developer.android.com/guide/topics/ui/actionbar.html#ActionView, it states that
On API level 11 or higher
Get the action view by calling getActionView() on the corresponding
MenuItem:
menu.findItem(R.id.action_search).getActionView()
I was wondering, is it necessary that I need to write my migrated code in the following way?
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
this.searchMenuItem.collapseActionView();
} else {
MenuItemCompat.collapseActionView(this.searchMenuItem);
}
I'm developing an application for APIs from v10 to v17.
My activity implements OnDragListener which is available only from API v14.
public class MyActivity extends Activity implements View.OnDragListener {
....
}
So, when application is installed on device with API v10 it fails to load activity with java.lang.NoClassDefFoundError.
I understand that this won't work on API v10, that's OK. I'm handling this inside the activity.
I just want to know What is the practice to handle different API versions when it comes to features like this?
First of all you can check API levels and depending on that to execute or not your code. For example :
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
// don't use it.
} else {
// use the new API :
// myView.setOnDragListener(...);
}
You should not implement interfaces from new API's in Activity if you want to support old android versions too. Instead of that use the approach above.
In your case you should not declare the activity to implement View.OnDragListener. Just do the logic in another class and instantiate it only when you need it (I suppose you add the listener only for API >= 14).
I´m experiencing a weird problem with SlidingMenu Library and ViewPager when they are running on devices with Android 3.2 (Honeycomb).
The problem appears when we "toggle" the SlidingMenu to show the Menu that is hidden on the left of the app. When we do this, both ContentView and BehingContentView stops responding to touch events.
Thinking that this was a problem related to my application, I downloaded the last version of ABS and SlidingMenu library and configured a new project using the built-in example that comes with the SlidingMenu and, for my surprise, the same behavior occurred with the ViewPager example.
These are the steps that I did:
Configure an Emulator using API Level 13 and 7" WSVGA (Tablet);
Download ABS and SlidingMenu from GIT;
Setup a new Project, using the compatibility library android-support-v41 (Also tested with android-support-v4);
Solved the problem 'getSupportActionBar() is undefined' as described here: https://github.com/jfeinstein10/SlidingMenu/issues/145;
Run the 'Example Application' and choose 'ViewPager' example;
Swipe pages to the right and to the left, without opening the menu;
Open the menu. See that the lists don´t scroll as expected;
Close the menu. See that the viewpager doesn´t responds to touch events anymore;
Notice that this behavior was reported only on Android 3.2 devices. We have the same application running on 2.x and on 4.x devices, without this problem.
Also, noticed that the Example Application that was downloaded from Google Play doesn´t have this problem.
Does anybody have any advice? Thanks a lot!
Edit 1
Tested on a real device, and confirmed the Behavior. Does anybody have an advice?
I had the same problem and fixed it by using the following work-around.
Replace these lines in SlidingMenu.java:
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void manageLayers(float percentOpen) {
if (Build.VERSION.SDK_INT < 11) return;
with:
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void manageLayers(float percentOpen) {
if (Build.VERSION.SDK_INT < 14) return;
I'm working on an app that is targeted for version 2.3 so that it will run on my sister's phone. However, I can run 4.0 on my phone. I want to add some swipe animations and such but I don't run the animations to run on her phone.
Is this even possible?
Yes, simply put the API specific code in a if/else block, so it is only called when the system supports it:
Like this:
if (currentapiVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
//do things that are only supported on JellyBean
} else {
//do the other stuff
}
You can use a ViewPager and FragmentPagerAdapter to provide swipe functionality among various Fragments. All of which are available in the support library.
ViewPager(Note, don't do anything with the ActionBar, as they are not yet in the support library):
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
FragmentPagerAdapter(pretty much the example code you will need):
http://developer.android.com/reference/android/support/v13/app/FragmentPagerAdapter.html
Fragment:
http://developer.android.com/reference/android/support/v4/app/Fragment.html
I am developing an application based on Android 2.1, on one page of my application, there needs an numberpicker. we know that android 2.1 doesn't contain numberpicker control, so I write one.
I need to show my version of numberpicker on Android 2.1, but the ICS style numberpicker in Android 4.0, to achieve that aim, I used reflection, When in my code, I detected the current Build.VERSION.SDK_INT >= 14, I reflect a numberpicker of the target platform, and add it to current view dynamically.
My question is I can reflect a numberpicker when I run my application on an Android 4.0 platform, but the numberpicker style doesnot appear to be ICS style, Why?
thanks in advance!
If you have the patience you could probably back-port the NumberPicker class. That aside, why use reflection? If you set your target API to 15 this is all you need. In your layout folder declare your alternative number picker. In layout-v11 (NumberPicker is available in API 11 Honeycomb or higher) declare android.widget.NumberPicker. Give each the same ID and in your Activity have something along these lines:
private NumberPicker mNumPicker;
private SomeView mOldNumPicker;
public void onCreate(...) {
// Use NumberPicker
if (android.os.Build.VERSION.SDK_INT >= 11) {
mNumPicker = findViewById(R.id.numpicker);
} else {
mOldNumPicker = findViewById(R.id.numpicker);
}
}
This was you do not need reflection and will not run into any crashes due to accessing non-existent APIs. From here on out you just check if (mNumPicker == null) and if (mOldNumPicker == null) and determine which methods to call based on that. There is also this example of a Number Picker using Buttons and an EditText.