Monday, 12 March 2018

Android Progress Dialog Example using Kotlin



1. Creating New Project in Kotlin
  • Open Android Studio.
  • Go to File => New => New Project.Give Name To your application. Then, check Include Kotlin Support and click next button.
  • Select minimum SDK you need. However, we have selected 21 as minimum SDK. Then, click next button.
  • Then, select Empty Activity => click next => click finish.
2.  Here we take Simple Button and on button Click we start Progress Dialog.As in below xml layout.

main_activity.xml

<?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"
    tools:context="com.dharmendra.progressdialog.MainActivity">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Start Progress Dialog" />

</RelativeLayout>


3.MainActivity.kt



package com.dharmendra.progressdialog

import android.app.ProgressDialog
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Here We Create Object of Progress Dialog
        val progressDialog = ProgressDialog(this@MainActivity)

        //Find View By Id of Button
        val btn = findViewById<Button>(R.id.button) as Button

        //On Click of Button
        btn.setOnClickListener {

            //Set Title Of Progress Dialog
            progressDialog.setTitle("Progress Dialog")

            //Set Message Of Progress Dialog
            progressDialog.setMessage("Click on Screen to Dismiss")

            //Show  Progress Dialog
            progressDialog.show()


        }
    }
}



Output:


No comments:

Post a Comment