0

Hi I'm kind of beginner to android OS programming, and I got stuck with a problem, I cant figure out how to do a dynamic background, based on timers (say each 10 seconds a background changes to a different one) I have some code but it comes up with error, here's a sample:

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

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

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

Any help would be greatly apprieciated :D thanks

EDIT: The errors I get are on this line:

layout.setBackgroundResource(LinearView1).getDrawable(images[index++]);

It says that:

-layout cannot be resolved

-the method getDrawable(int) is undefined for the type Object


This error:

layout.setBackgroundResource(LinearView1).getDrawable(images[index++]);

It says that:

-layout cannot be resolved

-the method getDrawable(int) is undefined for the type Object

Please help :)

Farshid Shekari
  • 2,391
  • 4
  • 27
  • 47
Bercik
  • 139
  • 1
  • 14
  • read below please :D I put it as an aswer as, this doesnt seem to accept code ;) – Bercik Sep 11 '11 at 15:39
  • @Albert: if you have extra information to add to your question, please edit your question instead of adding an answer. What you've added isn't an 'answer' because it doesn't answer your question. – Luke Woodward Sep 11 '11 at 18:03
  • Yeah true, I'm new to this site and I'm still learning to use it :) – Bercik Sep 11 '11 at 20:19
  • [enter link description here][1] [1]: http://stackoverflow.com/questions/7408920/dynamic-background-on-linearlayout-what-is-my-error/7408971#comment-8953318/%20%22Link%20to%20an%20answer%20to%20this%20thread – Bercik Sep 14 '11 at 21:39

1 Answers1

1

I have finally worked it out, after removing a few errors I have came up with this (and its working) :

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

}

Bercik
  • 139
  • 1
  • 14