Skip to main content

Posts

Android Kotlin: How to create and use a HashMap

This code demonstrates how to work with HashMaps in an Android application written with Kotlin. HashMaps are a fundamental data structure used to store collections of key-value pairs. The code showcases three types of HashMaps: MutableHashMap, HashMap, and LinkedHashMap. Each type offers slight variations in behavior. The code provides a clear separation between the UI layout (defined in activity_main. xml) and the logic behind the button clicks (implemented in MainActivity. kt). When a button is clicked, it triggers specific functionalities related to the chosen HashMap type. The functionalities include adding key-value pairs, iterating through the map, removing entries, updating values, and checking for emptiness. The results are displayed on the TextView element. Summary This code provides a valuable learning resource for Android developers working with Kotlin. It offers a practical example of using different HashMap types, demonstrating their functionalities and ...

Android Kotlin: How to create and use an Enum

This code demonstrates how to create and use enums in an Android application written in Kotlin. Enums are a special kind of class that defines a set of named constants. They offer several advantages over simple string constants, including improved type safety, readability, and access to additional functionalities. The code includes an example activity ( MainActivity.kt ) that showcases three different types of enums: A basic enum with simple constants (like Position ). An enum with each constant holding a single additional value (like Capital ). An enum with each constant holding multiple additional values (like Country ). The activity displays information about these enums upon clicking corresponding buttons. Summary By following this example, you'll gain a solid understanding of how to define and utilize enums in your Android projects. This approach enhances code maintainability, readability, and reduces the risk of errors by ensuring that only valid values can be ass...

android kotlin - Palette API Example

MainActivity.kt package com.cfsuman.kotlintutorials import android.graphics.Bitmap import android.os.Bundle import android.view.View import android.widget.AdapterView import android.widget.ArrayAdapter import androidx.appcompat.app.AppCompatActivity import com.cfsuman.kotlintutorials.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) // Get the bitmap from assets val bitmap:Bitmap? = assetsBitmap("flower103.jpg") // Display the bitmap in image view binding.imageView.setImageBitmap(bitmap) // Button click listener binding.button.setOnClickListener{ // If bitmap is not null then do the next task bitmap?.apply { ...