34

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?

Sam Vloeberghs
  • 1,082
  • 5
  • 18
  • 29
Brandon Ling
  • 3,841
  • 6
  • 34
  • 49

5 Answers5

50

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);
Adam
  • 516
  • 5
  • 3
  • 2
    Thanks that solved my problem but instead of getView() I used getRootView(): fragment.getRootView().setBackgroundColor(Color.WHITE); – zarej Apr 19 '13 at 10:05
  • I am getting a NullPointerException when I try this on a PreferenceFragment =s – Solace Jul 09 '15 at 19:42
  • @Solace - I think that means it isn't yet part of the visual tree. Try setting it in an override that is called later. – ToolmakerSteve Jun 28 '18 at 20:34
10

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>
Brandon Ling
  • 3,841
  • 6
  • 34
  • 49
4

Get the fragment object like:

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

Change it's background like:

fragment.getView().setBackgroundColor(Color.WHITE);
el2e10
  • 1,518
  • 22
  • 22
3

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

<androidx.fragment.app.FragmentContainerView
    ...
    android:background="#FFFFFF"/>
nhcodes
  • 1,206
  • 8
  • 20
0

I have faced the same problem but my requirement was to set or change background drawable image of fragment. As Adam answered the approach is right and I would like to show the connection from fragment to activity.

The best practice is to use an interface named 'Connector'(or any name).Then from your fragment:

Connector connector = (Connector) getActivity();
connector.setIt(this);

Then in your activity which will implement 'Connector' interface and have the method.

@Override
public void setIt(Fragment fragment){
    FirstFragment firstFrag = (FirstFragment) getSupportFragmentManager().findFragmentByTag("first");
    firstFrag.getView().setBackgroundDrawable(getResources().getDrawable(R.drawable.app_background));
    //or for color set
    firstFrag.getView().setBackgroundColor(Color.WHITE);
}
Asad
  • 1,260
  • 13
  • 19