3

This is my first question with SO.

I've just used RecyclerView with a GridManagerLayout of CardViews. Now it all works perfectly with the CustomAdapter and all, but I want to set a background Image behind those CardViews so it's not so bare behind them.

Now I tried setting the background Image to the Fragment using it, the XML layout (FrameLayout) that holds the RecyclerView and then tried the same with the RecyclerView itself.

It's killing me to try and set an image as I'm set on doing it since it seems to be tricky.

Is there any way that it can be done?

Edit:

The layout holding the RecyclerView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context="fragments.SpaceFlightFragment">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>

The method that creates the View in the fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_spaceflight, container, false);

    RecyclerView recyclerView = (RecyclerView)v.findViewById(R.id.recyclerView);

    SpaceFlightAdapter spaceFlightAdapter = new SpaceFlightAdapter(missions);

    recyclerView.setAdapter(spaceFlightAdapter);

    recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
    return v;
}

Now I tried setting the background image in the XML layout to all the tags, LinearLayout, FrameLayout and the RecyclerView.

I've also tried to Dynamically add the image to them in the OnCreateView method.....still nothing :/.

4
  • this should work, what do you see when you render the xml while developing. Oct 25, 2015 at 14:03
  • Tell me about it haha. Like there is no reason why it shouldn't...on paper it's the same way anything gets a background image. When the fragment gets created and the xml renders, the content I have being created from the Adapter is up and everything is dandy. But no image in the background.
    – SmiffyKmc
    Oct 25, 2015 at 14:05
  • well do you see the background when you see the render of your xml, in the render mode (preview mode) ? Oct 25, 2015 at 14:07
  • Oh sorry. Yes! It shows inside the IDE xml render feature. But when tested on the device...nothing.
    – SmiffyKmc
    Oct 25, 2015 at 14:08

3 Answers 3

3

Found the answer. Was a silly mistake! Was doing everything right, but the image was a bit to big and didn't get created. So tested a smaller image to chance it and it worked.....should of tried another image before asking on SO. Was expecting the OutOfMemory exception if it was a big picture problem. Thanks though to david145 and hars.

1

Wrapping all of it into a LinearLayout and setting it's background works great for me:

<LinearLayout android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_light">

        <FrameLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="4dp"
                android:paddingBottom="8dp"
                android:clipToPadding="false"/>

        </FrameLayout>

</LinearLayout>

You can also use a FrameLayout instead of the LinearLayout.

3
  • Thanks for checking it out right away man :)! So I tried what you said and it comes up in the Android Studio layout preview like the ways I tried above. But once tested on the device, nothing. Still bare white behind the CardLayouts that are populated on it....shame though :(
    – SmiffyKmc
    Oct 25, 2015 at 13:43
  • Can you post some code in your opening post, so we get a better overview. Are you sure you have set the right layout params and also using a valid background image?
    – dabo248
    Oct 25, 2015 at 13:47
  • Just edited it there with two areas in which I tried to add the image. Thanks for looking over it :)
    – SmiffyKmc
    Oct 25, 2015 at 13:59
0

i know it should work the way you are doing but can you try a hack by adding a imageview inside your FrameLayout with a background

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/background"
   tools:context="fragments.SpaceFlightFragment">

<FrameLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ImageView
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/background"
 />
<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</FrameLayout>

</LinearLayout>
3
  • I found out the problem, real silly mistake but learned something. I know the way you are thinking alright but managed to get it up in the end with a smaller picture with less bits :)
    – SmiffyKmc
    Oct 25, 2015 at 14:14
  • that is a pointer i can take while setting background images with large size.thanks. Oct 25, 2015 at 14:16
  • No problem at all :). Even the smallest of things help!
    – SmiffyKmc
    Oct 25, 2015 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.