Question

I wanted my app to had a fading picture first before going into another page using threads. Below is the code i used and it worked well for me. However, it stops in the white page at the end of the thread. What will i do so that it will go to the next activity without clicking on anything? Or after the page turns white, what code should i use so that it will go now to the next activity?

package com.kfc;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.*;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class Intro extends Activity {
    LinearLayout screen;
    Handler handler = new Handler();
    int i;
    Intent intent;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.introxml);

        screen = (LinearLayout) findViewById(R.id.myintro);

        (new Thread(){
            @Override
            public void run(){
                for(i=0; i<255; i++){
                    handler.post(new Runnable(){
                        public void run(){
                            screen.setBackgroundColor(Color.argb(255, i, i, i));
                        }
                    });
                    // next will pause the thread for some time
                    try{ sleep(10); }
                    catch(Exception e){ break; }
                }
                for(i=255; i>0; i--){
                    handler.post(new Runnable(){
                        public void run(){
                            screen.setBackgroundColor(Color.argb(255, i, i, i));
                        }
                    });
                    // next will pause the thread for some time
                    try{ sleep(10); }
                    catch(Exception e){ break; }
                }
                startActivity(new Intent(Intro.this,NewKFCActivity.class));
            }
        }).start();
    }
}
Was it helpful?

Solution

After the for loop exits. Add the code to start a new activity.

startActivity(new Intent(Intro.this,NewACtivity.class));

You need to put it outside the for loop. If you put it after start method, it will execute before the thread is completed. You may also need to scope the 'this variable' using Intro.this. Also remember to add the new activity in the manifest file as

<activity android:name=".NewActivity"/>

Just use this

screen = (FrameLayout) findViewById(R.id.layout);
(new Thread(){
    @Override
public void run(){
    for(i=0; i<255; i++){
        handler.post(new Runnable(){
            public void run(){
                screen.setBackgroundColor(Color.argb(255, i, i, i));
            }
        });
        // next will pause the thread for some time
        try{ sleep(100); }
           catch(Exception e){ break; }
        }
        startActivity(new Intent(TabTester.this,NewKFCActivity.class));
    }
}).start();

this pointer should point to the Intro activity object. But inside the thread, this will refer to the the current thread object(am not sure to what it point exactly) so you need to scope it using 'Intro.this' which mean 'use this that points to the Intro activity'

You background picture will be overwritten when you use setBackgroundColor to the same view. A way to do this will be to use to layouts, the outer layout will have the background picture and the inner layout will be the one that has setBackgroundColor applied to. Eg:

You also need to alter the code

screen.setBackgroundColor(Color.argb(255, i, i, i));

to

screen.setBackgroundColor(Color.argb(120, i, i, i));

The alpha value is set to 255 which means opaque and will not show the background image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top