Lists are common way of representing data in any mobile application. In this example I’ll create a list view of all countries using Kotlin for Android. This implementation will include the necessary components to display country names in a RecyclerView.
First, let’s organize this into several parts:
// 1. First, let's define a Country data class
data class Country(val name: String, val code: String)
// 2. Create an adapter for the RecyclerView
class CountriesAdapter(private val countries: List<Country>) :
RecyclerView.Adapter<CountriesAdapter.CountryViewHolder>() {
class CountryViewHolder(val binding: ItemCountryBinding) : RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CountryViewHolder {
val binding = ItemCountryBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return CountryViewHolder(binding)
}
override fun onBindViewHolder(holder: CountryViewHolder, position: Int) {
val country = countries[position]
holder.binding.apply {
tvCountryName.text = country.name
tvCountryCode.text = country.code
}
}
override fun getItemCount(): Int = countries.size
}
// 3. MainActivity implementation
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setupRecyclerView()
}
private fun setupRecyclerView() {
val countries = getCountriesList()
val adapter = CountriesAdapter(countries)
binding.recyclerViewCountries.apply {
this.adapter = adapter
layoutManager = LinearLayoutManager(this@MainActivity)
addItemDecoration(DividerItemDecoration(this@MainActivity, DividerItemDecoration.VERTICAL))
}
}
private fun getCountriesList(): List<Country> {
return listOf(
Country("Afghanistan", "AF"),
Country("Albania", "AL"),
Country("Algeria", "DZ"),
Country("Andorra", "AD"),
Country("Angola", "AO"),
Country("Antigua and Barbuda", "AG"),
Country("Argentina", "AR"),
Country("Armenia", "AM"),
Country("Australia", "AU"),
Country("Austria", "AT"),
Country("Azerbaijan", "AZ"),
Country("Bahamas", "BS"),
Country("Bahrain", "BH"),
Country("Bangladesh", "BD"),
Country("Barbados", "BB"),
Country("Belarus", "BY"),
Country("Belgium", "BE"),
Country("Belize", "BZ"),
Country("Benin", "BJ"),
Country("Bhutan", "BT"),
Country("Bolivia", "BO"),
Country("Bosnia and Herzegovina", "BA"),
Country("Botswana", "BW"),
Country("Brazil", "BR"),
Country("Brunei", "BN"),
Country("Bulgaria", "BG"),
Country("Burkina Faso", "BF"),
Country("Burundi", "BI"),
Country("Cabo Verde", "CV"),
Country("Cambodia", "KH"),
Country("Cameroon", "CM"),
Country("Canada", "CA"),
Country("Central African Republic", "CF"),
Country("Chad", "TD"),
Country("Chile", "CL"),
Country("China", "CN"),
Country("Colombia", "CO"),
Country("Comoros", "KM"),
Country("Congo", "CG"),
Country("Costa Rica", "CR"),
Country("Croatia", "HR"),
Country("Cuba", "CU"),
Country("Cyprus", "CY"),
Country("Czech Republic", "CZ"),
Country("Democratic Republic of the Congo", "CD"),
Country("Denmark", "DK"),
Country("Djibouti", "DJ"),
Country("Dominica", "DM"),
Country("Dominican Republic", "DO"),
Country("Ecuador", "EC"),
Country("Egypt", "EG"),
Country("El Salvador", "SV"),
Country("Equatorial Guinea", "GQ"),
Country("Eritrea", "ER"),
Country("Estonia", "EE"),
Country("Eswatini", "SZ"),
Country("Ethiopia", "ET"),
Country("Fiji", "FJ"),
Country("Finland", "FI"),
Country("France", "FR"),
Country("Gabon", "GA"),
Country("Gambia", "GM"),
Country("Georgia", "GE"),
Country("Germany", "DE"),
Country("Ghana", "GH"),
Country("Greece", "GR"),
Country("Grenada", "GD"),
Country("Guatemala", "GT"),
Country("Guinea", "GN"),
Country("Guinea-Bissau", "GW"),
Country("Guyana", "GY"),
Country("Haiti", "HT"),
Country("Honduras", "HN"),
Country("Hungary", "HU"),
Country("Iceland", "IS"),
Country("India", "IN"),
Country("Indonesia", "ID"),
Country("Iran", "IR"),
Country("Iraq", "IQ"),
Country("Ireland", "IE"),
Country("Israel", "IL"),
Country("Italy", "IT"),
Country("Jamaica", "JM"),
Country("Japan", "JP"),
Country("Jordan", "JO"),
Country("Kazakhstan", "KZ"),
Country("Kenya", "KE"),
Country("Kiribati", "KI"),
Country("Kuwait", "KW"),
Country("Kyrgyzstan", "KG"),
Country("Laos", "LA"),
Country("Latvia", "LV"),
Country("Lebanon", "LB"),
Country("Lesotho", "LS"),
Country("Liberia", "LR"),
Country("Libya", "LY"),
Country("Liechtenstein", "LI"),
Country("Lithuania", "LT"),
Country("Luxembourg", "LU"),
Country("Madagascar", "MG"),
Country("Malawi", "MW"),
Country("Malaysia", "MY"),
Country("Maldives", "MV"),
Country("Mali", "ML"),
Country("Malta", "MT"),
Country("Marshall Islands", "MH"),
Country("Mauritania", "MR"),
Country("Mauritius", "MU"),
Country("Mexico", "MX"),
Country("Micronesia", "FM"),
Country("Moldova", "MD"),
Country("Monaco", "MC"),
Country("Mongolia", "MN"),
Country("Montenegro", "ME"),
Country("Morocco", "MA"),
Country("Mozambique", "MZ"),
Country("Myanmar", "MM"),
Country("Namibia", "NA"),
Country("Nauru", "NR"),
Country("Nepal", "NP"),
Country("Netherlands", "NL"),
Country("New Zealand", "NZ"),
Country("Nicaragua", "NI"),
Country("Niger", "NE"),
Country("Nigeria", "NG"),
Country("North Korea", "KP"),
Country("North Macedonia", "MK"),
Country("Norway", "NO"),
Country("Oman", "OM"),
Country("Pakistan", "PK"),
Country("Palau", "PW"),
Country("Palestine", "PS"),
Country("Panama", "PA"),
Country("Papua New Guinea", "PG"),
Country("Paraguay", "PY"),
Country("Peru", "PE"),
Country("Philippines", "PH"),
Country("Poland", "PL"),
Country("Portugal", "PT"),
Country("Qatar", "QA"),
Country("Romania", "RO"),
Country("Russia", "RU"),
Country("Rwanda", "RW"),
Country("Saint Kitts and Nevis", "KN"),
Country("Saint Lucia", "LC"),
Country("Saint Vincent and the Grenadines", "VC"),
Country("Samoa", "WS"),
Country("San Marino", "SM"),
Country("Sao Tome and Principe", "ST"),
Country("Saudi Arabia", "SA"),
Country("Senegal", "SN"),
Country("Serbia", "RS"),
Country("Seychelles", "SC"),
Country("Sierra Leone", "SL"),
Country("Singapore", "SG"),
Country("Slovakia", "SK"),
Country("Slovenia", "SI"),
Country("Solomon Islands", "SB"),
Country("Somalia", "SO"),
Country("South Africa", "ZA"),
Country("South Korea", "KR"),
Country("South Sudan", "SS"),
Country("Spain", "ES"),
Country("Sri Lanka", "LK"),
Country("Sudan", "SD"),
Country("Suriname", "SR"),
Country("Sweden", "SE"),
Country("Switzerland", "CH"),
Country("Syria", "SY"),
Country("Taiwan", "TW"),
Country("Tajikistan", "TJ"),
Country("Tanzania", "TZ"),
Country("Thailand", "TH"),
Country("Timor-Leste", "TL"),
Country("Togo", "TG"),
Country("Tonga", "TO"),
Country("Trinidad and Tobago", "TT"),
Country("Tunisia", "TN"),
Country("Turkey", "TR"),
Country("Turkmenistan", "TM"),
Country("Tuvalu", "TV"),
Country("Uganda", "UG"),
Country("Ukraine", "UA"),
Country("United Arab Emirates", "AE"),
Country("United Kingdom", "GB"),
Country("United States", "US"),
Country("Uruguay", "UY"),
Country("Uzbekistan", "UZ"),
Country("Vanuatu", "VU"),
Country("Vatican City", "VA"),
Country("Venezuela", "VE"),
Country("Vietnam", "VN"),
Country("Yemen", "YE"),
Country("Zambia", "ZM"),
Country("Zimbabwe", "ZW")
)
}
}
// 4. Layout files below:
// layout/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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewCountries"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/item_country" />
</androidx.constraintlayout.widget.ConstraintLayout>
// layout/item_country.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:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/tvCountryName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/tvCountryCode"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="United States" />
<TextView
android:id="@+id/tvCountryCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/darker_gray"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="US" />
</androidx.constraintlayout.widget.ConstraintLayout>
// 5. Don't forget to add the necessary dependencies in build.gradle (app level)
/*
dependencies {
implementation 'androidx.core:core-ktx:1.10.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.recyclerview:recyclerview:1.3.0'
// For View Binding
buildFeatures {
viewBinding true
}
}
*/
To set up this project, you’ll need to:
- Create a new Android project with Kotlin support
- Set up View Binding in your app-level build.gradle file
- Create the layout files as shown above
- Implement the Kotlin code
The implementation includes:
- A
Country
data class to hold country information (name and country code) - A
CountriesAdapter
class that extends RecyclerView.Adapter to display the countries - The
MainActivity
that sets up the RecyclerView with a complete list of countries - XML layouts for both the main activity and individual country list items
This approach displays each country with its name and two-letter country code. The RecyclerView is configured with a LinearLayoutManager and includes dividers between items for better readability.
You can extend this implementation by:
- Adding country flags using a library like Flagkit
- Implementing search functionality to filter countries
- Adding click listeners to show more details about each country
- Using a more sophisticated data source like a remote API or database
Thank you for reading!