2

I am using 1.6 i.e. API 4 to build my application. There are couple of commands that are supported by higher versions. I want to write those commands and make application more compatible for higher versons. Like, I use Tabs. I want to use setLeftStripDrawable and setRightStripDrawable, but they are supported by API 8.

I write something like :

// I want these lines to come into affect only if the device SDK is greater than 7 as SDK of below 7, doesn't support these methods.
if (android.os.Build.VERSION.SDK_INT > 7) {   
    tw.setLeftStripDrawable(R.drawable.tab_selected_bar_left_v4);  // TabWidget
}

EDIT : I want to set setLeftStripDrawable to the tabs used in my app. In my manifest I have uses-sdk android:minSdkVersion="4". If I write the lines as above and compile it in 2.3, it compiles successfully. When I run in 1.6 I get "java.lang.VerifyError". If I remove those liens and again run in 1.6, it works properly.

What should I do to execute those lines only if the device SDK api is > 7, and if it is less than that then those lines should not come under any affect ?

Any clue ?

tshepang
  • 10,772
  • 21
  • 84
  • 127
Tvd
  • 4,143
  • 16
  • 71
  • 118

4 Answers4

3
if (Build.VERSION.SDK_INT > 7) {
    ...
}
THelper
  • 14,136
  • 6
  • 59
  • 95
  • I cna get all that values. My point is : while building on 1.6, tese lines will obviously not compile as their are no such methods in 1.6. The methos id introduced in API 8 and 1.6 is aPI 4. if (android.os.Build.VERSION.SDK_INT > 7) { tw.setLeftStripDrawable(R.drawable.tab_selected_bar_left_v4); } – Tvd May 24 '11 at 13:55
  • You should be building against the newest SDK that your application supports, not the oldest. – Jake Wharton May 24 '11 at 14:03
  • 1
    @Jake, I build my app against 2.3.3 and added the above lines if condition. Now if I try to execute the app in 1.6 device, I get "java.lang.VerifyError". I removed those liens and again executed in 1.6, it ran successfully. How do I handle this ? My manifest contains android:minSdkVersion="4". – Tvd May 25 '11 at 07:14
  • You're right this doesn't work in your case. I got mixed up. You can use the above only for calling a method that was removed in a later SDK. Your problem requires reflection. I will post another answer with the solution. – THelper May 25 '11 at 08:21
2

I think you should use something like this. I did this by heart, so there might be some errors.

try {
    Method twMethod = TabWidget.class.getMethod("setLeftStripDrawable", new Class[] { int.class });
    twMethod.invoke(tw, R.drawable.yourdrawable);
} catch (NoSuchMethodException e) {
    /* not supported */
} catch (IllegalArgumentException e) {
    /* wrong class provided */
} catch (IllegalAccessException e) {
    /* Java access control has denied access */
} catch (InvocationTargetException e) {
    /* method has thrown an exception */
}
THelper
  • 14,136
  • 6
  • 59
  • 95
  • With this code, I get "NoSuchMethodException" only even if I run the app in 2.3 emulator. I tried changing the parameter from Integer to Drawable and provided the R.drawable as Drawable. With that I got "IllegalArgumentException". I made this and the calling function as public. The calling function is being called from an ASyncTask object. Despite the se exceptions are thrown. Any idea, what more am I missing that can be the fault. – Tvd May 25 '11 at 11:03
  • Thanks. { int.class } did the job. Its working in 2.3, not in 2.1 as its API is 7. BUT It seems I don't get what I want. Please look at http://stackoverflow.com/questions/6070472/android-set-clicked-color-of-tab-to-background-color-of-a-tablerow See the difference in left and right side. I want to produce results as left side using settings. Any idea what to use for that ? Looking at the descr I thought this method will do the job, but its just filling that space not making a smooth curve on both sides. – Tvd May 25 '11 at 11:22
1

You can try looking at Android Reflection. I have not used it yet myself, but as far as i understand, you can test for classes and methods that you know the name of. Then you can instantiate and use them.

You can read some of the basics here: http://www.tutorialbin.com/tutorials/85977/java-for-android-developers-reflection-basics

Rasmus Øvlesen
  • 492
  • 2
  • 7
  • Thanks. WEll Reflection might be helpful as it is used to execute at Runtime and not Compile time. I read the article twice, but couldn't make out how to write these lines which are part of a tab settings method : if (android.os.Build.VERSION.SDK_INT > 7) { tw.setLeftStripDrawable(R.drawable.tab_selected_bar_left_v4); tw.setRightStripDrawable(R.drawable.tab_selected_bar_right_v4); } – Tvd May 25 '11 at 07:43
1

Here's some sample Android code, using reflection, that does something similar. It calls the getRotation() method from the Display class; the method only exists in SDK 8+. I've used it in one of my apps and it works:

    //I want to run this:  displayrotation = getWindowManager().getDefaultDisplay().getRotation();
    //but the getRotation() method only exists in SDK 8+, so I have to call it in a sly way, using Java "reflection"
    try{
        Method m = Display.class.getMethod("getRotation", (Class[]) null);//grab the getRotation() method if it exists
        //second argument above is an array containing input Class types for the method. In this case it takes no inputs.
        displayrotation = (Integer) m.invoke(getWindowManager().getDefaultDisplay(),(Object[]) null);
        //again, second argument is an array of inputs, in this case empty
    }catch(Exception e){//if method doesn't exist, take appropriate alternate actions
        Log.w("getRotation","old OS version => Assuming 90 degrees rotation");
        displayrotation = Surface.ROTATION_90;
    }
Luke
  • 11
  • 1