Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6577631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:39:41+00:00 2026-05-25T15:39:41+00:00

I have a problem, I’m trying to get my apps background to change, each

  • 0

I have a problem, I’m trying to get my apps background to change, each ten seconds, based on a timer… I have done what i could and cant work it out as im a beginner to java and programming 🙂 I would love if someone could just correct my code please 😉 (I can packgage it into a phone etc eclipse doesnt show an error, but my app force closes, when the timer goes), here it is:

public class CookBookActivity extends Activity {
    /** Called when the activity is first created. */

    private static final long GET_DATA_INTERVAL = 10000;
    int images[] = {R.drawable.smothie1,R.drawable.omletherb1};
    int index = 0;
    ImageView img;
    Handler hand = new Handler();
    private LinearLayout layout;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
        layout = (LinearLayout)findViewById(R.id.linearLayout1);
        hand.postDelayed(run, GET_DATA_INTERVAL);
    }

    Runnable run = new Runnable() {
        public void run() {
            layout.setBackgroundDrawable(getDrawable(images[index++]));
            if (index == images.length)
                index = 0;
            hand.postDelayed(run, GET_DATA_INTERVAL);


        Typeface tf2 = Typeface.createFromAsset(getAssets(),
                "fonts/BPreplay.otf");
        TextView tv2 = (TextView) findViewById(R.id.textView2);
        tv2.setTypeface(tf2);


        Typeface tf = Typeface.createFromAsset(getAssets(),
                "fonts/BPreplay.otf");
        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setTypeface(tf);


        Button mainNext = (Button) findViewById(R.id.nextScreen1);
        mainNext.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent i = new Intent();
                i.setClassName("com.unKnown.cookbook", "com.unKnown.cookbook.screen1");
                startActivity(i);

            }
        });
        }
    };

    protected Drawable getDrawable(int i) {
        // TODO Auto-generated method stub
        return null;
    }
}

EDIT:

Now I have finnaly solved my problem and my image sets to background (thanks to @Yashwanth Kumar ‘s help and me 🙂 ), Its almost fine now, but now my it only sets one image as background (each ten seconds it sets the same image), I think it’s down to either of two of the following things:

either:

-handler stops (whcich i doubt)- I have now confirmed it works and every swecond it does the procedure, so it’s down to second issue

or:

it only uses the first image from the list (R.drawable.omletherb1), in which case I’ll have to set something like if R.Drawable.zzz is set then do set image R.drawable.ccc

Please tell me what you think, and here is tyhe code I have now ended up with:

public class CookBookActivity extends Activity {
/** Called when the activity is first created. */

private static final long GET_DATA_INTERVAL = 1000;
int images[] = {R.drawable.omletherb1,R.drawable.smothie1};
int index = 0;
LinearLayout img;
Handler hand = new Handler();
private LinearLayout layout;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);
    layout = (LinearLayout)findViewById(R.layout.main);
    hand.postDelayed(run, GET_DATA_INTERVAL);

    Typeface tf2 = Typeface.createFromAsset(getAssets(),
            "fonts/BPreplay.otf");
    TextView tv2 = (TextView) findViewById(R.id.textView2);
    tv2.setTypeface(tf2);


    Typeface tf = Typeface.createFromAsset(getAssets(),
            "fonts/BPreplay.otf");
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setTypeface(tf);


    Button mainNext = (Button) findViewById(R.id.nextScreen1);
    mainNext.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent();
            i.setClassName("com.unKnown.cookbook", "com.unKnown.cookbook.screen1");
            startActivity(i);

        }
    });
}

Runnable run = new Runnable() {
    public void run() {
        layout.setBackgroundDrawable(getDrawable(images[index++]));
        if (index == images.length)
            index = 0;
        hand.postDelayed(run, GET_DATA_INTERVAL);

    }
};

protected Drawable getDrawable(int i) {
    // TODO Auto-generated method stub
    return getResources().getDrawable(images[i%2]);
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:39:42+00:00Added an answer on May 25, 2026 at 3:39 pm
    layout.setBackgroundDrawable(getDrawable(images[index++]));
    
    protected Drawable getDrawable(int i) {
        // TODO Auto-generated method stub
        return null;
    }
    

    This is the problem, you are setting null to the background.return some valid drawable and it will work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem when I try to set a background to .header - nothing
I have problem with Razor syntax and jQuery, I'm trying to append some items
I have problem with configuring NODE.JS. I was trying to install node.js with this
I have problem with types in my schema when trying to use xsd:any element
i have problem with build my own webservice based on cxf/camel. My webservice should
I have problem witch change style border-left-color in next button, if main button is
I have problem with my timer. In my game view in viewDidLoad I have:
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.