Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Change Background Color of Fragment

I tried changing the background color of a fragment, but a small problem occurred.

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

So, shown above is the code I had for my main class that calls the XML file for the fragment.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.northreal.practice.FirstFragment"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#CBA" />

</LinearLayout>

Above is the main.xml layout that is called by the main class (MainActivity).

public class FirstFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.main, parent, false);
    }
}

Above the XML file with the fragment calls this class.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
     >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BLAHHHH"
        android:layout_gravity="center_vertical" />

</LinearLayout>

This layout above is inflated by the class FirstFragment

So, why doesn't this actually change the color of the background of my fragment?

like image 313
Brandon Ling Avatar asked Jul 27 '12 16:07

Brandon Ling


4 Answers

Fragments don't inherit from View and thus don't have a set background method.

Any easy fix is to just grab the root view of fragment and set its background

fragment.getView().setBackgroundColor(Color.WHITE);
like image 76
Adam Avatar answered Oct 18 '22 22:10

Adam


The reason why that doesn't work is because fragment is treated similar to an activity. It is a container that is the child of the main activity, and is used to display other items.

You need to put the android:background="#CBA" in the actual layout that holds the TextView and NOT the fragment itself. Like so:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#CBA"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="BLAHHHH" />
</LinearLayout>
like image 45
Brandon Ling Avatar answered Oct 18 '22 22:10

Brandon Ling


Get the fragment object like:

Fragment fragment = (Fragment) getFragmentManager().findFragmentById(R.id.fragmentId);

Change it's background like:

fragment.getView().setBackgroundColor(Color.WHITE);
like image 4
el2e10 Avatar answered Oct 18 '22 23:10

el2e10


In AndroidX you can use a FragmentContainerView instead of a Fragment to set a background:

<androidx.fragment.app.FragmentContainerView
    ...
    android:background="#FFFFFF"/>
like image 3
nh7 Avatar answered Oct 18 '22 22:10

nh7