Search code examples
androidmaterial-designandroid-compatibilityandroid-api-levels

How to deal with different API levels in code


I started with development with material design.

My layouts and styles are separated in those for pre-lollipop devices and those for lollipop and higher.

E.g i use

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
style="@style/Toolbar" />

In my pre-lollipop layout file and

<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
style="@style/Toolbar" />

in the other one. Therefore I am forced to use two different objects in the related activity class. That confuses me a lot and leads to confusing code.

I know how to distinguish between API levels via

Build.VERSION.SDK_INT

Nevertheless what is the best approach to solve this problem?

Thank you for answers


Solution

  • i usually use android.support.v7.widget.Toolbar, because it's compatible with other API. The regular one will give you a lot of problems if you use it a lot in your code.