title: How to add a Recycler View to an activity in Java
body: <p>I have tried to create <code>RecyclerView</code> in an <code>Activity</code>, but I don't get the correct way to add a <code>RecyclerView</code>.
  I searched on the Internet and saw a lot of videos on it, but the method is different on every website, so I was confused.
  I want to create a <code>RecyclerView</code> that displays flags and Names of all the list of countries in Asia.</p>
  <p>Here is my code</p>
  <p><strong>Main Activity</strong></p>
  <pre><code>    import androidx.appcompat.app.AppCompatActivity
      import android.os.Bundle
      import androidx.recyclerview.widget.LinearLayoutManager
      import androidx.recyclerview.widget.RecyclerView
      import kotlinx.android.synthetic.main.activity_main.view.*
  
      class MainActivity : AppCompatActivity() {
  
          lateinit var recyclerDashboard: RecyclerView
  
          lateinit var layoutManager:RecyclerView.LayoutManager
          
          val sumList= arrayListOf(&quot;Russia&quot;,&quot;China&quot;,&quot;India&quot;,&quot;Kazakhstan&quot;,&quot;Saudi Arabia&quot;,&quot;Iran&quot;,&quot;Mangolia&quot;,&quot;Indonesia&quot;,&quot;Pakistan&quot;,&quot;Turkey&quot;,&quot;Mayanmar&quot;,&quot;Afghanistan&quot;,&quot;Yemen&quot;,&quot;Thailand&quot;,&quot;Turkmenistan&quot;,&quot;Uzbekistan&quot;,&quot;Iraq&quot;,&quot;Japan&quot;,&quot;Vietnam&quot;,&quot;Malaysia&quot;,
          &quot;Oman&quot;,&quot;Philippines&quot;,&quot;Iaos&quot;,&quot;kyrgyzstan&quot;,&quot;Syria&quot;,&quot;Combodia&quot;,&quot;Bangladesh&quot;,&quot;Nepal&quot;,&quot;Tujikistan&quot;,&quot;North Korea&quot;,&quot;South Korea&quot;,&quot;Azerbaijan&quot;,&quot;United Arab Emirates&quot;,
          &quot;Georgia&quot;, &quot;Sri Lanka&quot;,&quot;Bhutan&quot;,&quot;Tiwan&quot;,&quot;Armenia&quot;,&quot;Israel&quot;,&quot;Kuwait&quot;,&quot;Timor Leste&quot;,&quot;Qatar&quot;,&quot;Lebanon&quot;,&quot;Cyprus&quot;,&quot;Pelastine&quot;,&quot;Brunei&quot;,&quot;Bahrain&quot;,&quot;Singapore&quot;,&quot;Maldives&quot;)
  
          lateinit var recyclerAdapter: DashboardRecyclerAdapter
  
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              setContentView(R.layout.activity_main)
  
              recyclerDashboard=findViewById(R.id.recyclerDashboard)
              layoutManager=LinearLayoutManager(this)
              recyclerAdapter= DashboardRecyclerAdapter(sumList)
          }
      }
  </code></pre>
  <p><strong>activity_main.xml</strong></p>
  <pre><code>    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
      &lt;RelativeLayout
          xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
          xmlns:tools=&quot;http://schemas.android.com/tools&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          tools:context=&quot;.MainActivity&quot;&gt;
  
         &lt;androidx.recyclerview.widget.RecyclerView
          android:id=&quot;@+id/recyclerDashboard&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_margin=&quot;5dp&quot;
          android:scrollbars=&quot;vertical&quot;
          android:padding=&quot;10dp&quot;
          tools:listitem=&quot;@layout/recycler_dashboard_single_row&quot; /&gt;
  
  
      &lt;/RelativeLayout&gt;
  </code></pre>
  <p><strong>Data Class</strong></p>
  <pre><code>    data class User(
          val id:Int=0,
          val country_name:String?=null,
          var country_capital:String?=null,
          var country_flag:String?=null,
          var country_region:String?=null,
          var country_subregion:String?=null,
          var country_population:String?=null,
          var country_borders:String?=null,
          var country_languages:String?=null
      )
  </code></pre>
  <p><strong>Adapter Class</strong></p>
  <pre><code>    package com.bindu.asiancountries
      import android.view.LayoutInflater
      import android.view.View
      import android.view.ViewGroup
      import android.widget.ImageView
      import android.widget.TextView
      import androidx.recyclerview.widget.RecyclerView
  
      class DashboardRecyclerAdapter(
          val itemList:ArrayList&lt;String&gt;
      ):RecyclerView.Adapter&lt;DashboardRecyclerAdapter.ViewHolder&gt;() {
  
          override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
              val v=LayoutInflater.from(parent.context).inflate(R.layout.recycler_dashboard_single_row,parent,false)
              
              return ViewHolder(v)
          }
  
          override fun getItemCount(): Int {
              return itemList.size
          }
  
          override fun onBindViewHolder(holder: ViewHolder, position: Int) {
              val text=itemList[position]
          
              holder.textViewName.text= text
          }
  
          class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
              val image:ImageView=itemView.findViewById(R.id.russia)
              val textViewName :TextView= itemView.findViewById(R.id.txtRussia)
          }
      }
  </code></pre>
  <p><strong>Item xml</strong></p>
  <pre><code>    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
      &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
  
          &lt;androidx.cardview.widget.CardView
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:layout_margin=&quot;10dp&quot;
              app:cardCornerRadius=&quot;4dp&quot;
              app:cardElevation=&quot;4dp&quot;&gt;
  
              &lt;RelativeLayout
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;&gt;
  
                  &lt;ImageView
                      android:layout_width=&quot;100dp&quot;
                      android:id=&quot;@+id/russia&quot;
                      android:layout_height=&quot;100dp&quot;
                      android:src=&quot;@drawable/russia&quot;
                      android:layout_margin=&quot;20dp&quot;/&gt;
  
                  &lt;TextView
                      android:id=&quot;@+id/txtRussia&quot;
                      android:layout_width=&quot;wrap_content&quot;
                      android:layout_toEndOf=&quot;@id/russia&quot;
                      android:layout_marginStart=&quot;120dp&quot;
                      android:textSize=&quot;30sp&quot;
                      android:layout_marginTop=&quot;50dp&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:text=&quot;@string/russia&quot; /&gt;
  
              &lt;/RelativeLayout&gt;
  
          &lt;/androidx.cardview.widget.CardView&gt;
  
      &lt;/RelativeLayout&gt;
  </code></pre>