
Splash Screen in Android using Kotlin : Create Splash Screen in Android using Kotlin with Background Image
Tuesday, August 28, 2018
Comment
So, Let's begin by creating a new Android project in Android Studio. Fill all details about your application like Application Name, company domain, and project location. If you are using Android Studio 3.0 or above don't forget to add kotlin in your project or you can also add it's later in your project.
Firstly, Upload background.png and logo.png in your drawable folder also create new drawable resource file splash_screen.xml to create a background layer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item> | |
<bitmap android:src="@drawable/background" | |
android:gravity="center"> | |
</bitmap> | |
</item> | |
<item android:drawable="@color/whiteShade"> | |
</item> | |
</layer-list> |
After creating drawable resource edit your splash screen layout file which is located at res\layout directory. Make changes in your layout file as per below code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
xmlns:tool="http://schemas.android.com/tools" | |
android:background="@drawable/splash_screen" | |
tool:context=".activities.SplashScreen" | |
> | |
<LinearLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
android:layout_centerVertical="true" | |
android:layout_centerHorizontal="true"> | |
<ImageView | |
android:layout_width="269dp" | |
android:layout_height="90dp" | |
android:src="@drawable/logo" /> | |
<TextView | |
android:id="@+id/taglineText" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:fontFamily="@font/quicksand_medium" | |
android:text="Learn | Fun | Earn" | |
android:textColor="#849AAB" | |
android:textSize="20dp" | |
android:textAlignment="textEnd"/> | |
</LinearLayout> | |
</RelativeLayout> |
Also, make some changes in your Manifests file by adding launcher category into the intent filter of your splash screen activity. Below is the code snippet of AndroidManifest.xml file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.dreamvisionary.kotlinSplash"> | |
<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/AppTheme"> | |
<activity android:name=".activities.SplashScreen" | |
android:theme="@style/SplashTheme"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<activity android:name=".activities.MainActivity" | |
android:screenOrientation="portrait"> | |
<intent-filter> | |
<category android:name="android.intent.category.DEFAULT" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
Now Its time to work on your SplashScreen.kt. Here, you can set how much delay you want to start your application by focusing your splash screen. Also, you can set your splash screen as Fullscreen and if you want to make some animation you need to put your kotlin code in this file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dreamvisionary.kotlinSplash | |
import android.content.Intent | |
import android.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
import android.os.Handler | |
import android.view.View | |
import stuquiz.stupedia.com.stuquiz.R | |
class SplashScreen : AppCompatActivity() { | |
private var mDelayHandler: Handler? = null | |
private val SPLASH_DELAY: Long = 3000 | |
override fun onWindowFocusChanged(hasFocus: Boolean) { | |
super.onWindowFocusChanged(hasFocus) | |
if (hasFocus){ | |
hideSystemUI() | |
} | |
} | |
private fun hideSystemUI(){ | |
val decorView = window.decorView | |
decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_FULLSCREEN | |
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE | |
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | |
or View.SYSTEM_UI_FLAG_FULLSCREEN | |
// Hide the Nav bar and status bar | |
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | |
or View.SYSTEM_UI_FLAG_FULLSCREEN) | |
} | |
internal val mRunnable: Runnable = Runnable { | |
val intent = Intent(this,MainActivity::class.java) | |
startActivity(intent) | |
finish() | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_splash_screen) | |
//Initializing the Handle | |
mDelayHandler =Handler() | |
//Navigate with delay | |
mDelayHandler!!.postDelayed(mRunnable,SPLASH_DELAY) | |
} | |
} |
Best Book for Entrepreneur ==> Strategies To Build Successful Web Agency
0 Response to "Splash Screen in Android using Kotlin : Create Splash Screen in Android using Kotlin with Background Image"
Post a Comment