Skip to main content

Posts

Showing posts with the label NonFormatted

Android Kotlin: EditText change underline color programmatically

The code consists of two parts: the first part is the definition of an extension function setUnderlineColor that can be used to change the underline color of an EditText, and the second part is the implementation of a simple activity that demonstrates how to use the setUnderlineColor function. The setUnderlineColor function takes an integer parameter color that represents the color of the underline. The function first calls the mutate method on the EditText's background drawable. This is necessary because the setColorFilter method modifies the drawable, and we don't want to modify the original drawable that's defined in the XML layout. Next, the function checks the API level using Build.VERSION.SDK_INT . If the API level is greater than or equal to Build.VERSION_CODES.Q (Android 10), then the function sets a BlendModeColorFilter on the background drawable. Otherwise, it sets a PorterDuff color filter. The BlendModeColorFilter and PorterDuff color filter both achi...

android kotlin - Coroutines get html from url

MainActivity.kt package com.example.coroutine import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.view.View import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.lifecycleScope import kotlinx.android.synthetic.main.activity_main.* import kotlinx.coroutines.* import java.io.IOException import java.net.MalformedURLException import java.net.URL class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // make text view text scrollable textView.movementMethod =ScrollingMovementMethod() // url to get html val url: URL? = try { URL("https://developer.android.com/kotlin/") } catch (e: MalformedURLException) { // catch invalid url exception button.isEnabled = false textView.text = e.message...