codeme2020
codeme2020

Reputation: 853

Android: Having Background as an Image?

For my Android application I wish to have the background as an image, however all of the images I use are very intense and take away from the the Textviews etc on the app.

Is there a way to fade a background image so that is it provides an aesthetic background without taking away from the app's main components?

Current XML code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:background="@drawable/loginbackgroundfour">

    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/linearlayout_bg"
        android:padding="10dp"
        >

        <Button 
            android:id="@+id/btnSignUp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_margin="4dp"
            android:text="Sign Up"
            android:background="@drawable/button_default_bg"
            style="@style/DefaultButtonText"
           />
        <Button 
            android:id="@+id/btnSingIn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_margin="4dp"
            android:text="Sign In"
            style="@style/DefaultButtonText"
            android:background="@drawable/button_default_bg"
           />

        <Button 
            android:id="@+id/btnGameRegister"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_margin="4dp"
            android:text="Register Game"
            style="@style/DefaultButtonText"
            android:background="@drawable/button_default_bg"
           />


        </LinearLayout>

</RelativeLayout>

Upvotes: 0

Views: 83

Answers (2)

Karim
Karim

Reputation: 776

Also you can set alpha in layout:

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/main"
                android:alpha="0.5" />

In your case you can change your layout to be like that:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
        <FrameLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="fitXY"
                    android:src="@drawable/loginbackgroundfour"
                    android:contentDescription="@string/text" 
                    android:alpha="0.5"/>
             </FrameLayout>
<LinearLayout 
    android:orientation="vertical"
<!-- rest of your layout !-->

Upvotes: 2

Lal
Lal

Reputation: 14810

You can use setAlpha()

See the docs

Try this in your Java class

View backgroundimage = findViewById(R.id.background);
Drawable background = backgroundimage.getBackground();
background.setAlpha(80);

Alpha Values 0-255, 0 means fully transparent, and 255 means fully opaque

Upvotes: 1

Related Questions