Created
July 24, 2018 03:03
-
-
Save iamdeveloper-lopez/92af6592210bbfcb6c6d92f69f9a6efb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StatusBar extends View { | |
private int statusBarColor = Integer.MIN_VALUE; | |
public StatusBar(Context context) { | |
super(context); | |
} | |
public StatusBar(Context context, @Nullable AttributeSet attrs) { | |
super(context, attrs); | |
init(context, attrs); | |
} | |
public StatusBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(context, attrs); | |
} | |
public StatusBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(context, attrs); | |
} | |
private void init(Context context, AttributeSet attrs) { | |
if (attrs != null) { | |
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, | |
R.styleable.StatusBar, 0, 0); | |
try { | |
//get the text and colors specified using the names in attrs.xml | |
statusBarColor = a.getInteger(R.styleable.StatusBar_statusBarColor, Integer.MIN_VALUE);//0 is default | |
} finally { | |
a.recycle(); | |
} | |
} | |
} | |
@Override | |
public void draw(Canvas canvas) { | |
Activity host = ((Activity) getContext()); | |
Rect rectangle = new Rect(); | |
Window window = host.getWindow(); | |
window.getDecorView().getWindowVisibleDisplayFrame(rectangle); | |
int height = rectangle.top; | |
DisplayMetrics metrics = host.getResources().getDisplayMetrics(); | |
int width = metrics.widthPixels; | |
if (statusBarColor == Integer.MIN_VALUE) { | |
TypedValue typedValue = new TypedValue(); | |
host.getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true); | |
statusBarColor = typedValue.data; | |
} | |
Paint paint = new Paint(); | |
paint.setStyle(Paint.Style.FILL); | |
paint.setColor(statusBarColor); | |
paint.setAntiAlias(true); | |
Rect statusBar = new Rect( | |
0, // Left | |
0, // Top | |
width, // Right | |
height // Bottom | |
); | |
canvas.drawRect(statusBar, paint); | |
super.draw(canvas); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="StatusBar"> | |
<attr name="statusBarColor" format="color"/> | |
</declare-styleable> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment