Day 1: Creating SplashScreen Using Kotlin with MVVM Pattern(SHouse)

Day 1: Creating SplashScreen Using Kotlin with MVVM Pattern(SHouse)



In this article, We are going to create the very first activity of our project which is Splash Screen Activity. The basic use of splash Screen is for branding and load or check network connections. But in this example, we are only focusing to create a very simple splash screen in Android studio using Kotlin.

 Step 1: Create New Project In Your android studio with an empty activity. Don't forget to select Kotlin as language and AndroidX support library.

READ ALSO

Step 2: In this entire project we are going to use MVVM pattern so before starting any work just create some common folder architecture like below 
  • Data - Here we will create classes of entire backend logic like the connection to the database, API calls etc.
  • UI - It will contain all activity and classes which mostly communicate with our User Interface
  • Utils - Most of the time we need to use repeated some utility methods like Toast, Progress bars or other. 
Step 3: Copy your logo file and paste in your drawable folder which is located in the resource directory.

Step 4: Create a new empty activity with name of SplashScreen in UI directory 

Step 5: Open activity_splash_screen.xml layout file and type below code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@android:color/white"
tools:context=".UI.SplashScreen">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:layout_centerInParent="true"
android:layout_margin="72dp"
/>
</RelativeLayout>




Step 6: Open SplashScreen.kt class file and type below code

package com.shouse.UI
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import androidx.appcompat.app.AppCompatActivity
import com.shouse.R
import com.shouse.UI.Auth.LoginActivity
class SplashScreen : AppCompatActivity() {
lateinit var handler:Handler;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash_screen)
handler = Handler()
handler.postDelayed({
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
finish()
},3000) // Activity will change after 3 sec
}
}
view raw SplashScreen.kt hosted with ❤ by GitHub


In the above code, we used Handler for executing inside code after a specific time. We need to provide time in a millisecond to the handler. To learn more about handler visit android official documentation for the handler

Step 7: Add intent filter for SplashScreen activity in AndroidManifest.xml File. Your manifest file looks like below:


 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shouse">
<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=".UI.SplashScreen"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
</application>
</manifest>


We used <intent-filter> to define what is the exact role and type of the Intent defined in manifest file. 

Step 8: Almost done here. Connect your android device or use AVD and run your application.



Related Posts

1 Response to "Day 1: Creating SplashScreen Using Kotlin with MVVM Pattern(SHouse)"

Iklan Atas Artikel

Advetisement

Advertisement

Iklan Bawah Artikel