Skip to content

Commit

Permalink
Add DynamicViewUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavpandey committed Sep 7, 2017
1 parent 4d4a502 commit e5e7e81
Showing 1 changed file with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.pranavpandey.android.dynamic.utils;

import android.annotation.TargetApi;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;

/**
* Helper class to perform {@link View} operations.
*/
public class DynamicViewUtils {

/**
* Set light status bar if we are using light primary color on
* Android M or above devices.
*
* @param isLight {@code true} to set the light status bar.
*/
@TargetApi(android.os.Build.VERSION_CODES.M)
public static void setLightStatusBar(@NonNull View view, boolean isLight){
if (DynamicVersionUtils.isMarshmallow()) {
int flags = view.getSystemUiVisibility();
if (isLight) {
flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} else {
flags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}

view.setSystemUiVisibility(flags);
}
}

/**
* Set light navigation bar if we are using light primary color on
* Android O or above devices.
*
* @param isLight {@code true} to set the light navigation bar.
*/
@TargetApi(android.os.Build.VERSION_CODES.O)
public static void setLightNavigationBar(@NonNull View view, boolean isLight){
if (DynamicVersionUtils.isOreo()) {
int flags = view.getSystemUiVisibility();
if (isLight) {
flags |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
} else {
flags &= ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
}

view.setSystemUiVisibility(flags);
}
}

/**
* Add a view to the view group.
*
* @param viewGroup View group to add the view.
* @param view View to be added.
* @param removePrevious {@link true} to remove all the previous
* views of the view group.
*/
public static void addView(@NonNull ViewGroup viewGroup,
@NonNull View view, boolean removePrevious) {
if (removePrevious && viewGroup.getChildCount() > 0) {
viewGroup.removeAllViews();
}

viewGroup.addView(view);
}
}

0 comments on commit e5e7e81

Please sign in to comment.