locked
android:windowBackground property makes background black (Xamarin.Android) RRS feed

  • Question

  • User337237 posted

    I want to make the following image to be the background for the entire Xamarin.Android application:

    I reference the image in Base application theme in styles.xml :

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
       .....
       <item name="android:windowBackground">@drawable/ic_app_background_image</item>
       .....
    </style>
    

    The background applies to all the application but the image has dark background like that:

    Also the number 51 has some strange things around it.

    When I set the android:background="@drawable/ic_app_background_image" to the most outer LinearLayout of an activity it works fine, it shows the screen like that:

    How can I fix the android:windowBackground property setting the background to black issue so I don't have to put android:background="@drawable/ic_app_background_image" in the LinearLayout of every activity?

    Sunday, December 29, 2019 12:15 PM

Answers

  • User382871 posted

    Change android:windowBackground to android:background, the android:windowBackground is used to change the activity background color. <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> ..... <item name="android:background">@drawable/ic_app_background_image</item> <!--<item name="android:windowBackground">@drawable/ic_app_background_image</item>--> ..... </style>

    Tutorial: https://developer.android.com/guide/topics/ui/look-and-feel/themes#CustomizeTheme

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, December 30, 2019 8:38 AM

All replies

  • User382871 posted

    Change android:windowBackground to android:background, the android:windowBackground is used to change the activity background color. <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> ..... <item name="android:background">@drawable/ic_app_background_image</item> <!--<item name="android:windowBackground">@drawable/ic_app_background_image</item>--> ..... </style>

    Tutorial: https://developer.android.com/guide/topics/ui/look-and-feel/themes#CustomizeTheme

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, December 30, 2019 8:38 AM
  • User337237 posted

    Doing that, I get the following result:

    Monday, December 30, 2019 9:11 AM
  • User382871 posted

    Sorry for my mistake, just use the android:windowBackground. I've tested the code in a basic sample and it works well as shown.

    Here is the demo file you can refer to it.

    Tuesday, December 31, 2019 9:00 AM
  • User337237 posted

    Can you tell me which lines of the project have you changed so it works, because opening the TestDemo is risky?

    Tuesday, December 31, 2019 12:02 PM
  • User382871 posted

    Here are the related code and settings. styles.xml ```

    layout.xml

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/text"
            android:focusable="true"
            android:text="text"
            android:textColor="@android:color/white"
            android:focusableInTouchMode="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <ImageView
            android:src="@drawable/grid_"
            android:layout_width="match_parent"
            android:layout_height="35dp"/>
    </LinearLayout>
    
    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="text" />
    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button1" />
    

    ```

    Wednesday, January 1, 2020 2:10 AM
  • User337237 posted

    Only putting <item name="android:windowBackground">@drawable/img</item> in style tag in styles.xml like that:

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        .....
        <item name="android:windowBackground">@drawable/img</item>
        .....
    </style>
    

    works for you because your image has a background, while mine doesn't, it looks like that:

    When I put my image in your application,I get the same result as in my case, the following screen:

    What I don't understand is why <item name = "android:windowBackground"> @drawable/ic_app_background_image </item> sets the background color to black when without the image it is white , like that:

    and how to fix it?

    Friday, January 3, 2020 10:34 AM
  • User382871 posted

    Would you mind sharing a basic demo to produce the issue or the image file?

    Monday, January 6, 2020 10:25 AM
  • User337237 posted

    It's not allowoing me to upload the zip demo. It is telling me that the file is too big. I don't know why, it is only 60 MB. Here is the image:

    Monday, January 6, 2020 12:08 PM
  • User382871 posted

    Try to add the two lines code in styles.xml. <item name="android:windowIsTranslucent">true</item> <item name="android:windowBackground">@android:color/transparent</item>

    Refer to: https://stackoverflow.com/questions/2176922/how-do-i-create-a-transparent-activity-on-android

    Tuesday, January 7, 2020 10:25 AM
  • User337237 posted

    I put the following two lines:

    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@drawable/ic_app_background_image</item>
    

    Instead of:

    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    

    Because putting <item name="android:windowBackground">@android:color/transparent</item> instead of <item name="android:windowBackground">@drawable/ic_app_background_image</item> makes the background image dissapear. I got the following result:

    The wallper of the telephone becomes visible.

    Saturday, January 11, 2020 8:21 AM
  • User382871 posted

    If you've solved the issue, please mark the solution as the answer. It'll help others who face the similar problem.

    Monday, January 13, 2020 7:20 AM