Skip to main content

Posts

Showing posts with the label Kotlin Better

android kotlin - RadioButton with image and text

MainActivity.kt package com.example.jetpack import android.content.Context import android.graphics.drawable.Drawable import android.os.Bundle import android.text.style.DynamicDrawableSpan import android.widget.RadioButton import androidx.appcompat.app.AppCompatActivity import androidx.core.text.set import androidx.core.text.toSpannable import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // set radio buttons icon and text programmatically setRadioImageText(radioNormal, R.drawable.ic_text_format," Normal text") setRadioImageText(radioBold, R.drawable.ic_format_bold," Bold text") setRadioImageText(radioItalic, R.drawable.ic_format_italic," Italic text") setRadioImageText(radioUnd...

android kotlin - RadioButton circle color programmatically

MainActivity.kt package com.cfsuman.kotlintutorials import android.content.res.ColorStateList import android.graphics.BlendMode import android.graphics.Color import android.graphics.PorterDuff import android.os.Build import android.os.Bundle import android.widget.* import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Get the widgets reference from XML layout val radioGroup = findViewById<RadioGroup>(R.id.radioGroup) val radioAndroid = findViewById<RadioButton>(R.id.radioAndroid) val radioKotlin = findViewById<RadioButton>(R.id.radioKotlin) val radioFireworks = findViewById<RadioButton>(R.id.radioFireworks) val radioColdFusion = findViewById<RadioButton>(R.id.radioColdFusion) val tvResult = findViewById...

android kotlin - Coroutines JSON from URL

MainActivity.kt package com.example.coroutine import android.os.Bundle import android.util.Log import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.* import kotlinx.android.synthetic.main.activity_main.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.json.JSONException import org.json.JSONObject import java.io.BufferedReader import java.io.IOException import java.io.InputStreamReader import java.net.MalformedURLException import java.net.URL class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // url to download json data val url:URL? = try { URL("https://pastebin.com/raw/2bW31yqa") }catch (e:MalformedURLException){ Log.d("Exception", e.toString()) null } // downloa...

android kotlin - Coroutines url to bitmap

MainActivity.kt package com.example.coroutine import android.graphics.Bitmap import android.graphics.BitmapFactory import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* import kotlinx.coroutines.* import java.io.IOException import java.net.URL class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // url of image val urlImage:URL = URL("https://images.pexels.com/photos/954129/" + "pexels-photo-954129.jpeg?" + "auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260") button.setOnClickListener { it.isEnabled = false // disable button // async task to get bitmap from url val result: Deferred<Bitmap?> = GlobalScope.async { urlImage.toBitm...

Android Kotlin: How to put title at the center of ActionBar

Centering the ActionBar Title in Android (Kotlin) This code demonstrates how to center the title text displayed within the ActionBar of an Android application written in Kotlin. The default behavior of the ActionBar is to align the title text to the left side. This approach utilizes a custom view to achieve the desired centered alignment. Code Breakdown The MainActivity.kt file defines the core logic for customizing the ActionBar title. Here's a breakdown of the key functionalities: Setting Up the Custom Title: Inside onCreate() , the supportActionBar?.apply block handles modifications to the ActionBar. customView = actionBarCustomTitle() assigns a custom TextView (created by the actionBarCustomTitle function) as the ActionBar's custom view. displayOptions = DISPLAY_SHOW_CUSTOM hides the default title and displays the custom view instead. Additional lines set the app icon and enable its display. Creating the Custom Title View: The actionBarCustomTitle function cre...