Skip to main content

Posts

Showing posts with the label Extra

android kotlin - Get raw resource uri

MainActivity.kt package com.example.jetpack import android.content.Context import android.net.Uri import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get raw resource uri programmatically by raw resource id // get resource uri from res/raw folder val uriById = rawResourceUriById(R.raw.yellow_flower) textView.text = uriById.toString() imageView.setImageURI(uriById) // get raw resource uri by resource name, without file extension val uriByName = rawResourceUriByName("tulip") textView2.text = uriByName.toString() imageView2.setImageURI(uriByName) } } // extension function to get raw resource uri by resource id fun Context.raw...

Android Kotlin: On back button pressed example

This example demonstrates how to customize the behavior of the back button press in an Android application written with Kotlin. The code focuses on the MainActivity.kt file, where the logic resides. Breakdown of MainActivity.kt The class MainActivity inherits from AppCompatActivity , which provides functionalities for most activities in an Android application. onCreate: This method is called when the activity is first created. Here, it sets the layout for the activity using setContentView . onBackPressed: This is the key method that gets called when the user presses the back button on the device. By default, this method simply finishes the activity, but we can override it to define custom behavior. In this example, the onBackPressed method creates an AlertDialog with two buttons: "Yes" and "No". AlertDialog: This class helps build a dialog box with a message and action buttons. Here, the dialog title and message ask for confirmation before e...

Android Kotlin: How to get string resource by name

The code demonstrates how to retrieve a string resource by its name in an Android application written in Kotlin. It defines an extension function stringFromResourcesByName that takes the resource name as a parameter and returns the corresponding string value. Here's a breakdown of the code: Import Statements: The code starts by importing necessary classes like Context , Resources , etc. These classes provide access to Android's resource framework. MainActivity Class: The MainActivity class inherits from AppCompatActivity , which is the base class for most activities in Android applications. onCreate Method: The onCreate method is the entry point of the activity's lifecycle. It's called when the activity is first created. Setting Layout: Inside the onCreate method, the setContentView method is used to set the layout file ( activity_main.xml ) for the activity. Get String Resource: The following line retrieves the string resource by name: Kotlin textView....