Skip to main content

Posts

Showing posts with the label Kotlin Best

android kotlin - Coroutines download image from url

MainActivity.kt package com.example.coroutine import android.content.Context import android.content.ContextWrapper import android.graphics.Bitmap import android.graphics.BitmapFactory import android.net.Uri import android.os.Bundle import android.view.View import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* import kotlinx.coroutines.* import java.io.File import java.io.FileOutputStream import java.io.IOException import java.io.OutputStream import java.net.URL import java.util.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val context = this // url of image to download val urlImage:URL = URL("https://images.pexels.com/photos/730344/" + "pexels-photo-730344.jpeg?" + "auto=compress&cs=tinysrgb&dpr=2&h...

android kotlin - Material button disabled color example

MainActivity.kt package com.cfsuman.kotlintutorials import android.content.res.ColorStateList import android.graphics.Color import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import com.google.android.material.button.MaterialButton class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the widgets from XML layout val btnClick = findViewById<MaterialButton>(R.id.btnClick); // Create a color state list programmatically val states = arrayOf( intArrayOf(android.R.attr.state_enabled), // enabled intArrayOf(-android.R.attr.state_enabled) // disabled ) val colors = intArrayOf( Color.parseColor("#545AA7"), // enabled color Color.parseColor("#E6E6FA") // disabled color ) val colorStates...

android kotlin - Spinner onItemSelectedListener example

Spinner onItemSelectedListener The Spinner provides an easy way to select an item from a list. The Spinner always shows its currently selected item only. Other items remain invisible and users can see them when they click on the Spinner itself. On Spinner click, it displays a drop-down menu with all other available items. So, the user can select an item from all of the available items. The following android application development tutorial will demonstrate to us how we can set an item selected listener for the Spinner widget. The Spinner AdapterView OnItemSelectedListener registers a callback to invoke when an item in this AdapterView has been selected. So developer gets the Spinner selected value immediately when the user selects it or changes their selection. To populate a Spinner widget with items we have to create an item list. Then we create an instance of ArrayAdapter with this list. After that, we bind the Spinner widget with this ArrayAd...

android kotlin - Room database example

MainActivity.kt package com.cfsuman.jetpackexamples import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.text.method.ScrollingMovementMethod import kotlinx.android.synthetic.main.activity_main.* import org.jetbrains.anko.doAsync import org.jetbrains.anko.toast import org.jetbrains.anko.uiThread import java.util.* class MainActivity : AppCompatActivity() { private lateinit var mDb:RoomSingleton private val mRandom:Random = Random() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Initialize the room database mDb = RoomSingleton.getInstance(applicationContext) // Make the text view scrollable textView.movementMethod = ScrollingMovementMethod() // Insert button click listener btnInsert.setOnClickListener{ // Initialize a new student val student = Student(id = null...

android kotlin - Convert JPG to PNG to WebP programmatically

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.graphics.* import android.os.Build import android.os.Bundle import android.widget.* import java.io.ByteArrayOutputStream import java.io.IOException class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) val imageView2 = findViewById<ImageView>(R.id.imageView2) val textView = findViewById<TextView>(R.id.textView) val textView2 = findViewById<TextView>(R.id.textView2) val button = findViewById<Button>(R.id.button) // get the bitmap from assets folder val bitmap = assetsToBitmap("flower2.jpg") // If bitmap is not null bitmap?.let { ...

android kotlin - Resize bitmap keep aspect ratio

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.content.Context import android.graphics.* import android.os.Bundle import android.widget.* import java.io.IOException import kotlin.math.roundToInt class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) val imageView2 = findViewById<ImageView>(R.id.imageView2) val button = findViewById<Button>(R.id.button) val textView = findViewById<TextView>(R.id.textView) val textView2 = findViewById<TextView>(R.id.textView2) // get the bitmap from assets folder val bitmap = assetsToBitmap("flower103.jpg") // If bitmap is not null bitmap?.let { ...

android kotlin - Resize a bitmap

MainActivity.kt package com.cfsuman.kotlintutorials import android.graphics.Bitmap import android.graphics.BitmapFactory import android.os.Bundle import android.util.Log import android.widget.* import androidx.appcompat.app.AppCompatActivity import java.io.IOException import kotlin.random.Random class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the widgets reference from XML layout val button = findViewById<Button>(R.id.button) val ivOriginal = findViewById<ImageView>(R.id.ivOriginal) val ivResized = findViewById<ImageView>(R.id.ivResized) val tvOriginal = findViewById<TextView>(R.id.tvOriginal) val tvResized = findViewById<TextView>(R.id.tvResized) // Get the bitmap from assets and display into image view val bitmap = assetsToBitmap(...

android kotlin - Compress bitmap example

