How can I get background color and text color (default for child views) of an Activity in Java?
To get background color of a Layout: LinearLayout lay = (LinearLayout) findViewById(R. id. lay1); ColorDrawable viewColor = (ColorDrawable) lay.
screen); View root = someView. getRootView(); root. setBackgroundColor(color. white);
android:background="" is the attribute used to set background for any Layout file. You can directly specify the value as HEX color code as we do for CSS files in HTML.
TypedArray array = getTheme().obtainStyledAttributes(new int[] {
android.R.attr.colorBackground,
android.R.attr.textColorPrimary,
});
int backgroundColor = array.getColor(0, 0xFF00FF);
int textColor = array.getColor(1, 0xFF00FF);
array.recycle();
For Background
TypedArray array = context.getTheme().obtainStyledAttributes(new int[] {
android.R.attr.windowBackground});
int backgroundColor = array.getColor(0, 0xFF00FF);
array.recycle();
view.setBackgroundColor(backgroundColor);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With