Showing posts with label widget. Show all posts
Showing posts with label widget. Show all posts

Monday, 12 March 2018

Android Progress Bar 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 Progress Bar (Horizontal) and Button.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.progressbar.MainActivity">


    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="85dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progressBar"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp"
        android:text="Button" />


</RelativeLayout>


3.MainActivity.kt



package com.dharmendra.progressbar

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.Button
import android.widget.ProgressBar

class MainActivity : AppCompatActivity() {

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

        //Find  view By Id for ProgressBar,Button

        val progressBar = findViewById<ProgressBar>(R.id.progressBar) as ProgressBar
        val btn = findViewById<Button>(R.id.button) as Button

        //On Click for Button
        btn.setOnClickListener {


            //Set button Clickable false to stop multiple Clicks at a time
            btn.isClickable = false

            //Here We Create one Thread to change Status of Progress bar
            var status = 0   //Initial status of Progress Bar

            Thread(Runnable {

                while (status < 100) {

                    status += 1


                    //if error Occures than it will habdle in try-catch block.

                    try {

                        Thread.sleep(30)
                        progressBar.setProgress(status)
                    } catch (e: Exception) {

                        Log.e("Error", e.message)
                    }


                }


                //Set Button Clickable true after Progress Complete
                btn.isClickable = true

            }).start()

        }


    }
}



Output:

Sunday, 11 March 2018

Android Switch Widget 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 Switch in our Layout . 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.aswitch.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="50dp"
        android:layout_marginTop="139dp"
        android:text="Switch :"
        android:textSize="20dp"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/textView" />

    <Switch
        android:id="@+id/sw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

3. MainActivity.kt

package com.dharmendra.aswitch

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Switch
import android.widget.Toast

class MainActivity : AppCompatActivity() {

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

        //Find View By Id for Switch

        val s=findViewById<Switch>(R.id.sw) as Switch

        //Onclik For Switch

        s.setOnClickListener {

            //To Chek Switch is On Or Off
            if(s.isChecked){
                Toast.makeText(this@MainActivity,"Switch is On",Toast.LENGTH_LONG).show()
            }

            else{
                Toast.makeText(this@MainActivity,"Switch is Off",Toast.LENGTH_LONG).show()
            }
        }
    }
}


Output:


Android Spinner/Drop Down Menu 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 Spinner in our Layout . 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.spinner.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="73dp"
        android:gravity="center"
        android:textSize="20dp"
        android:textStyle="bold"
        android:textColor="@android:color/holo_blue_dark"
        android:text="Select Country"
        android:id="@+id/textView" />

    <Spinner
        android:id="@+id/country"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:layout_marginStart="16dp"
        android:layout_marginTop="26dp" />


</RelativeLayout>


3. MainActivity.kt

package com.dharmendra.spinner

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*

class MainActivity : AppCompatActivity() {

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

        //Find View by id for Spinner and Button


        val spinner=findViewById<Spinner>(R.id.country) as Spinner

        //Create An Array which Contain Country name

        val country= arrayOf("India","United States","Canada","Russia","Brazil")

        //Set Array in Adapter

        val adapter=ArrayAdapter(this@MainActivity,android.R.layout.simple_spinner_dropdown_item,country)

        spinner.adapter=adapter


      spinner.onItemSelectedListener=object :AdapterView.OnItemSelectedListener{
          override fun onNothingSelected(p0: AdapterView<*>?) {
              TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
          }

          override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {

              Toast.makeText(this@MainActivity,"You have Selected "+country[p2],Toast.LENGTH_LONG).show()

          }

      }



    }
}

Output: