Skip to main content

Posts

Showing posts from April, 2018

android kotlin - Play default ringtone example

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.content.Context import android.media.MediaPlayer import android.media.Ringtone import android.media.RingtoneManager import android.os.Bundle import android.provider.Settings import android.widget.* class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val buttonPlay = findViewById<Button>(R.id.buttonPlay) val buttonStop = findViewById<Button>(R.id.buttonStop) val buttonMediaPlay = findViewById<Button>(R.id.buttonMediaPlay) val buttonMediaStop = findViewById<Button>(R.id.buttonMediaStop) // Get the device default ringtone val ringtone: Ringtone = defaultRingtone ...

android kotlin - Turn on off do not disturb programmatically

MainActivity.kt package com.cfsuman.kotlinexamples import android.app.NotificationManager import android.content.Context import android.support.v7.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_main.* import android.widget.Toast import android.content.Intent import android.os.Build import android.provider.Settings class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the notification manager system service val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager // Click listener for button widget button_on.setOnClickListener { if (checkNotificationPolicyAccess(notificationManager)){ notificationManager.onDOD() toast("Do Not Disturb turned on.") } } ...

android kotlin - Set ringer volume programmatically

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.app.NotificationManager import android.content.Context import android.content.Intent import android.media.AudioManager import android.os.Bundle import android.provider.Settings import android.widget.* import kotlin.random.Random class MainActivity : Activity() { 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) // Get the audio manager system service val audioManager: AudioManager = getSystemService(AUDIO_SERVICE) as AudioManager // get the notification manager system service val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager // Click listener for button widget bu...

android kotlin - Set media volume programmatically

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.content.Context import android.media.AudioManager import android.os.Bundle import android.widget.* import kotlin.random.Random class MainActivity : Activity() { 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) // Get the audio manager system service val audioManager: AudioManager = getSystemService(AUDIO_SERVICE) as AudioManager // Click listener for button widget button.setOnClickListener { // Get the maximum media/music volume val maxVolume = audioManager.mediaMaxVolume // Get a random volume index in a range //val randomIndex = Random.nextInt(((maxVolume - 0) + 1) + 0) ...

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 - Bitmap scale down with 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 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 { imageView.setImageBitmap(b...

android kotlin - Rotate a bitmap

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 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 textView2 = findViewById<TextView>(R.id.textView2) val seekBar = findViewById<SeekBar>(R.id.seekBar) // get the bitmap from assets folder val bitmap = assetsToBitmap("flower2.jpg") // If bitmap is not null bitmap?.apply { // show original bitmap in first image view imageView.setImageBitmap(this) ...

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 - Convert bitmap to drawable

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.content.Context import android.graphics.* import android.graphics.drawable.BitmapDrawable import android.os.Bundle import android.widget.* 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 button = findViewById<Button>(R.id.button) // get the bitmap from assets folder val bitmap = assetsToBitmap("flower103.jpg") // If bitmap is not null bitmap?.let { imageView.setImageBitmap(bitmap) } // Click listener for button widget button.setOnClic...

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 - Launch app programmatically

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.content.Intent class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Package name of GMail app val packageName = "com.google.android.gm" // Click listener for button widget button.setOnClickListener{ // Launch the app programmatically launchApp(packageName) } } // Custom method to launch an app private fun launchApp(packageName: String) { // Get an instance of PackageManager val pm = applicationContext.packageManager // Initialize a new Intent val intent:Intent? = pm.getL...

android kotlin - Save image to Gallery 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 android.provider.MediaStore import android.graphics.drawable.BitmapDrawable import android.net.Uri import android.os.Build import android.support.v4.content.ContextCompat import kotlinx.android.synthetic.main.activity_main.* 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 } // Click listener for button widget button.setOnClickListener{ // Disable the save button it.isEnabled = false // Save the image to ...

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...