Friday, April 24, 2020

Android kotlin - ListView add header example

ListView add header in Android kotlin example

MainActivity.kt
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // list to populate list view
        val list = mutableListOf(
            "Celadon blue",
            "Alloy orange",
            "Amaranth pink",
            "Apple green",
            "Cameo pink",
            "Black chocolate"
        )
        // initialize an array adapter
        val adapter:ArrayAdapter<String> = ArrayAdapter(
            this,
            android.R.layout.simple_dropdown_item_1line,list
        )
        // attach the array adapter with list view
        listView.adapter = adapter
        // list view item click listener
        listView.onItemClickListener = OnItemClickListener{
                parent, view, position, id ->
            val selectedItem = parent.getItemAtPosition(position)
            textViewResult.text = "Selected : $selectedItem"
        }

        // get the layout for list view header view
        val header:MaterialTextView = layoutInflater.inflate(
            android.R.layout.simple_dropdown_item_1line,
            listView,
            false
        ) as MaterialTextView
        // set text for header view
        header.text = "List of colors."
        // make header text bold
        header.setTypeface(header.typeface, Typeface.BOLD)
        // give a background color to header
        header.setBackgroundColor(Color.parseColor("#C19A6B"))
        // set header text color
        header.setTextColor(Color.parseColor("#1B1811"))
        // finally, add the header view to list view
        listView.addHeaderView(
            header,
            null,
            false // make it not selectable/clickable
        )
    }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:background="#EDEAE0"
        android:choiceMode="singleChoice"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/textViewResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:textAppearance=
            "@style/TextAppearance.MaterialComponents.Subtitle1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:text="TextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
Result listview add header in kotlin Android
ListView add header example

No comments:

Post a Comment