shaoyihe
shaoyihe

Reputation: 1087

Why doesn't android:colorBackground work?

In this HelloWorld app, I'm using the android:colorBackground property, but it doesn't apply the red background.

AndroidManifest.xml:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".EditTextActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Layout xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".EditTextActivity">

    <TextView
        android:text="Hello World!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        />
</RelativeLayout>

style.xml:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:colorBackground">#ff0000</item>
    </style>
</resources>

The result looks like this (with no red color background): Hello World

Upvotes: 2

Views: 2230

Answers (2)

Gibas
Gibas

Reputation: 55

I don't know why your problem happens, but what I did was just put the hexadecimal code as a custom color in colors.xml, then reference it in android:colorBackground. That way, the background color really applies.

Upvotes: 1

Mahdi Nouri
Mahdi Nouri

Reputation: 1389

you can use android:background="#FF0000" in your RelativeLayout or create a styles.xml for v21 and write this:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@color/redcolor</item>

</style>

also in your styles.xml :

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@color/redcolor</item>
</style>

Upvotes: 5

Related Questions