A splash screen is a graphical control element consisting of a window containing an image a logo or the current version of the software.
A splash screen usually appears while a game or program is launching.
Program for Splash Screen
- Use this code in Activity_main.xml File in Layout folder
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:background="#ff0" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textSize="30dp" android:textColor="@color/colorPrimaryDark" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
2. In Java file of Main XML, MainActivity.java write this code to make the splash work, the first screen will appear for three seconds and then will move to the next screen (second) that we have created.The movement from one activity to second is possible due to the use of Intent.
import android.content.Intent; import android.os.Bundle; import android.os.Handler; public class MainActivity extends AppCompatActivity { Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); handler=new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { Intent intent=new Intent(getApplicationContext(), second.class); startActivity(intent); finish(); } },3000); } } NOTE: the second.class will give error this time, as this activity is not created at the moment.
Now in next step we will create “second activity”
3.Create another activity with name “second” by doing right click on Java folder in right side of android project space, then click new, then click empty activity, and give name “second“, this will create .XML and java both
type of file. In XML type the code given
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context=".second"> <EditText android:id="@+id/edName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="Name" tools:layout_editor_absoluteX="5dp" tools:layout_editor_absoluteY="23dp" /> <EditText android:id="@+id/edContact" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="text" android:hint="Contact" /> <EditText android:id="@+id/edTotalFee" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="number" android:hint="Total Fees" /> <EditText android:id="@+id/edFeePaid" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="number" android:hint="Fees Paid" /> <Button android:id="@+id/btnsave" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Save" /> <Button android:id="@+id/btnShow" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Show" /> </LinearLayout>
Now Run your Project and you will see the output of this splash screen project.