Recently Added

6/recent/ticker-posts

How to create splashscreen in android studio Part 1

In this part 1, I'm going to create splash screen and I will also ask for the read and write permission in the splash screen and when we got permission then we will instantly go to our next activity.



1.I will create the first splash.xml in drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/black" />
<item>
<bitmap
android:gravity="center"
android:src="@drawable/logo" />
</item>
</layer-list>

2.Now inside your app>res>values>themes create style


<style name="Splashscreen" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:statusBarColor" tools:targetApi="l">@color/black</item>
<item name="android:windowBackground">@drawable/splash</item>
<item name="android:navigationBarColor" tools:targetApi="lollipop">@color/light_black</item>
<item name="android:navigationBarDividerColor" tools:targetApi="o_mr1">@color/light_grey</item>
</style>


3.Create SplashScreen.java and paste this code
package com.example.player;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.player.activity.MainActivity;

public class SplashScreen extends AppCompatActivity {

private static final int REQUEST_CODE_PERMISSION = 123;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//also add the permission in manifest
permission();
}
private void nextActivity() {
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
finish();
}
private void permission() {
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(SplashScreen.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_PERMISSION);
} else {
nextActivity();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_PERMISSION) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(SplashScreen.this, "Permission Granted", Toast.LENGTH_SHORT).show();
nextActivity();

} else {
Toast.makeText(this, "denied", Toast.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(SplashScreen.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_PERMISSION);
}
}

}

}

4.we have to make changes also in the AndroidManifest file add user permission and also create an activity for splash screen
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.player">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Player">
<activity android:name=".activity.VideoFolder"></activity>
<activity android:name=".activity.MainActivity" />
<activity
android:name=".SplashScreen"
android:theme="@style/Splashscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Watch Full Video Tutorials

Post a Comment

0 Comments