I have a fullscreen activity, for which I want to programmatically set the background. I have four different images in my drawable folder, and each time the activity is created, I want to randomly choose one for the background. Here is my code:
LayoutInflater inflater = getLayoutInflater();
FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.activity_my, null);
int[] images = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
Random rand = new Random();
layout.setBackgroundResource(images[rand.nextInt(images.length)]);
Here is the XML file:
<FrameLayout 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"
tools:context="com.example.mkessler.MyApp.MyActivity">
<TextView
android:id="@+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:id="@+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="@color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent">
<Button
android:id="@+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Unimportant text"
android:onClick="someFunction"/>
</LinearLayout>
</FrameLayout>
However, when I run the app on my phone, the background is just black. The same happens when I try to set it using a specific one of the images. Needless, when I set it from the XML file it works fine.
EDIT: Just to clarify, even when I try to set it to a given image programmatically and not though the XML file, I get a black background. I think focusing on the random aspect of the question isn't going to get anywhere.
EDIT #2: Minimum API of my project set to 15. Don't know if this is relevant, but in case anyone thinks it matters...