Skip to content

Commit

Permalink
Support Light Status Bar
Browse files Browse the repository at this point in the history
  • Loading branch information
niorgai committed Dec 12, 2018
1 parent ff207ae commit cd6af54
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 29 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ Add the dependency
```groovy
dependencies {
// After AndroidX
implementation ('com.github.niorgai:StatusBarCompat:2.2.0', {
exclude group: 'com.android.support'
implementation ('com.github.niorgai:StatusBarCompat:2.3.0', {
exclude group: 'androidx.appcompat:appcompat'
exclude group: 'com.google.android.material:material'
})
// Before AndroidX
compile ('com.github.niorgai:StatusBarCompat:2.1.4', {
Expand All @@ -46,7 +47,8 @@ TranslucentStatusBar | SetStatusBarColor
---

This is a utils for status bar, And you **do not need to** change your `style.xml` file.
The most feature is **you can toggle `setStatusBarColor` and `translucentStatusBar` without recreate activity**.

The most important feature is **you can toggle `setStatusBarColor` and `translucentStatusBar` without recreate activity**.

```java
//set color for status bar
Expand All @@ -63,12 +65,18 @@ The most feature is **you can toggle `setStatusBarColor` and `translucentStatusB
setStatusBarColorForCollapsingToolbar(Activity activity, AppBarLayout appBarLayout, CollapsingToolbarLayout collapsingToolbarLayout,
Toolbar toolbar, int statusColor)

//Change to Light status bar (convert icon and text color to dark, SDK >= 23)
changeToLightStatusBar(Activity activity)

//Cancel Light status bar (convert icon and text color to light)
cancelLightStatusBar(Activity activity)
```

# How to use

1. Use it after call `setContentView`
2. If use FullScreen Activity, must call `StatusBarCompat.translucentStatusBar(activity);`
3. If you want to set light status bar, Remember call `changeToLightStatusBar()` at last.

# About Demo:
Demo shows One Activity with four Fragments.
Expand All @@ -77,6 +85,7 @@ Demo shows One Activity with four Fragments.
2. TranslucentFragment call `translucentStatusBar` method, when sdk >= 21, button can toggle hide/show statusBar's shadow.
3. DrawerFragment call `setStatusBarColor` method, show how to compat for DrawerLayout and CoordinatorLayout.
4. CollapsingFragment call `setStatusBarColorForCollapsingToolbar` method, compat for CollapsingToolbarLayout.
5. Each Fragment can toggle Light status bar with Checkbox.

