Skip to content

Instantly share code, notes, and snippets.

@gabrielemariotti
Created September 3, 2014 10:15
Show Gist options
  • Save gabrielemariotti/d78709cc6cbc28d74faf to your computer and use it in GitHub Desktop.
Save gabrielemariotti/d78709cc6cbc28d74faf to your computer and use it in GitHub Desktop.
HelperUtil
public class HelperUtil {
public static HelperUtilBase getInstance(Context context) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
return new HelperUtilL(context);
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
return new HelperUtilKK(context);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
return new HelperUtilJB(context);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return new HelperUtilICS(context);
} else {
return new HelperUtilBase(context);
}
}
}
public class HelperUtilBase {
protected Context mContext;
public HelperUtilBase(Context context){
this.mContext = context;
}
public void setActionBar() {
// Do nothing pre-L
}
public void setElevation(View view, float elevation){
// Do nothing pre-L
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public class HelperUtilKK extends HelperUtilBase{
public HelperUtilKK(Context context) {
super(context);
}
}
@TargetApi(Build.VERSION_CODES.L)
public class HelperUtilL extends HelperUtilBase{
private Toolbar mActionBarToolbar;
public HelperUtilL(Context context) {
super(context);
}
@Override
public void setElevation(View view, float elevation){
view.setElevation(elevation);
}
@Override
public void setActionBar() {
mActionBarToolbar = (Toolbar) ((Activity)mContext).findViewById(R.id.toolbar);
if (mActionBarToolbar != null) {
((Activity)mContext).setActionBar(mActionBarToolbar);
}
}
//......
}
public class MyActivity extends Activity {
HelperUtilBase helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
helper = HelperUtil.getInstance(this);
//....
helper.setActionBar();
helper.setElevation(cardView, 4.0f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment