Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ImageView as background for an Android Layout

I want to take advantage of the scaleType property, which I cannot use if I set my image using the background property on the LinearLayout. I've tried using a LinearLayout with 2 children: the first is an ImageView (for the background image) and the second child is another LinearLayout containing TextViews, Buttons, etc. But the result is just the image followed by the other views.

Is it possible to float the nested LinearLayout over the ImageView somehow? I know that there's lots of variations on this question around, but I haven't found anything that's helped me yet. If I've missed something, please point me in that direction. Also, I'd like to do this in XML if possible, and I'm flexible about what type of layout I can use (it doesn't have to Linear).

like image 547
RTF Avatar asked Jan 05 '14 00:01

RTF


People also ask

What is the use of ImageView in Android?

ImageView class is used to display any kind of image resource in the android application either it can be android. graphics. Bitmap or android. graphics.

Can ImageView be used as button?

We will be building a simple application in which we will be displaying an ImageView and when we click on that ImageView we will get into a new activity or simply we can say that we are going to use ImageView as a button to switch between different activities.

Is ImageView clickable in Android?

To make a View clickable so that users can tap (or click) it, add the android:onClick attribute in the XML layout and specify the click handler. For example, you can make an ImageView act like a simple Button by adding android:onClick to the ImageView . In this task you make the images in your layout clickable.


1 Answers

I did it thusly using a RelativeLayout as the parent:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#000000" >

    <ImageView
        style="@style/BackGroundImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp"
        tools:ignore="ContentDescription" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <!--Rest of widgets......-->

    </LinearLayout>

</RelativeLayout>

My Style for it:

<style name="BackGroundImageView">
    <item name="android:alpha">0.5</item>
    <item name="android:scaleType">fitXY</item>
    <item name="android:src">@drawable/img_background</item>
</style>

I used a style, because the alpha setting doesn't work on lower APIs (can't remember what the limit is offhand).

like image 183
Rick Falck Avatar answered Oct 12 '22 02:10

Rick Falck