Showing posts sorted by relevance for query kotlin. Sort by date Show all posts
Showing posts sorted by relevance for query kotlin. Sort by date Show all posts

Wednesday 5 December 2018

Kotlin-bind OnClickListener on view

Hello Friends,
 In this tutorial I am showing you how to bind the click listener on View in Kotlin.

1. Code snippet to set OnClickListener on Textview in Kotlin Android
val tvStatic = findViewById(R.id.tv_static) as TextView
tvStatic.setOnClickListener {
 // your code to perform when the user clicks on the TextView
 Toast.makeText(this, "You clicked on TextView 'Click Me'.", Toast.LENGTH_SHORT).show()
}

2. Code snippet to set OnClickListener on Button in Kotlin Android
// get reference to button
val btn_click = findViewById(R.id.btn_click_me) as Button
// set on-click listener
btn_click.setOnClickListener {
    Toast.makeText(this@MainActivity, "You clicked on Button.", Toast.LENGTH_SHORT).show()
}

3. Code snippet to set OnClickListener on ImageView in Kotlin Android
// get reference to ImageView
val iv_profile = findViewById(R.id.image_view) as ImageView
// set on-click listener for ImageView
iv_profile.setOnClickListener {
    // your code here
}

Hope this will help someone.
Enjoy Coding..... :)

Monday 11 May 2020

Kotlin - Android notification in kotlin | Android MVP in Kotlin

Hello Friends,
       Lets first tell you what is notification.


Notification is a message that is used to display some short messages outside of our main application. Even if the app is not running, notifications will still work. Notifications have the following contents: an icon, title of notification and some text content.

Now today I am going to share , how create notification in Kotlin.

Download code from here
Hope this will helps someone..
Enjoy Coding.... :)

Wednesday 5 December 2018

Kotlin-Create TextView Programatically

Hello Friends,
      In this tutorial I am going create a textview dynamically in kotlin and add it to Linearlayout layout.

1. activity_main.xml: Following is the activity_main.xml containing the TextView with the text .

<?xml version="1.0" encoding="utf-8"?>  
 <android.support.constraint.ConstraintLayout xmlns:android="https://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=".TextViewSample">  
  <LinearLayout  
   android:id="@+id/ll_main_layout"  
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"  
   android:orientation="vertical">  
   <TextView  
    android:id="@+id/tv_static"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:textSize="20sp"  
    android:padding="20sp"  
    android:justificationMode="inter_word"  
    android:text="This textview is created from xml"/>  
  </LinearLayout>  
 </android.support.constraint.ConstraintLayout>

2. Creation of textview dynamically,
val tv_programtically = TextView(this)
tv_programtically.textSize = 20f
tv_programtically.text = "This textview is an dynamic textview in kotlin"

3. Finally the complete code is:

  TextViewSample.kt
package com.android.developer.kotlinsample

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.TextView
class TextViewSample : AppCompatActivity() {

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

    /** Dynamic creation of text view */
    val tv_programtically = TextView(this)
    tv_programtically.textSize = 20f
    tv_programtically.text = "This textview is an dynamic textview in kotlin"
    /**------end--------**/

    // add TextView to LinearLayout
    val ll_main_layout = findViewById(R.id.ll_main_layout) as LinearLayout
    ll_main_layout.addView(tv_programtically)
  }
}
Hope this will help some one. Enjoy Coding.... :)

Monday 11 May 2020

Kotlin - Enum Classes in Kotlin | Enum Methods | Enum Properties

Hello Friend,
              Today I am going to share about Enum class in Kotlin
 

- How to create/initialize enum classes
- methods and properties of enum classes.



Initializing enums –


Now we can easily access the color of fruits,
Enum Methods-

  1. values: This method returns a list of all the constants defined within the enum class.
  2. valueOf: This methods returns the enum constant defined in enum, matching the input string.  If the constant, is not present in the enum, then an IllegalArgumentException is thrown.

