Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android find default background color

Tags:

android

I have an app with a simple recyclerview.

The layout is declared as follow

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/contentView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/some_id" />

</RelativeLayout>

I notice that on different devices, the recyclerView actually had a different background. In newer devices, the background is just white. On older devices, the background is light gray.

In android studio, the background color is blank in the design view.

So my question is, where does this gray color come from? How can I change it universally to white?

I can obviously just add background:white to this particular view. But is there a way to overwrite the system default?

like image 673
Zhen Liu Avatar asked Dec 06 '16 16:12

Zhen Liu


People also ask

What is the default background color in Android?

By default each activity in Android has a white background. To change the background color, first add a new color definition to the colors.

How do I change my default background color?

To open it go to Tools -> Android -> Theme Editor.


2 Answers

Android default background color

 <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="?android:colorBackground">
 </RelativeLayout

use

 android:background="?android:colorBackground"
like image 119
Atul Avatar answered Sep 29 '22 20:09

Atul


What you are seeing is actually the background of the activity, not the recyclerview, which is transparent by default. The color did change a view time per Android version.

You can override this in your app theme.

First define the color in values/colors.xml

 <resources>
     <color name="background">#FF0000 </color> 
 </resources> 

Create a themes.xml file in res/values that references that color:

<resources>  
    <style name="MyTheme" parent="@android:style/Theme.Light">       
        <item name="android:windowBackground">@color/background</item>  
    </style> 
</resources> 

and then in your AndroidManifest.xml specify this as the theme for your activities to use.

 <activity
        android:name=".MyActivity"
        android:theme="@style/MyTheme" />

see https://stackoverflow.com/a/10157077/4623782

like image 38
Raymond Avatar answered Sep 29 '22 18:09

Raymond