adjust Status Bar - Android User Interface

Android examples for User Interface:StatusBar

Description

adjust Status Bar

Demo Code


//package com.java2s;

import android.content.Context;
import android.os.Build;
import android.util.TypedValue;
import android.view.View;
import java.lang.reflect.Field;

public class Main {
    public static void adjustStatusBar(View view, Context context) {
        if (view == null) {
            return;
        }// w w w  .ja v  a  2s  .  c o  m
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT >= 21) {
                view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            }
            view.setPadding(0, getStatusBarHeight(context), 0, 0);
        }
    }

    public static int getStatusBarHeight(Context context) {
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object obj = clazz.newInstance();
            Field field = clazz.getField("status_bar_height");
            int id = Integer.parseInt(field.get(obj).toString());
            return context.getResources().getDimensionPixelSize(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                25, context.getResources().getDisplayMetrics());
    }
}

Related Tutorials