How to show screen saver screen for 3 seconds on Android?

I would like the splash image to start and remain for 3 seconds, and then disappear and continue or be replaced by the rest of the layout in main.xml.

This is my code:

Main.java

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); ImageView splash = (ImageView) this.findViewById(R.id.splash); 

main.xml

 <?xml version="1.0" encoding="utf-8"?> <!-- margin=0px, padding=20px --> <!--textview padding=10dp, textSize=16sp--> <!--px=pixel, dp=density indepen sp=scale indepen fontsize preference --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/splash" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/splash2"/> <ImageView android:id="@+id/myImageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bg_main"/> <ImageView android:id="@+id/myImageView0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bar_top"/> <!-- <TextView android:id="@+id/textItem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:paddingTop="10dp" android:paddingLeft="110dp" android:background="#00000000" android:textColor="#ffffffff" android:textSize="22sp" android:text="Find Car" android:enabled="false" > --> <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="3dp"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" /> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignBottom = "@android:id/tabcontent" /> </RelativeLayout> </TabHost> </RelativeLayout> 
+7
source share
10 answers

You can do it

 ImageView splash = (ImageView) this.findViewById(R.id.splash); splash.postDelayed(new Runnable(){ splash.setVisibility(View.GONE); }, 3000); 

Or perhaps add some animation by calling this method (from Android docs) instead of setting the visibility directly to GONE.

 private void fadeSplashOut() { // Set the content view to 0% opacity but visible, so that it is visible // (but fully transparent) during the animation. mContentView.setAlpha(0f); mContentView.setVisibility(View.VISIBLE); // Animate the content view to 100% opacity, and clear any animation // listener set on the view. mContentView.animate() .alpha(1f) .setDuration(mShortAnimationDuration) .setListener(null); // Animate the loading view to 0% opacity. After the animation ends, // set its visibility to GONE as an optimization step (it won't // participate in layout passes, etc.) splash.animate() .alpha(0f) .setDuration(mShortAnimationDuration) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { splash.setVisibility(View.GONE); } }); } 
+15
source
 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //Sets the layout of welcome_screen.xml setContentView(R.layout.welcome_screen); Thread timer= new Thread() { public void run() { try { //Display for 3 seconds sleep(3000); } catch (InterruptedException e) { // TODO: handle exception e.printStackTrace(); } finally { //Goes to Activity StartingPoint.java(STARTINGPOINT) Intent openstartingpoint=new Intent("xyzSTART"); startActivity(openstartingpoint); } } }; timer.start(); } //Destroy Welcome_screen.java after it goes to next activity @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); finish(); } 
+15
source

There is another solution for this, you can create another class for SplashScreen and make SplashScreen as your Launcher activity, but not MainActivity. Like this:

  <activity android:name=".SplashScreen" android:label="@string/title_activity_splash_screen" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme" > </activity> 

And thn in SplashSacreen.java, you write the code like this:

  public class SplashScreen extends AppCompatActivity { private static int SPLASH_TIME_OUT = 3000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash_screen); new Handler().postDelayed(new Runnable() { @Override public void run() { // This method will be executed once the timer is over // Start your app main activity Intent i = new Intent(SplashScreen.this, MainActivity.class); startActivity(i); // close this activity finish(); } }, SPLASH_TIME_OUT); } } 

Thn after in SplashScreen.xml

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_red_dark" > <ImageView android:id="@+id/imgLogo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/comp_logo" /> 

And then check it out

+4
source

Use the Handler to hold the user interface for some time:

 public class SplashActivity extends Activity { /*Duration of wait*/ private final int SPLASH_DISPLAY_LENGTH = 2000; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); new Handler().postDelayed(new Runnable() { @Override public void run() { /* Create an Intent that will start the MainActivity. */ Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class); startActivity(mainIntent); finish(); } }, SPLASH_DISPLAY_LENGTH); } } 
+4
source

Thus, a good way to do this would be to call asynctask and wait 3 seconds, after which postProgress will set the image with id splash to visibility.

So, here are some resources ...

http://developer.android.com/reference/android/os/AsyncTask.html

If necessary, I can explain further. You can also consider alternatives. I just suggested a solution for your current setup.

I decided to include the code ...

 private class SplashScreen extends AsyncTask<ImageView, Void, Void> { ImageView imgView; protected Void doInBackground(ImageView... view) { imgView = view[0]; wait(3000); // not sure if this works but u can fo a while loop etc if not } protected void onPostExecute(Long result) { imgView.setVisibility(ImageView.GONE); } } 

Then in your onCreate() instance and do this ...

 new SplashScreen().execute(splash); 
+1
source

Create a new XML layout for your splash named splash below in setContentView(R.layout.splash); . Then do a new action to play after the surge, I called it ACTIVITYTWO below, but you can change it. Change the number in while (lTimer1 < 3000) to change the burst length, from 1000 equal to 1 second.

 import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Window; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.splash); Thread lTimer = new Thread() { public void run() { try { int lTimer1 = 0; while (lTimer1 < 3000) { sleep(100); lTimer1 = lTimer1 + 100; } startActivity(new Intent("com.example.ACTIVITYTWO")); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { finish(); } } }; lTimer.start(); } } 
+1
source

try it

 public class Welcome extends Activity { /** Called when the activity is first created. */ Handler mHandler,actHandler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.welcome); new Thread(){ public void run(){ try{ Thread.sleep(3000); } catch(Exception ex){ Log.e("Welcome Exception :",ex.toString()); } try{ Message msg=mHandler.obtainMessage(); mHandler.sendMessage(msg); } catch(NullPointerException ex){ Log.e("Handler Exception :",ex.toString()); } } }.start(); mHandler=new Handler(){ public void handleMessage(Message msg){ super.handleMessage(msg); Intent i=new Intent(Welcome.this,M_chat.class); startActivity(i); finish(); } }; } } 
0
source
 private void moveToHome(){ new Handler().postDelayed(new Runnable() { @Override public void run() { Intent i = new Intent(SplashScreen.this, HomeScreen.class); startActivity(i); finish(); } }, 4000); } 
0
source

For Kotlin:

 class SplashActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_splash) Handler().postDelayed(Runnable { val i = Intent( this@SplashActivity , HomeScreen::class.java) startActivity(i) finish() }, 4000) } } 
0
source
 public class SplashActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); /*Duration of wait*/ int SPLASH_DISPLAY_LENGTH = 10000; new Handler().postDelayed(new Runnable() { @Override public void run() { /* Create an Intent that will start the MainActivity. */ Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class); startActivity(mainIntent); finish(); } }, SPLASH_DISPLAY_LENGTH); } } 
0
source

Source: https://habr.com/ru/post/906573/


All Articles