Skip to main content

Posts

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