Skip to main content

Posts

Android Kotlin: How to add border/divider between GridView items

Android Kotlin - How to show/add border/divider between GridView items This code demonstrates how to create a simple GridView with borders between each item in Kotlin for Android development. The code is divided into two parts: MainActivity.kt: This file handles the logic and data population for the GridView. activity_main.xml: This file defines the layout for the activity, including the GridView and a TextView. Breakdown of MainActivity.kt: The onCreate function sets up the GridView by: Defining a list of plant names. Creating an ArrayAdapter for the GridView that customizes the view for each item. The custom view sets the text, centers it, and sets the background color. Setting a click listener for the GridView items to display the selected item text in a separate TextView. Key points for creating borders: The borders are created by setting the background color of each GridView item along with the horizontalSpacing and verticalSpacing properties of the GridView itself. ...

android kotlin - GridView OnItemClickListener

MainActivity.kt package com.example.jetpack import android.os.Bundle import android.widget.ArrayAdapter 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) // list of plants to populate grid view val list = listOf( "Striped alder", "Amy root", "Arizona sycamore", "Green ash", "Cherry birch", "Gray birch", "Mahogany birch", "Spice birch", "Yellow birch" ) // array adapter for grid view ArrayAdapter(this,android.R.layout.simple_list_item_1,list).apply { gridView.adapter = this } ...

Android Kotlin: CheckBox checked change listener

Android Kotlin - How to implement CheckBox checked change listener This code demonstrates how to implement a listener for a checkbox's checked state change in an Android application written in Kotlin. The code consists of two parts: the MainActivity.kt file which handles the logic and the activity_main.xml file which defines the user interface elements. Breakdown of the Code: 1. MainActivity.kt: This file defines the MainActivity class which extends AppCompatActivity . The onCreate method is responsible for initializing the activity. Inside onCreate : The layout for the activity is inflated using setContentView(R.layout.activity_main) . An anonymous listener is assigned to the checkBox using setOnCheckedChangeListener . This listener is triggered whenever the checkbox's checked state changes. The listener takes two arguments: buttonView (which is the checkbox itself) and isChecked (a boolean indicating the current checked state). Inside the listener: If the chec...