5

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...

faerubin
  • 177
  • 12
  • your rand may be greater than images array size , make sure that it is between 0 and 3 – Asmaa Rashad Oct 09 '16 at 16:05
  • @AsmaaRashad Isn't that what `rand.nextInt(images.length)` gives me? – faerubin Oct 09 '16 at 16:06
  • Check here -- http://stackoverflow.com/questions/12523005/how-set-background-drawable-programmatically-in-android – Tasos Oct 09 '16 at 16:12
  • check that answer for generating random number in range http://stackoverflow.com/a/21049922/5470794 – Asmaa Rashad Oct 09 '16 at 16:14
  • @AsmaaRashad, what that answer says is "yes, that's exactly what `rand.nextInt(images.length)` gives me." @Tasos, I've read it several times already, but I'll admit I haven't tried every last suggestion there - some are deprecated and some need a minimal API higher than my app defined. I'd still appreciate any input on why the obvious way isn't working. – faerubin Oct 09 '16 at 16:21
  • @AsmaaRashad Random.nextInt(n) returns a random int in range[0, n-1] – SpiralDev Oct 09 '16 at 16:21
  • There is another easy way to generate a random image as you have named them 1,2,3 eg (final String str = "img_" + rnd.nextInt(2);) -- and to set (getResources().getDrawable(getResourceID(str, "drawable", getApplicationContext()))) --- check here http://stackoverflow.com/questions/20549705/how-to-display-random-images-on-image-view – Tasos Oct 09 '16 at 16:23
  • From your EDIT: -- what does the Logcat say?? have you checked that?? – Tasos Oct 09 '16 at 16:28
  • @Tasos, I honestly only check the logcat when the app crashes, which is doesn't. What should I be looking for there? – faerubin Oct 09 '16 at 16:29
  • The Logcat also shows Warnings -- just monitor it and see if it shows you anything suspicious when you change the image – Tasos Oct 09 '16 at 16:30
  • @Tasos, it's too much to copy to a comment, but from what I can tell, there's nothing in the logcat about the image. – faerubin Oct 09 '16 at 16:37
  • In AS -- To filter just for the App in the search box add the name of your package eg (com.nyapplicaionname) -- you get less output – Tasos Oct 09 '16 at 16:39
  • @Tasos, it already is filtered just for the app. – faerubin Oct 09 '16 at 16:40
  • then im not sure -- the random image is created ok and its in the drawable folder so you should be good to go as you say -- Add the (R.layout.activity_my) xml to your Question, looks like the problem is there – Tasos Oct 09 '16 at 16:44
  • I think you need to set the inflater to true -- eg - (FrameLayout) inflater.inflate(R.layout.activity_my, true);) --- check here https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/ – Tasos Oct 09 '16 at 16:56

2 Answers2

2

Don't get view via LayoutInflater, if your Activity has xml layout and you called setContentView(int resId) you just find your root view and set background.

FrameLayout layout = (FrameLayout) findViewById(...);
layout.setBackgroundResource(images[rand.nextInt(images.length)]);

If you want to get view via LayoutInflater :

LayoutInflater inflater = getLayoutInflater();
FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.activity_my, null);
layout.setBackgroundResource(images[rand.nextInt(images.length)]);
setContentView(layout);
Hamed Nabizadeh
  • 527
  • 4
  • 9
0

Try this:

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

final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    layout.setBackgroundDrawable(getResources().getDrawable(images[rand.nextInt(images.length)]));
} else {
    layout.setBackground(getResources().getDrawable(images[rand.nextInt(images.length)]));
}
SpiralDev
  • 7,011
  • 5
  • 28
  • 42
  • Thanks, but I've tried that already, and I still get a black background. – faerubin Oct 09 '16 at 16:23
  • @faerubin I edited the answer. Try that one too. Hope it helps! – SpiralDev Oct 09 '16 at 16:31
  • I'll edit my original question to clarify this, but for your benefit - I defined my minimum API level to 15, and the logic under `else` requires min of 16, so my code doesn't compile. – faerubin Oct 09 '16 at 16:32