How to build a SplashActivity in Android

How to build a SplashActivity in Android
 Step1: create new Activity :"SplashActivity" > inset code1 on SplashActivity.java
 Step2: in /res/layout/activity_splash.xml:
 + insert code splash (just have image)
 + in Preview: change AppThem > NoTitleBar
 Step3: in /manifests/AndroidManifest.xml:
 + in activity1:
 Change <activity android:name=".SplashActivity"> to <activity android:name=".MainActivity">
 + in activity2:
 Change <activity android:name=".MainActivity"> to <activity android:name=".SplashActivity">
 Step4: run

////////////////////////////////////////////////////////////////////////////////////

public class splash extends AppCompatActivity {

 @Override public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.splash);
 Thread mythread =new Thread(){
 @Override
 public void run(){
 try {
 sleep(3000);
 Intent intent=new Intent(getApplicationContext(),MainActivity.class);
 startActivity(intent);
 finish();
 } catch (InterruptedException e) {
 e.printStackTrace();

 }
 }
 };
 mythread.start();

 }
 @Override
 public boolean onTouchEvent(MotionEvent event)
 {

 boolean defaultResult = super.onTouchEvent(event);

 switch (event.getAction()){
 case MotionEvent.ACTION_DOWN:
 Intent intent = new Intent(getApplicationContext(), MainActivity.class);
 startActivity(intent);
 break;
 }
 return defaultResult;
 };

}
///////////////splash.xml//////////////////////////////////////////////////

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:padding="1dp"
 tools:context="splash" >
 <ImageView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 app:srcCompat="@drawable/r17"
 android:scaleType="centerCrop"
 android:id="@+id/splashImageView" />

</RelativeLayout>

Code1:
 @Override public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.splash);
 Thread mythread =new Thread(){
 @Override
 public void run(){
 try {
 sleep(3000);
 Intent intent=new Intent(getApplicationContext(),MainActivity.class);
 startActivity(intent);
 finish();
 } catch (InterruptedException e) {
 e.printStackTrace();

 }
 }
 };
 mythread.start();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s