Add background image to individual fragments
To add background to fragment, you have to wrap it in some container
<LinearLayout
android:id="@+id/linearlayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ccc"
android:layout_weight="1"
android:orientation="vertical">
<fragment android:name="com.example.simplefragmentexample.LayOutOne"
android:id="@+id/frag_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
If you wish to use same xml file, you should set images programmatically
LinearLayout l = (LinearLayout) findViewById(R.id.linearlayout01);
l.setBackground(Image);
or use several xmls with android:background
.
Ryan Sayles
Updated on June 04, 2022Comments
-
Ryan Sayles almost 2 years
I have an app with multiple
fragments
and I would like to know how to add a background that is different for eachfragment
. The layout I am using has scrollable tabs which all use the same xml file. I also have aMainActivity
that sets the view and anadapter
for eachfragment
. I know you can add a background using the xml file withandroid:background
or something of the sort as well as setting it to the view in the main activity but I can't figure out how to do it to each tab. Thank you for any help! -
Nazerke over 10 yearsHey Andrea,thanks for the answer. Could you please have a look at my question stackoverflow.com/questions/21181193/… I'm having trouble with setting background image, when I do f.getView().setBackgroundResource(R.drawable.image) it doesn't change, similarly when i do f.getView().getBackground() it returns null.