Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to get background color of Activity in Java?

How can I get background color and text color (default for child views) of an Activity in Java?

like image 744
TN. Avatar asked Sep 07 '10 07:09

TN.


People also ask

How do you get the background color on a view?

To get background color of a Layout: LinearLayout lay = (LinearLayout) findViewById(R. id. lay1); ColorDrawable viewColor = (ColorDrawable) lay.

How to change background color in Android programmatically?

screen); View root = someView. getRootView(); root. setBackgroundColor(color. white);

Which of the following is used to set the background color in Android?

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.


2 Answers

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();
like image 146
jzavisek Avatar answered Oct 31 '22 01:10

jzavisek


For Background

TypedArray array = context.getTheme().obtainStyledAttributes(new int[] {
        android.R.attr.windowBackground});
    int backgroundColor = array.getColor(0, 0xFF00FF);
    array.recycle();
    view.setBackgroundColor(backgroundColor);
like image 39
Javier Avatar answered Oct 31 '22 01:10

Javier