<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Prasanth Skyblue on Medium]]></title>
        <description><![CDATA[Stories by Prasanth Skyblue on Medium]]></description>
        <link>https://medium.com/@prasanth968?source=rss-ac842b5386e1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*iRqNRc8GOykE5ZqDdnfohw.jpeg</url>
            <title>Stories by Prasanth Skyblue on Medium</title>
            <link>https://medium.com/@prasanth968?source=rss-ac842b5386e1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 05:19:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@prasanth968/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Kotlin : Image Picker From Gallery using ActivityResultContracts]]></title>
            <link>https://medium.com/@prasanth968/kotlin-image-picker-from-gallery-using-activityresultcontracts-9b5aa32e42d0?source=rss-ac842b5386e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b5aa32e42d0</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Prasanth Skyblue]]></dc:creator>
            <pubDate>Fri, 16 Feb 2024 07:02:13 GMT</pubDate>
            <atom:updated>2024-02-16T07:02:13.584Z</atom:updated>
            <content:encoded><![CDATA[<h3>Kotlin : Image Picker From Gallery using ActivityResultContracts</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2ddP0Fh4FIUH55i4sgKSow.jpeg" /></figure><p>A collection of some <strong>standard activity call contracts</strong>, as provided by android.</p><p><strong>Nested type</strong></p><p>public class ActivityResultContracts.GetContent extends ActivityResultContract</p><p><strong>Developer documentation</strong></p><ul><li>An ActivityResultContract to prompt the user to pick a piece of content, receiving a content:// Uri</li><li>content that allows you to use android.content.ContentResolver.openInputStream to access the raw data. By default,</li><li>this adds Intent.CATEGORY_OPENABLE to only return content that can be represented as a stream.</li><li>The input is the <strong>mime type</strong> to filter by, e.g. <strong>image/\*</strong>.</li></ul><p><strong>Register a request to start an activity for result</strong></p><pre>registerForActivityResult();</pre><p><strong>androidx.activity.result.ActivityResultCaller</strong> Register a request to <strong>start an activity for result</strong>, designated by the given <strong>contract</strong>. This creates a record in the <strong>registry</strong> associated with this caller, managing request code, as well as conversions to/from <strong>Intent </strong>under the hood. This *must* be called unconditionally, as part of initialization path, typically as a field initializer of an <strong>Activity or Fragment.</strong></p><p><strong>Code</strong></p><pre>val galleryLauncher = registerForActivityResult(ActivityResultContracts.GetContent(),<br>    fun(it: Uri?) {<br>        val galleryUri = it<br>        try {<br>            // TODO SOMETHING &#39;galleryUri&#39;<br>        } catch (e: Exception) {<br>            e.printStackTrace()<br>        }<br>    })</pre><p><strong>Call galleryLauncher</strong></p><pre>galleryLauncher.launch(&quot;image/*&quot;)</pre><p><strong>Full code xml ( activity_main.xml )</strong></p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;RelativeLayout<br>    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;<br>    tools:context=&quot;.MainActivity&quot;<br>    android:orientation=&quot;vertical&quot;&gt;<br><br>    &lt;Button<br>        android:id=&quot;@+id/button&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:layout_gravity=&quot;center_horizontal&quot;<br>        android:layout_marginTop=&quot;36dp&quot;<br>        android:text=&quot;SELECT IMAGE&quot;<br>        android:layout_alignParentBottom=&quot;true&quot;<br>        android:layout_margin=&quot;20dp&quot;<br>        android:textColor=&quot;@color/white&quot;<br>        android:textSize=&quot;20sp&quot; /&gt;<br><br>    &lt;ImageView<br>        android:id=&quot;@+id/image&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;350dp&quot;<br>        android:layout_marginStart=&quot;16dp&quot;<br>        android:layout_marginTop=&quot;16dp&quot;<br>        android:layout_marginEnd=&quot;16dp&quot; /&gt;<br><br>&lt;/RelativeLayout&gt;</pre><p><strong>Full code Kotlin MainActivity.kt (</strong>Without lambda expression )</p><pre>class MainActivity : AppCompatActivity() {<br>    lateinit var binding: ActivityMainBinding<br>    val galleryLauncher = registerForActivityResult(ActivityResultContracts.GetContent(),<br>        fun(it: Uri?) {<br>            val galleryUri = it<br>            try {<br>                binding.image.setImageURI(galleryUri)<br>            } catch (e: Exception) {<br>                e.printStackTrace()<br>            }<br>        })<br><br>    override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)<br>        binding = ActivityMainBinding.inflate(layoutInflater)<br>        setContentView(binding.root)<br>        binding.button.setOnClickListener(fun(it: View) {<br>            galleryLauncher.launch(&quot;image/*&quot;)<br>        })<br>    }<br>}</pre><p><strong>Full code Kotlin MainActivity.kt (</strong>With lambda expression )</p><pre>class MainActivity : AppCompatActivity() {<br>    lateinit var binding: ActivityMainBinding<br>    val galleryLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) {<br>        val galleryUri = it<br>        try{<br>            binding.image.setImageURI(galleryUri)<br>        }catch(e:Exception){<br>            e.printStackTrace()<br>        }<br><br>    }<br>    override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)<br>        binding = ActivityMainBinding.inflate(layoutInflater)<br>        setContentView(binding.root)<br>        binding.button.setOnClickListener {<br>            galleryLauncher.launch(&quot;image/*&quot;)<br>        }<br>    }<br>}</pre><p>Available <a href="https://github.com/prasanth9689/Image_Picker_From_Gallery_using_ActivityResultContracts">git repo</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b5aa32e42d0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin : Store Application Specific Data]]></title>
            <link>https://medium.com/@prasanth968/kotlin-store-application-specific-data-71c45fbb5702?source=rss-ac842b5386e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/71c45fbb5702</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Prasanth Skyblue]]></dc:creator>
            <pubDate>Wed, 14 Feb 2024 12:46:46 GMT</pubDate>
            <atom:updated>2024-02-14T13:55:26.606Z</atom:updated>
            <content:encoded><![CDATA[<h3>Kotlin : Store Application Specific Data Folder</h3><p>Declare global variable</p><pre>val APP_DATA = &quot;/.Skyblue&quot;<br>private var bmp: Bitmap? = null</pre><p>Create folder</p><pre>  val dir = getExternalFilesDir(APP_DATA)<br>        if (!dir!!.exists()) {<br>            if (!dir!!.mkdir()) {<br>                Toast.makeText(<br>                    applicationContext,<br>                    &quot;The folder &quot; + dir!!.path + &quot;was not created&quot;,<br>                    Toast.LENGTH_SHORT<br>                ).show()<br>            }<br>        }</pre><p>Example: to create empty bitmap to save android application “<strong>specific data</strong>” folder</p><pre>        val w = 200<br>        val h = 200<br><br>        val conf = Bitmap.Config.ARGB_8888 // see other conf types<br>        bmp = Bitmap.createBitmap(w, h, conf) // this creates a MUTABLE bitmap<br>        bmp?.eraseColor(Color.RED)<br>        val canvas = Canvas(bmp!!)<br><br>        // Convert bitmap to jpg and save<br>        convertAndSaveImage()<br><br>        // get stored image<br>        val root = getExternalFilesDir(&quot;/&quot;)!!.path + &quot;/&quot; + &quot;.Skyblue/&quot;<br>        val file = File(root, &quot;test_image&quot; + &quot;.jpg&quot;)<br>        val testBitmap = BitmapFactory.decodeFile(file.absolutePath)</pre><p>Create method like</p><pre>convertAndSaveImage()</pre><p>Using this getExternalFilesDir() It returns the path to <strong>files</strong> folder inside <strong>Android/data/data/your_package/</strong> on your SD card.</p><pre> val root = getExternalFilesDir(&quot;/&quot;)!!.path + &quot;/&quot; + &quot;.Skyblue/&quot;</pre><p>Bitmap image saving to “Android specific data “ folder.</p><pre>  val file = File(root, &quot;test_image&quot; + &quot;.jpg&quot;)<br>  val testBitmap = BitmapFactory.decodeFile(file.absolutePath)</pre><p>Convert and save image</p><pre> private fun convertAndSaveImage() {<br>        var dir: String? = null<br>        dir = getExternalFilesDir(&quot;/&quot;)!!.path + &quot;/&quot; + &quot;.Skyblue/&quot;<br><br>        Toast.makeText(this, dir, Toast.LENGTH_LONG).show()<br><br>        val bos = ByteArrayOutputStream()<br>        bmp!!.compress(Bitmap.CompressFormat.JPEG, 80, bos)<br>        val bitmapdata = bos.toByteArray()<br><br>        val file = File(dir, &quot;test_image&quot; + &quot;.jpg&quot;)<br><br>        try {<br>            file.createNewFile()<br>        } catch (e: IOException) {<br>            e.printStackTrace()<br>        }<br><br>        //write the bytes in file<br>        var fos: FileOutputStream? = null<br>        try {<br>            fos = FileOutputStream(file)<br>            fos.write(bitmapdata)<br>            fos.flush()<br>            fos.close()<br>        } catch (e: FileNotFoundException) {<br>            e.printStackTrace()<br>        } catch (e: IOException) {<br>            e.printStackTrace()<br>        }<br>    }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*7qjOqXFgOTSwNIlAwdaOww.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/378/1*p35M_cYjr8ztOBoOstdobw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uAn6Q0xPqgz5sfYgdah2CQ.png" /></figure><p>Finally completed this available <a href="https://github.com/prasanth9689/Store_Application_Specific_Data_Kotlin">github repo</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71c45fbb5702" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin : Navigation drawer with dataBinding and Context]]></title>
            <link>https://medium.com/@prasanth968/navigation-drawer-with-databinding-and-context-8f8a8beeccf0?source=rss-ac842b5386e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/8f8a8beeccf0</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Prasanth Skyblue]]></dc:creator>
            <pubDate>Mon, 12 Feb 2024 12:39:48 GMT</pubDate>
            <atom:updated>2024-02-13T05:12:22.209Z</atom:updated>
            <content:encoded><![CDATA[<p>Create menu directory in res folder. then create xml file like ‘ nav_menu.xml’</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;<br>    &lt;item<br>        android:id=&quot;@+id/firstItem&quot;<br>        android:title=&quot;First Item&quot; /&gt;<br>    &lt;item<br>        android:id=&quot;@+id/secondtItem&quot;<br>        android:title=&quot;Second Item&quot; /&gt;<br>    &lt;item<br>        android:id=&quot;@+id/thirdItem&quot;<br>        android:title=&quot;Third Item&quot; /&gt;<br>&lt;/menu&gt;</pre><p>Create nav_header.xml in layout folder, layout folder inside</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;150dp&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    android:background=&quot;@color/teal_700&quot;&gt;<br><br>    &lt;TextView<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:text=&quot;Sample Title&quot;<br>        app:layout_constraintTop_toTopOf=&quot;parent&quot;<br>        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;<br>        app:layout_constraintStart_toStartOf=&quot;parent&quot;<br>        android:textColor=&quot;@color/white&quot;<br>        android:layout_marginStart=&quot;50dp&quot;/&gt;<br><br>&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;</pre><p>activity_main.xml</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;<br><br>    &lt;data&gt;<br><br>    &lt;/data&gt;<br><br>    &lt;androidx.drawerlayout.widget.DrawerLayout<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot;<br>        android:id=&quot;@+id/drawerLayout&quot;<br>        tools:context=&quot;.MainActivity&quot;&gt;<br>        &lt;RelativeLayout<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;match_parent&quot;<br>            android:background=&quot;#FFFFFF&quot;&gt;<br><br>            &lt;RelativeLayout<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;50dp&quot;<br>                android:background=&quot;#0092FF&quot;&gt;<br><br>                &lt;LinearLayout<br>                    android:layout_width=&quot;wrap_content&quot;<br>                    android:layout_height=&quot;wrap_content&quot;<br>                    android:orientation=&quot;horizontal&quot;<br>                    android:layout_centerVertical=&quot;true&quot;<br>                    android:layout_marginStart=&quot;10dp&quot;&gt;<br><br>                    &lt;ImageView<br>                        android:id=&quot;@+id/menu&quot;<br>                        android:layout_width=&quot;35dp&quot;<br>                        android:layout_height=&quot;35dp&quot;<br>                        android:src=&quot;@drawable/ic_menu&quot;<br>                        android:layout_gravity=&quot;center_vertical&quot;<br>                        app:tint=&quot;@color/white&quot; /&gt;<br><br>                    &lt;TextView<br>                        android:layout_width=&quot;wrap_content&quot;<br>                        android:layout_height=&quot;wrap_content&quot;<br>                        android:text=&quot;Side nav bar&quot;<br>                        android:textSize=&quot;20sp&quot;<br>                        android:layout_marginStart=&quot;5dp&quot;<br>                        android:textColor=&quot;@color/white&quot;<br>                        android:layout_gravity=&quot;center_vertical&quot;/&gt;<br>                &lt;/LinearLayout&gt;<br><br>            &lt;/RelativeLayout&gt;<br><br>        &lt;/RelativeLayout&gt;<br>        &lt;com.google.android.material.navigation.NavigationView<br>            android:id=&quot;@+id/navView&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;match_parent&quot;<br>            app:menu=&quot;@menu/nav_menu&quot;<br>            app:headerLayout=&quot;@layout/nav_header&quot;<br>            android:layout_gravity=&quot;start&quot;<br>            android:fitsSystemWindows=&quot;true&quot;/&gt;<br>    &lt;/androidx.drawerlayout.widget.DrawerLayout&gt;<br>&lt;/layout&gt;</pre><p>MainActivity.kt</p><pre>class MainActivity : AppCompatActivity() {<br>    private lateinit var binding: ActivityMainBinding<br>    private var context: Context = this@MainActivity<br>    private lateinit var toggle: ActionBarDrawerToggle<br><br>    override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)<br>        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);<br><br>        binding.apply {<br>            toggle = ActionBarDrawerToggle(this@MainActivity, drawerLayout, R.string.open, R.string.close)<br>            drawerLayout.addDrawerListener(toggle)<br>            toggle.syncState()<br><br>            supportActionBar?.setDisplayHomeAsUpEnabled(true)<br><br>            navView.setNavigationItemSelectedListener {<br>                when (it.itemId) {<br>                    R.id.firstItem -&gt; {<br>                        Toast.makeText(context, &quot;First Item Clicked&quot;, Toast.LENGTH_SHORT).show()<br>                    }<br>                    R.id.secondtItem -&gt; {<br>                        Toast.makeText(context, &quot;Second Item Clicked&quot;, Toast.LENGTH_SHORT).show()<br>                    }<br>                    R.id.thirdItem -&gt; {<br>                        Toast.makeText(context, &quot;third Item Clicked&quot;, Toast.LENGTH_SHORT).show()<br>                    }<br>                }<br>                true<br>            }<br>        }<br><br>        binding.menu.setOnClickListener{<br>            Toast.makeText(context, &quot;Clicked&quot;, Toast.LENGTH_SHORT).show();<br>            binding.drawerLayout.openDrawer(GravityCompat.START)<br>        }<br>    }<br><br>    override fun onOptionsItemSelected(item: MenuItem): Boolean {<br>        if (toggle.onOptionsItemSelected(item)){<br>            true<br>        }<br>        return super.onOptionsItemSelected(item)<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bTqY5-ylYQhE59RY5uNShg.jpeg" /></figure><p>And github <a href="https://github.com/prasanth9689/Navigation_Drawer">repository</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8f8a8beeccf0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin : Splash screen]]></title>
            <link>https://medium.com/@prasanth968/kotlin-splash-screen-78ece218ff1d?source=rss-ac842b5386e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/78ece218ff1d</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Prasanth Skyblue]]></dc:creator>
            <pubDate>Mon, 12 Feb 2024 06:17:43 GMT</pubDate>
            <atom:updated>2024-02-12T06:17:43.631Z</atom:updated>
            <content:encoded><![CDATA[<h3>Kotlin : Splash screen</h3><p>Add viewbinding in your build.gradle</p><pre> buildFeatures{<br>        viewBinding = true<br>    }</pre><p>Create splash activity ( activity_splash.xml )</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;<br>    android:background=&quot;#03A9F4&quot;<br>    tools:context=&quot;.SplashActivity&quot;&gt;<br><br>    &lt;TextView<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:text=&quot;Skyblue&quot;<br>        android:textSize=&quot;30sp&quot;<br>        android:layout_centerInParent=&quot;true&quot;<br>        android:textColor=&quot;@color/white&quot;<br>        /&gt;<br><br>&lt;/RelativeLayout&gt;</pre><p>SplashActivity.kt</p><pre>class SplashActivity : AppCompatActivity() {<br>    private lateinit var binding: ActivitySplashBinding<br><br>    override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)<br>        binding = ActivitySplashBinding.inflate(layoutInflater)<br>        setContentView(binding.root)<br><br>        window.setFlags(<br>            WindowManager.LayoutParams.FLAG_FULLSCREEN,<br>            WindowManager.LayoutParams.FLAG_FULLSCREEN<br>        )<br><br>        Handler(Looper.getMainLooper()).postDelayed({<br>            val intent = Intent(this, MainActivity::class.java)<br>            startActivity(intent)<br>            finish()<br>        }, 3000)<br>    }<br>}</pre><p>Create MainActivity ( activity_main.xml )</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;<br>    tools:context=&quot;.MainActivity&quot;&gt;<br><br>    &lt;TextView<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:text=&quot;Home&quot;<br>        android:textSize=&quot;35sp&quot;<br>        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;<br>        app:layout_constraintEnd_toEndOf=&quot;parent&quot;<br>        app:layout_constraintStart_toStartOf=&quot;parent&quot;<br>        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;<br><br>&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;</pre><p>MainActivity.kt</p><pre>class MainActivity : AppCompatActivity() {<br>    override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)<br>        setContentView(R.layout.activity_main)<br>    }<br>}</pre><p>Finally kotlin simple splash screen completed with viewBinding</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uokj-OOJimHFdERGKcT-xw.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=78ece218ff1d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Recyclerview load more Retrofit json]]></title>
            <link>https://medium.com/@prasanth968/recyclerview-load-more-retrofit-json-ff08ac8c6d2a?source=rss-ac842b5386e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff08ac8c6d2a</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[recyclerview]]></category>
            <dc:creator><![CDATA[Prasanth Skyblue]]></dc:creator>
            <pubDate>Fri, 09 Feb 2024 08:21:26 GMT</pubDate>
            <atom:updated>2024-02-09T08:21:26.331Z</atom:updated>
            <content:encoded><![CDATA[<p>Add databinding in your build.gradle</p><pre>    buildFeatures {<br>        dataBinding = true<br>    }</pre><p>API url</p><pre>https://api.skyblue.co.in/video_list.php</pre><p>API Received JSON</p><pre>[<br><br>    {<br>        &quot;user_name&quot;: &quot;Prasanth&quot;,<br>        &quot;thumbnail_url&quot;: &quot;http://skyblue.co.in/skyblue/user/post_video/thumbnail/PHcijR7oWnWVnHNEh2iYf3YtB.JPG&quot;,<br>        &quot;video_name&quot;: &quot;VG9tIGFuZCBKZXJyeQ==\n&quot;,<br>        &quot;placeholder_url&quot;: &quot;&quot;,<br>        &quot;likes&quot;: &quot;1&quot;,<br>        &quot;comments&quot;: &quot;1&quot;,<br>        &quot;user_id&quot;: &quot;68&quot;,<br>        &quot;profile_url&quot;: null,<br>        &quot;like_status&quot;: null,<br>        &quot;post_id&quot;: &quot;298&quot;,<br>        &quot;time_date&quot;: &quot;07-11-2023 15:26:27&quot;,<br>        &quot;user_cover&quot;: null,<br>        &quot;media_type&quot;: &quot;&quot;,<br>        &quot;video_url&quot;: &quot;http://skyblue.co.in/skyblue/user/post_video/PHcijR7oWnWVnHNEh2iYf3YtB.MP4&quot;,<br>        &quot;duration&quot;: &quot;0 min, 18 sec&quot;,<br>        &quot;channel_id&quot;: &quot;154&quot;,<br>        &quot;total_views&quot;: &quot;8&quot;,<br>        &quot;channel_name&quot;: &quot;Prasanth&quot;<br>    },<br>    {<br>        &quot;user_name&quot;: &quot;Prasanth&quot;,<br>        &quot;thumbnail_url&quot;: &quot;http://skyblue.co.in/skyblue/user/post_video/thumbnail/E8XnveZevlYPR5SL74vS9AqiQ.JPG&quot;,<br>        &quot;video_name&quot;: &quot;VGVzdCA=\n&quot;,<br>        &quot;placeholder_url&quot;: &quot;&quot;,<br>        &quot;likes&quot;: &quot;0&quot;,<br>        &quot;comments&quot;: &quot;0&quot;,<br>        &quot;user_id&quot;: &quot;68&quot;,<br>        &quot;profile_url&quot;: null,<br>        &quot;like_status&quot;: null,<br>        &quot;post_id&quot;: &quot;307&quot;,<br>        &quot;time_date&quot;: &quot;04-12-2023 23:17:01&quot;,<br>        &quot;user_cover&quot;: null,<br>        &quot;media_type&quot;: &quot;&quot;,<br>        &quot;video_url&quot;: &quot;http://skyblue.co.in/skyblue/user/post_video/E8XnveZevlYPR5SL74vS9AqiQ.MP4&quot;,<br>        &quot;duration&quot;: &quot;0 min, 8 sec&quot;,<br>        &quot;channel_id&quot;: &quot;153&quot;,<br>        &quot;total_views&quot;: &quot;5&quot;,<br>        &quot;channel_name&quot;: &quot;Akila&quot;<br>    },<br>    {<br>        &quot;user_name&quot;: &quot;Prasanth&quot;,<br>        &quot;thumbnail_url&quot;: &quot;http://skyblue.co.in/skyblue/user/post_video/thumbnail/udzsnHJF2b5HhgM7XnAyzZR1r.JPG&quot;,<br>        &quot;video_name&quot;: &quot;VGVzdCAy\n&quot;,<br>        &quot;placeholder_url&quot;: &quot;&quot;,<br>        &quot;likes&quot;: &quot;0&quot;,<br>        &quot;comments&quot;: &quot;0&quot;,<br>        &quot;user_id&quot;: &quot;68&quot;,<br>        &quot;profile_url&quot;: null,<br>        &quot;like_status&quot;: null,<br>        &quot;post_id&quot;: &quot;316&quot;,<br>        &quot;time_date&quot;: &quot;01-02-2024 15:19:38&quot;,<br>        &quot;user_cover&quot;: null,<br>        &quot;media_type&quot;: &quot;&quot;,<br>        &quot;video_url&quot;: &quot;http://skyblue.co.in/skyblue/user/post_video/udzsnHJF2b5HhgM7XnAyzZR1r.MP4&quot;,<br>        &quot;duration&quot;: &quot;4 min, 29 sec&quot;,<br>        &quot;channel_id&quot;: &quot;154&quot;,<br>        &quot;total_views&quot;: &quot;&quot;,<br>        &quot;channel_name&quot;: &quot;Prasanth&quot;<br>    }<br>]</pre><p>Setup retrofit - add dependency</p><pre>implementation (&quot;com.squareup.retrofit2:retrofit:2.9.0&quot;)<br>implementation (&quot;com.squareup.retrofit2:converter-gson:2.9.0&quot;)</pre><p>Create API Client — Retrofit</p><pre>public class APIClient {<br>    private static Retrofit retrofit = null;<br><br>    public static Retrofit getClient(){<br><br>        List&lt;Protocol&gt; protocols = new ArrayList&lt;Protocol&gt;()<br>        {{<br>            add(Protocol.HTTP_1_1); // &lt;-- The only protocol used<br>            add(Protocol.HTTP_2);<br>        }};<br><br>        final OkHttpClient okHttpClient = new OkHttpClient.Builder()<br>                .callTimeout(0, TimeUnit.SECONDS)<br>                .connectTimeout(60, TimeUnit.SECONDS)<br>                .readTimeout(60, TimeUnit.SECONDS)<br>                .connectTimeout(5, TimeUnit.SECONDS)<br>                .protocols(protocols)<br>                .build();<br><br>        Gson gson = new GsonBuilder()<br>                .setLenient()<br>                .create();<br><br><br>        if(retrofit ==null){<br>            retrofit = new Retrofit.Builder()<br>                    .baseUrl(&quot;https://api.skyblue.co.in&quot;)<br>                    .addConverterFactory(GsonConverterFactory.create())<br>                    .client(okHttpClient)<br>                    .build();<br>        }<br>        return  retrofit;<br>    }<br>}</pre><p>Create API Interface</p><pre>public interface APIInterface {<br>    @FormUrlEncoded<br>    @POST(&quot;/video_list.php&quot;)<br>    Call&lt;ResponseBody&gt; getCommonPosts(@Field(&quot;user_id&quot;) String user_id);<br>}</pre><p>Setup databinding</p><pre>private ActivityMainBinding binding;</pre><pre>binding = DataBindingUtil.setContentView(this, R.layout.activity_main);</pre><p>Setup recyclerview</p><pre>LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);<br>linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);<br>binding.recyclerView.setLayoutManager(linearLayoutManager);<br>binding.recyclerView.setHasFixedSize(true);<br>binding.recyclerView.setItemViewCacheSize(20);<br>recyclerViewAdapter = new RecyclerViewAdapter(rowsArrayList);<br>binding.recyclerView.setAdapter(recyclerViewAdapter);</pre><p>Create model class</p><pre>public class Post {<br>    String user_name;<br><br>    public String getUser_name() {<br>        return user_name;<br>    }<br><br>    public void setUser_name(String user_name) {<br>        this.user_name = user_name;<br>    }<br>}</pre><p>Create adapter class</p><pre>public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt; {<br>    private final int VIEW_TYPE_ITEM = 0;<br>    private final int VIEW_TYPE_LOADING = 1;<br>    public List&lt;Post&gt; postList;<br><br>    public RecyclerViewAdapter(List&lt;Post&gt; postList) {<br>        this.postList = postList;<br>    }<br><br>    @NonNull<br>    @Override<br>    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {<br>        if (viewType == VIEW_TYPE_ITEM) {<br>            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);<br>            return new PostViewHolder(view);<br>        } else {<br>            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_loading, parent, false);<br>            return new LoadingViewHolder(view);<br>        }<br>    }<br><br>    @Override<br>    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {<br>        if (holder instanceof PostViewHolder) {<br>            PostViewHolder holder1 = (PostViewHolder) holder;<br><br>            Post post = postList.get(position);<br>            holder1.txtUserName.setText(post.getUser_name());<br><br>        }else if (holder instanceof LoadingViewHolder) {<br>            LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;<br>            loadingViewHolder.progressBar.setIndeterminate(true);<br>        }<br>    }<br><br>    @Override<br>    public int getItemCount() {<br>        return postList == null ? 0 : postList.size();<br>    }<br><br>    @Override<br>    public int getItemViewType(int position) {<br>        return postList.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;<br>    }<br><br>    public static class PostViewHolder extends RecyclerView.ViewHolder{<br>        TextView txtUserName;<br><br>        public PostViewHolder(@NonNull View itemView) {<br>            super(itemView);<br>            txtUserName = itemView.findViewById(R.id.user_name);<br>        }<br>    }<br><br>    public static class LoadingViewHolder extends RecyclerView.ViewHolder {<br>        public ProgressBar progressBar;<br><br>        public LoadingViewHolder(View itemView) {<br>            super(itemView);<br>            progressBar = itemView.findViewById(R.id.progressBar);<br>        }<br>    }<br>}</pre><p>Now load video list from api using Retrofit</p><pre>private void loadList() {<br><br>    APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);<br>    Call&lt;ResponseBody&gt; call=apiInterface.getCommonPosts(&quot;1&quot;);<br><br>    call.enqueue(new Callback&lt;ResponseBody&gt;() {<br>        @Override<br>        public void onResponse(@NonNull Call&lt;ResponseBody&gt; call, @NonNull Response&lt;ResponseBody&gt; response) {<br>            if (response.isSuccessful()){<br>                String postList = &quot;&quot;;<br><br>                try {<br>                    assert response.body() != null;<br>                    postList = response.body().string();<br>                    JSONArray jsonArray = new JSONArray(postList);<br><br>                    for (int i = 0; i&lt;jsonArray.length(); i++) {<br>                        Post post = new Post();<br>                        JSONObject object = jsonArray.getJSONObject(i);<br>                        post.setUser_name(object.getString(&quot;user_name&quot;));<br>                        rowsArrayList.add(post);<br>                    }<br><br>                    recyclerViewAdapter.notifyDataSetChanged();<br>                } catch (JSONException e) {<br>                   // throw new RuntimeException(e);<br>                    Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();<br>                } catch (IOException e) {<br>                    throw new RuntimeException(e);<br>                }<br>            }<br>        }<br><br>        @Override<br>        public void onFailure(@NonNull Call&lt;ResponseBody&gt; call, @NonNull Throwable t) {<br>            Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();<br>        }<br>    });<br>}</pre><p>Setup recyclerview ScrollListener</p><pre>binding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {<br>            @Override<br>            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {<br>                super.onScrollStateChanged(recyclerView, newState);<br>            }<br><br>            @Override<br>            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {<br>                super.onScrolled(recyclerView, dx, dy);<br>                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();<br><br>                if (!isLoading) {<br>                    if (linearLayoutManager != null &amp;&amp; linearLayoutManager.findLastCompletelyVisibleItemPosition() == rowsArrayList.size() - 1) {<br>                        //bottom of list!<br>                        loadMore();<br>                        Log.e(&quot;loadmore_&quot;, &quot;isLoading = true&quot;);<br>                        isLoading = true;<br>                    }<br>                }<br>            }<br>        });</pre><p>Load more</p><pre>private void loadMore() {<br>    // add load more progressbar<br>    rowsArrayList.add(null);<br>    recyclerViewAdapter.notifyItemInserted(rowsArrayList.size() - 1);<br><br>    APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);<br>    Call&lt;ResponseBody&gt; call=apiInterface.getCommonPosts(&quot;1&quot;);<br><br>    call.enqueue(new Callback&lt;ResponseBody&gt;() {<br>        @Override<br>        public void onResponse(@NonNull Call&lt;ResponseBody&gt; call, @NonNull Response&lt;ResponseBody&gt; response) {<br>            if (response.isSuccessful()){<br>                String postList = &quot;&quot;;<br><br>                try {<br>                    assert response.body() != null;<br>                    postList = response.body().string();<br>                    JSONArray jsonArray = new JSONArray(postList);<br><br>                    // remove progress bar<br>                    rowsArrayList.remove(rowsArrayList.size() - 1);<br>                    int scrollPosition = rowsArrayList.size();<br>                    recyclerViewAdapter.notifyItemRemoved(scrollPosition);<br><br>                    for (int i = 0; i&lt;jsonArray.length(); i++) {<br>                        Post post = new Post();<br>                        JSONObject object = jsonArray.getJSONObject(i);<br>                        post.setUser_name(object.getString(&quot;user_name&quot;) + &quot;secondary&quot; + i);<br>                        rowsArrayList.add(post);<br>                    }<br><br>                    int arrayListSize = rowsArrayList.size();<br>                    Log.e(TAG, &quot;Load more : arrayListSize &quot; + arrayListSize);<br><br>                    recyclerViewAdapter.notifyDataSetChanged();<br>                    isLoading = false;<br>                } catch (JSONException e) {<br>                    // throw new RuntimeException(e);<br>                    Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();<br>                } catch (IOException e) {<br>                    throw new RuntimeException(e);<br>                }<br>            }<br>        }<br><br>        @Override<br>        public void onFailure(@NonNull Call&lt;ResponseBody&gt; call, @NonNull Throwable t) {<br>            Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();<br>        }<br>    });<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*-s8sGikS5LyD8B5cm8xhsg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff08ac8c6d2a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>