I want when my application starts, an image appears, and then open the application itself. Similar to the YouTube app, when you start, an image of the YouTube symbol appears and then opens the app.
I want when my application starts, an image appears, and then open the application itself. Similar to the YouTube app, when you start, an image of the YouTube symbol appears and then opens the app.
This feature you are looking for is called SplashScreen
.
First, create a layout with the image you would like to appear at the beginning. (Remembering that it should be in the Drawable folder)
You will get something like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/imagem_splash" />
</LinearLayout>
You should now create a class so that we can set the time this layout will run.
Notice in the example below that we set a int
to how long the screen will stay open. After that, we have the run
method where we define what the Activity call will be after the end of the time you defined previously.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends Activity {
//Define tempo que a tela vai exibir. (tempo em milisegundos)
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//Método que será executado uma vez.. Na abertura do app.
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}
Finally, we can not forget to go in AndroidManifest.xml
and create our new Activity (Splash) there.
You will have something like:
<activity
android:name="br.com.splashscreen.SplashScreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
// time private long splashRetraso = 2000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tlsplah);
// code that does what you want
TimerTask task = new TimerTask() {
@Override
public void run() {
Intent NovoLaytout;
NovoLaytout = new Intent(Tlsplah.this,MainActivity.class);
startActivity(NovoLaytout);
Tlsplah.this.finish();
}
};
Timer timer = new Timer();
timer.schedule(task,splashRetraso);
}
}