Skip to content

Instantly share code, notes, and snippets.

@gbzarelli
Created January 31, 2018 17:58
Show Gist options
  • Save gbzarelli/096776203423ccfd6f0a5aa2ae362afb to your computer and use it in GitHub Desktop.
Save gbzarelli/096776203423ccfd6f0a5aa2ae362afb to your computer and use it in GitHub Desktop.
Android – Bloquear a expansão da barra de status programaticamente (prevent status bar expansion)
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
/**
* <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
* Created by gbzarelli on 1/29/18.
*/
public class StatusBarUtils {
public static void removeViewGroup(Activity activity, ViewGroup viewGroup) {
WindowManager manager = (WindowManager) activity.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
manager.removeView(viewGroup);
}
public static ViewGroup preventStatusBarExpansion(Activity activity) {
return preventStatusBarExpansion(activity, new DefaultCustomView(activity));
}
public static ViewGroup preventStatusBarExpansion(Activity activity, ViewGroup viewGroup) {
WindowManager manager = ((WindowManager) activity.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
int resId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
int result = 0;
if (resId > 0) {
result = activity.getResources().getDimensionPixelSize(resId);
}
localLayoutParams.height = result;
localLayoutParams.format = PixelFormat.TRANSPARENT;
if (viewGroup == null) {
viewGroup = new DefaultCustomView(activity);
}
manager.addView(viewGroup, localLayoutParams);
return viewGroup;
}
public static class DefaultCustomView extends ViewGroup {
public DefaultCustomView(Context context) {
super(context);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
}
}
@Tmsolutions
Copy link

onde colocar este arquivo e como chamá-lo

@gbzarelli
Copy link
Author

@Tmsolutions você pode colocar em qualquer lugar (estrutura que desejar) no caso de onde chama-lo, quando implementei utilizei na minha Activity principal do projeto, sendo chamada pelo meu fragment. Ex:>

Activity:>

[...]
class HomeActivity : Activity() {

    companion object {
        @SuppressLint("StaticFieldLeak")
        private var customStatusBar: ViewGroup? = null

        fun resetStatusBar(activity: Activity) {
            if (customStatusBar != null) {
                StatusBarUtils.removeViewGroup(activity, customStatusBar)
                customStatusBar = null
            }
        }
    }
[...]

Chamada pelo Fragment associada a activity:

class GridActivity : FragmentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_grid)
        HomeActivity.resetStatusBar(this)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment