Let's see how we will implement this method in our project step by step.
Step 1. First of all we will create new project in android studio.As we create the project we will see an activity_main. xml and a MainActivity.java file.In this project we have done activity_main .xml file to show only next activity.But we have to create activity_splash first.
activity_splash
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/purple_700">
<ImageView
android:id="@+id/image"
android:layout_width="180dp"
android:layout_height="180dp"
android:src="@mipmap/ic_launcher_round"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Splash Screen"
android:layout_marginTop="10dp"
android:textSize="25sp"
android:textColor="#FFFFFF"
android:layout_centerInParent="true"
android:layout_below="@+id/image"/>
</RelativeLayout>
In this layout we have used an imageview and a textview .On which we will use animation so we have to find the id of these two.Now we will implement this layout in SplashActivity.
SplashActivity
package com.example.splashscreenandpermission;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
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;
public class SplashActivity extends AppCompatActivity {
public static final int splashTime=3000;
private ImageView imageView;
private TextView textView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
imageView=findViewById(R.id.image);
textView=findViewById(R.id.textView);
Animation animation= AnimationUtils.loadAnimation(this,R.anim.anim);
textView.startAnimation(animation);
imageView.startAnimation(animation);
if(!isPermissiongranted())
{
Toast.makeText(this, "Please give permission .....", Toast.LENGTH_SHORT).show();
}
else if(isPermissiongranted())
{
startNextActivity();
}
}
private boolean isPermissiongranted() {
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M)
{
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED)
{
return true;
}
else
{
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
return false;
}
}
else
{
return true;
}
}
private void startNextActivity() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent=new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
},splashTime);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
{
case 1:{
if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED)
{
if (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED){
startNextActivity();
}
}
else
{
finish();
}
}
}
}
}
In this class we have created splashtime and we will set animation on imageview and textview so that when app is run then animation is set on imageview and textview But before that we will create a folder of anim type, to create it, we will right click on the res folder and click on an Android resource directory and select the resource type anime and press ok so that the anim folder will be created.Then we will create animation xml file of type anim by clicking on anim folder.
anim
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="2000"
android:fromYDelta="100%p"
android:fromXDelta="0%p"/>
</set>
Then we'll load the animation layout with the class of ham animation type, and set the animation to the Textview and imageview.Then we will call the run time permission method.
activity_main
<?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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MainActivity"
android:textSize="30sp"
android:textColor="@color/purple_700"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivitypackage com.example.splashscreenandpermission;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I hope this article will be useful for you
Thank you
0 Comments
Please do not enter any spam link in the comment box
Emoji