JusLi
JusLi

Reputation: 45

LinearLayout defStyle on Android SDK 10?

I'm writing a class that extends LinearLayout from the Android library. In the class constructor I used the LinearyLayout constructor LinearLayout(Context context,AttributeSet attrs, int defStyles) introduced only in SDK 11. The problem is that I am trying to make the application run on Android 2.3.3 which runs SDK 10. I have tried introducing a check similar to this:

public SView(Context context, AttributeSet attrs, int defStyle) {
    if (android.os.Build.VERSION.SDK_INT <= 10) {
        super(context, attrs, defStyle);
    } else {
        super(context, attrs, defStyle);
    }
    setupView(context);
}

But the problem persists because super must be on the first line. Another issue is the functionality that defStyle provides is not substituted.

What should I do to fix this?

Upvotes: 1

Views: 267

Answers (1)

HalR
HalR

Reputation: 11073

One solution would be to use two separate XML files. One that uses SView, and one that uses an alternative that is "2.3.3 friendly".

You would do your check before setting your activity or fragment's layout. Like this:

   int layoutId = R.layout.fancy_stuff;
   if (android.os.Build.VERSION.SDK_INT <= 10) {
        layoutId = R.layout.boring_stuff;
   }
   setContentView(layoutId);

fancy_stuff would refer to your SView, and boring_stuff would refer to your SDK 10 stuff.

Upvotes: 0

Related Questions