This example demonstrates how to dynamically update a ListView on 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 androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.ArrayAdapter import android.widget.Button import android.widget.EditText import android.widget.ListView class MainActivity : AppCompatActivity() { lateinit var editText: EditText lateinit var button: Button lateinit var listView: ListView var list: ArrayList = ArrayList() lateinit ... Read More
This example demonstrates how to check if a Bluetooth device is connected with an Android device..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.bluetooth.BluetoothDevice import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = ... Read More
This example demonstrates how to display a list view in an Android Alert Dialog.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.view.View import android.widget.ArrayAdapter import android.widget.ListView import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { var country = arrayOf( "India", "Brazil", "Argentina", "Portugal", "France", "England", "Italy" ) ... Read More
Subscriber interface subscribes to publishers to receive items through onNext() method, error message through the onError() method, or a signal that no more items to be expected through the onComplete() method. Before any of those things happen, the publisher calls onSubscription() method.public interface Subscriber { public void onSubscribe(Subscription s); public void onNext(T t); public void onError(Throwable t); public void onComplete(); }Rules for Subscriber interface:A Subscriber must call through Subscription.request(long n) method to receive onNext() signals.Subscriber.onComplete() and Subscriber.onError(Throwable t) methods must not call any methods on Subscription or Publisher.Subscriber.onComplete() and Subscriber.onError(Throwable t) methods must consider the Subscription canceled after received ... Read More
This example demonstrates how to start Service using Alarmmanager in Kotlin Android.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.AlarmManager import android.app.PendingIntent import android.content.Context import android.content.Intent import android.os.Bundle import android.widget.Button import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import java.util.* class MainActivity : AppCompatActivity() { private lateinit var btnStart: Button private lateinit var btnStop: Button lateinit var pendingIntent: PendingIntent ... Read More
The DateTime.ToFileTimeUtc() method in C# is used to convert the value of the current DateTime object to a Windows file time.SyntaxFollowing is the syntax −public long ToFileTimeUtc ();ExampleLet us now see an example to implement the DateTime.ToFileTimeUtc() method −using System; public class Demo { public static void Main() { DateTime d = new DateTime(2019, 05, 10, 6, 10, 25); Console.WriteLine("Date = {0}", d); long res = d.ToFileTimeUtc(); Console.WriteLine("Windows file time (Utc) = {0}", res); } }OutputThis will produce the following output −Date = 5/10/2019 6:10:25 AM ... Read More
Java 9 added ProcessHandle interface to Process API to enhance Process class. An instance of the ProcessHandle interface identifies a local process that allows us to query process status and managing processes, and ProcessHandle.Info allows us to use local code because of the need to obtain the PID of a local process.ProcessBuilder class can be used to create separate operating system processes. In the below example, we can create a process of "notepad" application by using the ProcessBuilder class.Exampleimport java.time.ZoneId; import java.util.stream.Stream; import java.util.stream.Collectors; import java.io.IOException; public class ProcessBuilderTest { public static void main(String args[]) throws IOException { ProcessBuilder pb = new ProcessBuilder("notepad.exe"); ... Read More
This example demonstrates how to pass an image 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.view.View import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" } fun ... Read More
This example demonstrates how Save, edit, retrieve, delete shared preference data in 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.content.SharedPreferences import android.os.Bundle import android.view.View import android.widget.EditText import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") class MainActivity : AppCompatActivity() { var editTextName: EditText? = ... Read More
This example demonstrates how to draw a smooth line following my finger using 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.graphics.Bitmap import android.graphics.Canvas import android.graphics.Color import android.graphics.Paint import android.os.Bundle import android.view.MotionEvent import android.view.View import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity(), View.OnTouchListener { private lateinit var imageView: ImageView private var bitmap: Bitmap? = null var ... Read More