Android: Change Background Color of Fragment

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);

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>

Get the fragment object like:

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

Change it's background like:

fragment.getView().setBackgroundColor(Color.WHITE);