Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change opacity of background image?

Tags:

android

Is there any way that I could change the opacity of the background image that I set on the XML file? Also, I wanted to justify the text that I'm going to put in the xml file, however, as I researched for this same issue. Android studio doesnt support justify text yet. However, I tried to change to webview instead of textview however I'am having a lot of errors calling it to my main activity java since I used case statement to call the layouts and the java file of the webview is not fragment but rather activity. Your response would be much appreciated. Thank you

Here is the XML file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/minnions1"
android:id="@+id/minnions1"
>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Safe and Responsible usage of Computer,Internet and Email"
    android:textColor="@android:color/black"
    android:textSize="20sp"
    android:id="@+id/title1"
    android:layout_marginTop="125dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity ="center"
    android:fillViewport="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="100dp"
    android:id="@+id/scrollView"
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textColor="#000"
        android:textColorLink="@android:color/black"
        android:textSize="18sp"
        android:text="Using facilities and equipments should always be safe and useful in Information and Communication Technology (ICT)
like computer, email, and internet. It is important for us to study the guides and safe usage of computer, internet
and email in schools.\n \n"
        android:drawablePadding="5dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="20dp"
        android:layout_below="@+id/content1"
        android:layout_centerHorizontal="true"
        />
</ScrollView>
</LinearLayout>

Also, Here is the java file

package com.android.pet.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.doepiccoding.navigationdrawer.R;

public class Lesson1 extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.onepointone, null);
        return rootView;
    }
}
like image 832
Dreamer Avatar asked Mar 30 '16 12:03

Dreamer


2 Answers

Try This

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

here backgroundimage is for your understanding you can replace your layout which background you want to set opacity

like image 171
Abhishek Patel Avatar answered Sep 18 '22 22:09

Abhishek Patel


You can change it programatically using setAlpha(50) method and in xml you can use android:alpha="0.5" 1 is max so 0.5 means half transparent .

You can also change your design a little bit. Instead of doing this , do this

<FrameLayout>
<ImageView
    android:background="@drawable/background" 
    android:alpha="0.3" />
<LinearLayout>
//Same as before
</LinearLayout>

Make framelayout your parent and put your linear layout as a child and rest will remain the same . Give your imageview width of height of match parent and that's it.

like image 35
Syed Muhammad Oan Avatar answered Sep 18 '22 22:09

Syed Muhammad Oan