-1

I have this code that changes the background when you click a button however, I want to change the background every 10 seconds, and I want to switch between files img1.png, img2.png and img3.png and when the cycle is completed start all over again. Thanks in advance. Here is the code: In 'MainActivity.java'

package lucas.app_2001;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    Button button;
    LinearLayout mainLayout;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainLayout=(LinearLayout)findViewById(R.id.myLayout);
        button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                mainLayout.setBackgroundResource(R.drawable.yellowgradient);
            }
        });
    }
}

'MainActivity.xml':

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="64dp"
        android:layout_marginTop="71dp"
        android:text="Shout!" />

</LinearLayout>
Cœur
  • 37,241
  • 25
  • 195
  • 267
lucas rodriguez
  • 980
  • 2
  • 8
  • 20
  • take a look at this SO question: [How to set background drawable programmatically...](http://stackoverflow.com/questions/12523005) – Bö macht Blau Nov 05 '15 at 17:49

2 Answers2

0

Use a while loop to repeat.

To delay it for 10 seconds try something like this:

//wait 10 seconds
        Button.postDelayed(new Runnable() {

            @Override
            public void run() {
                Button.setClickable(true);                        
            }
        }, 10000);
0

You can do this way:

private Handler mHandler;
private Runnable mRunnable;
private int i = 0;

MainActivity.java:

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    Button button;
    LinearLayout mainLayout;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainLayout=(LinearLayout)findViewById(R.id.myLayout);
        button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                mainLayout.setBackgroundResource(R.drawable.yellowgradient);
            }
        });

        mHandler = new Handler();
        mRunnable = new Runnable(){
        @Override
        public void run() {
             i++;
             if(i==1){
             mainLayout.setBackgroundResource(R.drawable.image_one);
              }else if(i==2){
             mainLayout.setBackgroundResource(R.drawable.image_two);
              }else if(i==3){
             mainLayout.setBackgroundResource(R.drawable.image_three);
               i ==0;
              }

             mHandler.postDelayed(mRunnable , 10000);
            }
         }; 
        mHandler .post(mRunnable);
    }
}

on onStop():

if(mHandler!=null){
  mHandler.removeCallbacks(mRunnable);
}

Done

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151