Found 417 Articles for Kotlin

Where and how to use static variables in Android using Kotlin?

Azhar
Updated on 30-Nov-2020 13:19:29

407 Views

This example demonstrates how to use static variables in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.ktimport android.os.Bundle import android.util.Log import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    private val tag = "I'm a Static Variable"    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       setContentView(R.layout.activity_main)       title = "KotlinApp" ... Read More

How to send a variable from Activity to Fragment in Android using Kotlin?

Azhar
Updated on 30-Nov-2020 13:15:36

2K+ Views

This example demonstrates how to send a variable from Activity to Fragment in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.                 Step 3 − Add the following code to src/MainActivity.ktimport android.os.Bundle import android.widget.Button import android.widget.EditText import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentTransaction class MainActivity : AppCompatActivity() {    lateinit var editText: EditText    lateinit var button: Button    override fun onCreate(savedInstanceState: Bundle?) {   ... Read More

How to Change color of Button in Android when Clicked using Kotlin?

Azhar
Updated on 30-Nov-2020 13:11:15

2K+ Views

This example demonstrates how to Change color of Button in Android when Clicked using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.ktimport android.graphics.Color import android.os.Bundle import android.widget.Button import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       setContentView(R.layout.activity_main)       title = "KotlinApp"       val button: Button ... Read More

How to get the ActionBar height in Android using Kotlin?

Azhar
Updated on 30-Nov-2020 12:58:01

374 Views

This example demonstrates how to get the ActionBar height in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.                 Step 3 − Add the following code to src/MainActivity.ktimport android.os.Bundle import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar class MainActivity : AppCompatActivity() {    lateinit var toolbar: Toolbar    lateinit var button: Button    lateinit var textView: TextView    override fun onCreate(savedInstanceState: Bundle?) {   ... Read More

How to make an alert dialog fill 50% of screen size on Android devices using Kotlin?

Azhar
Updated on 30-Nov-2020 12:53:12

472 Views

This example demonstrates how to make an alert dialog fill 50% of screen size on Android devices using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.ktimport android.os.Bundle import android.util.DisplayMetrics import android.view.WindowManager import android.widget.Button import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    lateinit var button: Button    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       setContentView(R.layout.activity_main)   ... Read More

How to overlay two images in Android to set an imageview using Kotlin?

Azhar
Updated on 30-Nov-2020 12:49:54

923 Views

This example demonstrates how to overlay two images in Android to set an imageview using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Copy and paste two images in the res / drawable that you want to overlay(Merge) Then create a drawable resource file (layer.xml) and add the following code,             Step 4 − Add the following code to src/MainActivity.ktimport android.annotation.SuppressLint ... Read More

How to show a Soft Keyboard based on Android EditText focused using Kotlin?

Azhar
Updated on 30-Nov-2020 12:45:19

1K+ Views

This example demonstrates how to show a Soft Keyboard based on Android EditText focused using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.ktimport android.content.Context import android.os.Bundle import android.view.View import android.view.inputmethod.InputMethodManager import android.widget.EditText import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    lateinit var editText: EditText    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       setContentView(R.layout.activity_main)     ... Read More

How to set maximum date in datepicker dialog in Android using Kotlin?

Azhar
Updated on 30-Nov-2020 12:37:17

2K+ Views

This example demonstrates how to set maximum date in datepicker dialog in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.ktimport android.app.DatePickerDialog import android.os.Bundle import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import java.text.SimpleDateFormat import java.util.* class MainActivity : AppCompatActivity() {    private var year = 0    private var month = 0    private var day = 0    lateinit var textView: ... Read More

How to pass a String from one Activity to another Activity in Android using Kotlin?

Azhar
Updated on 30-Nov-2020 12:32:02

2K+ Views

This example demonstrates how to pass a String from one Activity to another Activity in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             Step 3 − Add the following code to src/MainActivity.ktimport android.content.Intent import android.os.Bundle import android.widget.Button import android.widget.EditText import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    lateinit var changeActivityButton: Button    lateinit var messageEditText: EditText    override fun onCreate(savedInstanceState: Bundle?) {       ... Read More

How to change Screen Orientation programmatically using a Button in Android Kotlin?

Azhar
Updated on 30-Nov-2020 12:27:00

1K+ Views

This example demonstrates how to change Screen Orientation programmatically using a Button in Android Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             Step 3 − Add the following code to src/MainActivity.ktimport android.content.pm.ActivityInfo import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.Toast class MainActivity : AppCompatActivity() {    lateinit var buttonLandscape: Button    lateinit var buttonPortrait: Button    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)     ... Read More

Advertisements