Enum Properties-

  1. ordinal: This property stores the ordinal value of the constant, which is usually a zero-based index.
  2. name: This property stores the name of the constant.
Below is the example which help us to understand the uses of Enum method and Properties.
Outputs:

Hope this will help some one.
Enjoy Coding... :)

Saturday 5 October 2019

Kotlin Android – AlertDialog – Example

Hello Friends,
                Here is the demo Alert Dialog in Kotlin. Android AlertDialog class
                is used to display a dialog box to user with positive and negative buttons.
It Appears on top of the activity layout. You may not physically access any other
UI components of activity. It will be run on UI thread.






To Create an AlertDialog, step by step process is :

1. Create an AlertDialog Builder using the activity’s context.
2. Set message content using the builder.
3. Set Positive Button Text and Action to be taken when the button is clicked using the builder.
4. Set Negative Button Text and Action to be taken when the button is clicked using the builder.
5. Create AlertDialog from the builder.
6. You may set the title to the AlertDialog box using setTitle() method.

1. MainActivity.kt
package com.android.developer.soulutions.myapplication

import android.content.DialogInterface
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AlertDialog
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btnShowAlert.setOnClickListener {
            // build alert dialog
            val dialogBuilder = AlertDialog.Builder(this)

            // set message of alert dialog
            dialogBuilder.setMessage("Do you want to close this  ?")
                    // if the dialog is cancelable
                    .setCancelable(false)
                    // positive button text and action
                    .setPositiveButton("OK", DialogInterface.OnClickListener { dialog, id ->
                        finish()
                    })
                    // negative button text and action
                    .setNegativeButton("Cancel", DialogInterface.OnClickListener { dialog, id ->
                        dialog.cancel()
                    })

            // create dialog box
            val alert = dialogBuilder.create()
            // set title for alert dialog box
            alert.setTitle("AlertDialogExample")
            // show alert dialog
            alert.show()
        };
    }
}
 

Download the code from here

Hope this will helps someone.
Enjoy Coding................... :)


Wednesday 1 September 2021

Android Jetpack Compose- Android Studio with Jetpack Compose Getting Started | Jetpack Compose Tutoria


Hi All,
  
Today I am going to share my first JetPack Compose Tutorial. Here we learn how\nto setup android compose in android studio. We see the rquired dependency and other settings.

Step 1: Installation:  First we have to download Android Studio Arctic Fox. That’s because when we use Android Studio to develop our app with Jetpack Compose, we can benefit from smart editor features, such as New Project templates and the ability to immediately
preview our Compose UI.

Step2: Create android app :  After the installint of latest android studio from link now lets 
create an app.
  • Open Android Studio > select File > New > New Project from the menu bar.  
  • In the Select a Project Template window, select Empty Compose Activity and click Next.
  • In the Configure your project window, do the following:
    1. Set the NamePackage name, and Save location as you normally would.
    2. Note that, in the Language dropdown menu, Kotlin is the only available option because Jetpack Compose works only with classes written in Kotlin.
    3. In the Minimum API level dropdown menu, select API level 21 or higher.
  • Click Finish.  


   You can see below dependecny in your app build.gradle file which is required for Compose.
    
 



 For more check this tutorial

While setting it if you are facing issue of  "Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8". Plesase check this tutorial

Hope this will help some one.
Enjoy Coding....... :)

Monday 7 October 2019

Kotlin Android - RecyclerView Example

Hello Friends,
          Today I am sharing the demo of RecyclerView in Kotlin.
A RecyclerView is essentially a ViewGroup of containers called ViewHolders which
populate a particular item.



So lets first familiar with RecyclerView and What RecyclerView requires:
1. It requires a set of data objects to work with
2. An xml file of the individual view item
3. An adapter to bind that data to the views shown in the ViewHolders
4. ViewHolder to populate the UI from the xml item file


Getting Started

Download code from here
Hope this will helps someone.
Enjoy coding.... :)

 

Copyright @ 2013 Android Developers Blog.