3

My app is using Actionbar to set title and show subtitle which keeps changing according to user interaction, i am also using splitActionBarWhenNarrow and showing menu options in actionbar.

I want my app to support devices with older version of android (bellow API 11), for this i have to set android:minSdkVersion="8", but when i set minimum sdk version bellow 11 i get error (Call requires API level 11 (current min is 8): android.app.Activity#getActionBar) on actionbar:

getActionBar().setTitle(record_name);
getActionBar().setSubtitle("Total: "+BigDecimal.valueOf(total).toPlainString()+"/-");

I tried to resolve this by adding an if condition to check version of API:

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if(currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB)
{   
    //Use Actionbar to set title and subtitle etc.
    getActionBar().setTitle(record_name);
    getActionBar().setSubtitle("Total: "+BigDecimal.valueOf(total).toPlainString()+"/-");
    getActionBar().setIcon(R.drawable.ic_header);
    getActionBar().setDisplayHomeAsUpEnabled(true);

}
else
{

}

But it dosent work, i still get error on getActionBar()...

What can i do to resolve this problem and avoid using actionbar if API is <11, and use actionbar if API is >=11 ?

Darshan
  • 515
  • 1
  • 3
  • 16
  • Annotate the method with `@TargetApi(Build.VERSION_CODES.HONEYCOMB`. It will remove the warnings. Keep the IF-ELSE block as well as this will not magically make action bar work on old devices. This is a common pattern. – Eugen Pechanec Sep 20 '15 at 14:52
  • @EugenPechanec , you mentioned that @TargetApi(...) will remove the warnings, as i have said i am getting error, it will remove error also? – Darshan Sep 20 '15 at 16:49
  • If you can compile and run the app now, it's going to be just fine. – Eugen Pechanec Sep 20 '15 at 17:13
  • @EugenPechanec Thanks man, it solved my problem, i am using actionBar at different places in activity so i just annotate the Class with '@TargetApi(Build.VERSION_CODES.HONEYCOMB', it removed error from everywhere in class. – Darshan Sep 20 '15 at 17:56
  • Good, glad I could help. I just posted a legit answer. – Eugen Pechanec Sep 20 '15 at 18:08

3 Answers3

2

To remove this warning you need to tell the IDE you are aware that the methods/fileds/classes you use are available since later SDK than your minimum supported. This is done via @TargetApi annotation. In your case @TargetApi(Build.VERSION_CODES.HONEYCOMB) annotated method or the whole class.

Important: This will only remove the warning, you still have to ensure that these methods do NOT get called on devices where they don't exist.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
1

Use Support Library and getSupportActionBar(). This way you will have ActinBar for every API level. Google Android Support Library v4 for more info.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • Support action bar is available in appcompat-v7 not support-v4. – Eugen Pechanec Sep 20 '15 at 14:53
  • Thanks @Alex , i will check it out, but is it possible to just avoid actionbar for older version and use it if API is 11 , without adding any additional libraries? – Darshan Sep 20 '15 at 14:53
  • Everyone uses support library – Alexander Kulyakhtin Sep 20 '15 at 17:54
  • You most likely want to use the support library anyway. There's other things in the support library that you may eventually want to use. Also, the new thing now is the Toolbar library which can be used as a ActionBar, but it offers other freedoms that you may wan to take advantage of eventually. – DeeV Sep 20 '15 at 18:19
  • Thanks for the information guys it will be helpful for my next app, in my current app there is only three lines where im using actionBar so for now i will go with @EugenPechanec 's suggestion. – Darshan Sep 20 '15 at 19:32
1
int currentApiVersion = android.os.Build.VERSION.SDK_INT;

gets you the current api version.

ActionBar actionBar = getActionBar(); //from API level 11

If it is null you do not have a actionbar at hand.

You can also use getSupportActionBar()

Ref: Retrieving Android API version programmatically

http://developer.android.com/reference/android/app/Activity.html#getActionBar%28%29

So I would do:

if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {   
    ActionBar actionBar = getActionBar(); //or SupportActionbar
    if (actionBar != null) {
        //Use Actionbar to set title and subtitle etc.
        actionBar.setTitle(record_name);
        actionBar.setSubtitle("Total: "+BigDecimal.valueOf(total).toPlainString()+"/-");
        actionBar.setIcon(R.drawable.ic_header);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}
Community
  • 1
  • 1
sschrass
  • 7,014
  • 6
  • 43
  • 62
  • It still gives error on actionBar as i have set 'android:minSdkVersion="8" '. – Darshan Sep 20 '15 at 16:54
  • The error was `Call requires API level 11 (current min is 8): android.app.Activity#getActionBar` wherever i have used ActionBar. – Darshan Sep 20 '15 at 17:58