Compress Bitmap The bitmap is used to display an image on ImageView. Sometimes android developers have to compress a Bitmap object before displaying it on the ImageView widget. The following android application development tutorial will demonstrate to us how we can compress a bitmap in an application. We used the Kotlin programming language to write the code for this tutorial. In this tutorial XML layout file, we put two ImageView widgets, a Button widget, and a TextView widget. The first ImageView widget displays the original uncompressed bitmap on its surface. In the Button’s click event, we programmatically compress the bitmap instance. On the second ImageView widget, we displayed the compressed bitmap after applying compression with a specified quality value/percentage. The TextView widget displays the applied compression quality value/percentage on it. So, how we can compress a bitmap object programmatically using the native android SDK ...

android kotlin - Save image to external storage example

MainActivity.kt package com.cfsuman.kotlinexamples import android.content.Context import android.support.v7.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_main.* import android.graphics.Bitmap import android.graphics.drawable.BitmapDrawable import android.net.Uri import android.os.Build import android.support.v4.content.ContextCompat import java.io.File import java.io.FileOutputStream import java.io.IOException import java.io.OutputStream import java.util.* import android.os.Environment import android.widget.Toast class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ // Check run time permission for write external storage // android.permission.WRITE_EXTERNAL_STORAGE } // Get the bitmap from gi...

android kotlin - CountDownTimer days hours minutes seconds example

MainActivity.kt package com.cfsuman.kotlinexamples import android.content.Context import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import kotlinx.android.synthetic.main.activity_main.* import android.os.CountDownTimer import java.util.* import java.util.concurrent.TimeUnit class MainActivity : AppCompatActivity() { private var isCancelled = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // 60 seconds (1 minute) val minute:Long = 1000 * 60 // 1000 milliseconds = 1 second // 1 day 2 hours 35 minutes 50 seconds val millisInFuture:Long = (minute * 1440) + (minute * 155) + (1000 * 50) // Count down interval 1 second val countDownInterval:Long = 1000 // Count down timer start button button_start.setOnClickListener{ // Start the timer ...

android kotlin - CountDownTimer start stop pause resume example

MainActivity.kt package com.cfsuman.kotlinexamples import android.content.Context import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import kotlinx.android.synthetic.main.activity_main.* import android.os.CountDownTimer class MainActivity : AppCompatActivity() { private var isPaused = false private var isCancelled = false private var resumeFromMillis:Long = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val millisInFuture:Long = 50000 val countDownInterval:Long = 1000 // Count down timer start button button_start.setOnClickListener{ // Start the timer timer(millisInFuture,countDownInterval).start() it.isEnabled = false button_stop.isEnabled = true button_pause.isEnabled = true isCancelled = false ...

android kotlin - AlertDialog yes no cancel button example

AlertDialog Yes No Cancel Button The AlertDialog is a subclass of Dialog that can display one, two, or three Buttons. It can display a text message. Developers can display a more complex View on the alert dialog window using a custom View. The following android application development tutorial will demonstrate to us how we can display an alert dialog with yes, no, and a cancel button inside it. The code is written in Kotlin programming language. We created the AlertDialog using the native android SDK API. To create an alert dialog we have to initialize an instance of AlertDialog.Builder object. This AlertDialog Builder object can define the alert dialog’s various properties such as button, message, icon and etc. Finally, we can create an AlertDialog using this builder instance. So how can we display the yes, no, and cancel buttons inside the alert dialog? Using the AlertDialog Builder instance developers can set its Positive, Negative, a...

android kotlin - AlertDialog multiple choice example

AlertDialog Multiple Choice The AlertDialog is a subclass of Dialog that can display buttons, a simple text message, a list of items, single-choice items, multiple-choice items, etc. Developers can display a more complex View on the alert dialog window using a custom View. The following android application development tutorial will demonstrate to us how we can display a list of multiple choice items in the alert dialog window as the content. And how developers can be notified when users select items from the multiple choice items list via the supplied listener. Android developers can add a click listener for the alert dialog multiple choice list items. To create an alert dialog developers have to initialize an instance of AlertDialog.Builder object. This AlertDialog Builder object can define the alert dialog’s various properties such as button, message, icon, list and etc. Finally, they can create an AlertDialog using this builder instance. ...

android kotlin - AlertDialog setSingleChoiceItems example

AlertDialog setSingleChoiceItems() The AlertDialog is a subclass of Dialog that can display buttons, a simple text message, a list of items, single-choice items, multiple-choice items, etc. Developers can display a more complex View on the alert dialog window using a custom View. The following android application development tutorial will demonstrate to us how we can display a list of single-choice items in the alert dialog window as the content. And how developers can be notified when users select an item from the single choice items list via the supplied listener. Android developers can add a click listener for the alert dialog single-choice list items. To create an alert dialog developers have to initialize an instance of AlertDialog.Builder object. This AlertDialog Builder object can define the alert dialog’s various properties such as button, message, icon, list and etc. Finally, they can create an AlertDialog using this builder instance. ...