## Issues:
These problem only show in SDK between 19 and 21:
Expand Down Expand Up @@ -112,11 +121,18 @@ StatusBarCompat.translucentStatusBar(Activity activity, boolean hideStatusBarBac

//为 CollapsingToolbarLayout 设置颜色
setStatusBarColorForCollapsingToolbar(Activity activity, AppBarLayout appBarLayout, CollapsingToolbarLayout collapsingToolbarLayout, Toolbar toolbar, int statusColor)

//SDK >= 23, 将状态栏改为浅色模式(状态栏 icon 和字体会变成深色)
changeToLightStatusBar(Activity activity)

//将状态栏改为深色模式(状态栏 icon 和字体会变成浅色, 即默认模式)
cancelLightStatusBar(Activity activity)
```

# 怎么使用
1.`setContentView` 方法调用后再设置.
2. 如果使用了全屏 Activity ,记得调用`StatusBarCompat.translucentStatusBar(activity);`
2. 如果要使用全屏 Activity , 调用 `StatusBarCompat.translucentStatusBar(activity)` .
3. 如果想让状态栏图标变成深色, 记得**最后**调用 `changeToLightStatusBar(activity)` .

# Demo 说明:
Demo 只有一个 Activity , 分四个 Tab.
Expand All @@ -125,6 +141,7 @@ Demo 只有一个 Activity , 分四个 Tab.
2. TranslucentFragment 为图片布局, 展示透明状态栏效果, SDK >= 21时, 可以通过按钮显示\隐藏 statusBar 的 shadow, 调用方法为: `translucentStatusBar`.
3. DrawerFragment 展示 DrawerLayout + CoordinatorLayout, 调用方法为: `setStatusBarColor`.
4. CollapsingFragment 展示 CollapsingLayout 的适配效果, 调用方法为: `setStatusBarColorForCollapsingToolbar`.
5. 每个 Fragment 下都可以通过 Checkbox 切换浅色/深色状态栏.

## 已知问题
这些问题仅仅出现在 4.4 上
Expand Down
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
implementation project(path: ':library')
implementation "androidx.appcompat:appcompat:${lib.androidXVersion}"
implementation "com.google.android.material:material:${lib.androidXVersion}"
implementation "androidx.legacy:legacy-support-v4:${lib.androidXVersion}"
implementation "androidx.cardview:cardview:${lib.androidXVersion}"
implementation "com.google.android.material:material:${lib.materialVersion}"
implementation "androidx.legacy:legacy-support-v4:${lib.materialVersion}"
implementation "androidx.cardview:cardview:${lib.materialVersion}"
}
43 changes: 43 additions & 0 deletions app/src/main/java/qiu/statusbarcompat/fragment/BaseFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package qiu.statusbarcompat.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import qiu.niorgai.StatusBarCompat;
import qiu.statusbarcompat.R;

/**
* Created by jianqiu on 12/12/18.
*/
public class BaseFragment extends Fragment implements CompoundButton.OnCheckedChangeListener {

protected CheckBox mCheckBox;

protected void initCheckBox() {
if (getView() == null) {
return;
}
mCheckBox = getView().findViewById(R.id.light_status_bar);
mCheckBox.setOnCheckedChangeListener(this);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initCheckBox();
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
StatusBarCompat.changeToLightStatusBar(getActivity());
} else {
StatusBarCompat.cancelLightStatusBar(getActivity());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import androidx.fragment.app.Fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import qiu.niorgai.StatusBarCompat;
import qiu.statusbarcompat.R;

/**
* Compat CollapsingToolbarLayout
* Created by qiu on 7/27/16.
*/
public class CollapsingToolbarFragment extends Fragment {
public class CollapsingToolbarFragment extends BaseFragment {

public CollapsingToolbarFragment() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;

import androidx.annotation.Nullable;
import qiu.niorgai.StatusBarCompat;
import qiu.statusbarcompat.R;
import qiu.statusbarcompat.activity.MainActivity;

/**
* Common Layout, test for setStatusBarColor
*/
public class CommonFragment extends Fragment {
public class CommonFragment extends BaseFragment {

public CommonFragment() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package qiu.statusbarcompat.fragment;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import qiu.niorgai.StatusBarCompat;
import qiu.statusbarcompat.R;

/**
* Test for DrawerLayout and CoordinatorLayout
*/
public class DrawerFragment extends Fragment {
public class DrawerFragment extends BaseFragment {

public DrawerFragment() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Test for translucent
*/
public class TranslucentFragment extends Fragment {
public class TranslucentFragment extends BaseFragment {

private boolean isHide = false;

Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/layout/fragment_collapsing_tool_bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@

</androidx.core.widget.NestedScrollView>

<CheckBox
android:id="@+id/light_status_bar"
android:layout_above="@+id/seek_bar"
android:layout_marginBottom="10dp"
android:layout_gravity="right|bottom"
android:layout_marginRight="10dp"
android:text="Light Status Bar"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_height="wrap_content"
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/layout/fragment_common.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@
android:layout_height="wrap_content"/>


<CheckBox
android:id="@+id/light_status_bar"
android:layout_above="@+id/seek_bar"
android:layout_marginBottom="50dp"
android:text="Light Status Bar"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</RelativeLayout>
11 changes: 11 additions & 0 deletions app/src/main/res/layout/fragment_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@

</androidx.core.widget.NestedScrollView>

<CheckBox
android:id="@+id/light_status_bar"
android:layout_above="@+id/seek_bar"
android:layout_gravity="right|bottom"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:text="Light Status Bar"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

<com.google.android.material.navigation.NavigationView
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/layout/fragment_translucent.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<CheckBox
android:id="@+id/light_status_bar"
android:layout_above="@+id/seek_bar"
android:layout_marginBottom="50dp"
android:text="Light Status Bar"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'com.android.tools.build:gradle:3.2.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -33,7 +33,8 @@ ext {
]

libs = [
androidXVersion : "1.0.0"
androidXVersion : "1.0.2",
materialVersion : "1.0.0"
]
}

2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
implementation "androidx.appcompat:appcompat:${lib.androidXVersion}"
implementation "com.google.android.material:material:${lib.androidXVersion}"
implementation "com.google.android.material:material:${lib.materialVersion}"
}
44 changes: 42 additions & 2 deletions library/src/main/java/qiu/niorgai/StatusBarCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import android.app.Activity;
import android.os.Build;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import android.view.View;
import android.view.Window;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;

/**
Expand Down Expand Up @@ -67,4 +71,40 @@ public static void setStatusBarColorForCollapsingToolbar(@NonNull Activity activ
StatusBarCompatKitKat.setStatusBarColorForCollapsingToolbar(activity, appBarLayout, collapsingToolbarLayout, toolbar, statusColor);
}
}

public static void changeToLightStatusBar(@NonNull Activity activity) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return;
}
if (activity == null) {
return;
}
Window window = activity.getWindow();
if (window == null) {
return;
}
View decorView = window.getDecorView();
if (decorView == null) {
return;
}
decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}

public static void cancelLightStatusBar(@NonNull Activity activity) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return;
}
if (activity == null) {
return;
}
Window window = activity.getWindow();
if (window == null) {
return;
}
View decorView = window.getDecorView();
if (decorView == null) {
return;
}
decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() ^ View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}

0 comments on commit cd6af54

Please sign in to comment.