Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Set a background color and a translucent background image on the Activity

I have an Android activity which has three buttons. I have set a color for the main activity LinearLayout's background via:

android:background="@color/homeBgColor"

I want to put up a translucent background image behind the buttons on the activity. I tried using an ImageView, but it pushes the buttons down.

Is there any way to set the background color as well as image for the activity, like we do in CSS?

#mydiv{ backround: #262626 url("link-to-my-img.png");}

Thanks

like image 536
Bilbo Baggins Avatar asked Dec 22 '12 01:12

Bilbo Baggins


People also ask

HOW DO YOU MAKE A activity translucent?

android:windowIsTranslucent indicates weather the window in which the activity is present in translucent state or not. It must be a boolean value i.e. it can be true or false. android:windowBackground is used to set the background of the main window. Here, we are setting the background as transparent.


1 Answers

You should be able to achieve this with a <layer-list /> drawable. Within it, place an <item /> which contains a <bitmap /> (example stolen from the developer docs):

<item>
    <bitmap android:src="@drawable/image"
            android:gravity="center" />
</item>

Then, your other item can just be a solid color. Make a drawable resource that just has that solid color in it, then combine them:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/your_color" />
    <item>
        <bitmap android:src="@drawable/image"
                android:gravity="center" />
    </item>
</layer-list>

(You may have to flip the order of the <item /> tags, I forget which is on top.)

like image 59
Cat Avatar answered Sep 24 '22 06:09

Cat