Question

I've already posted a related question but was partially solved, so here I'll show you the whole code.

The problem is that I can't set a background from a RelativeLayout in white, for example, and set simultaneously, by java code, a background resource (.PNG file) and merge them.

The .PNG image is a prototype of part of the game screen and has transparent space. What I want to obtain is to show this background in white, because of there are details in black that can't be seen because my pre-established background is black (initial theme selected I suppose).

The code below corresponds to the XML file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/id_act_principal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"
    android:keepScreenOn="true"
    tools:context=".Principal" >

And the .java file:

@Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);

        RelativeLayout fondo = (RelativeLayout) findViewById(R.id.id_act_principal);
        fondo.setBackgroundResource(R.drawable.prototipoestructurapantalla);

        }

The .java file sets the background image correctly, but in the XML file is set to show the white background and it doesn't show it. It keeps being black.

I hope you can help me.

Was it helpful?

Solution

Try this:
Create a drawable named image_with_white_backgroud.xml:

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <shape >
            <solid android:color="@android:color/white"/>
        </shape>
    </item>
    <item>
        <bitmap
            android:src="@drawable/prototipoestructurapantalla"/>
    </item>
</layer-list>  

In your RelativeLayout replace android:background="@android:color/white" with android:background="@drawable/image_with_white_backgroud"

OTHER TIPS

you can only have one background at a time, try leaving the relativelayout as it is, and insert an ImageView for the png.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top