How to add Bullet list in a RecyclerView in Android?

Recycler View allows us to show a list of items but to convert our list into the bulleted list we have to do some extra work. You can do this task by following these simple steps given below:-

  1. Add the support Library in build.gradle file for Recycler View in the dependencies section.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <dependency>
        implementation 'androidx.recyclerview:recyclerview:1.1.0'
    </dependency>

    chevron_right

    
    

  2. Create a bullet.xml file in the drawable folder.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        android:shape="oval">
        <padding
            android:left="8dp"
            android:right="8dp"/>
        <size
            android:width="6dp"
            android:height="6dp"/>
        <solid
            android:color="#219806"/>
    </shape>

    chevron_right

    
    

  3. In activity_main.xml, add the following code.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0" encoding="utf-8"?>
      
    <androidx.constraintlayout.widget.ConstraintLayout 
        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:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/recycler_view"
            />
      
    </androidx.constraintlayout.widget.ConstraintLayout>

    chevron_right

    
    

  4. Create a new custom_layout.xml file with the following code.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:layout_width="match_parent"
        android:id="@+id/show_name_layout"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="5dp"
        >
        <TextView
            android:drawableLeft="@drawable/bullet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <TextView
            android:layout_marginStart="2dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView"
            android:textSize="20sp"
            />
    </LinearLayout>

    chevron_right

    
    

  5. Create a MyAdapter.java class and add the following code.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    public class MyAdapter
        extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
      
        String text[];
        Activity activity;
      
        public MyAdapter(Activity activity,
                         String text[])
        {
            this.activity = activity;
            this.text = text;
        }
      
        // This method is used to attach
        // custom layout to the recycler view
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(
            @NonNull ViewGroup parent,
            int viewType)
        {
      
            View view
                = activity.getLayoutInflater()
                      .inflate(
                          R.layout.custom_layout,
                          parent, false);
            return new MyViewHolder(view);
        }
      
        // This method is used to set the action
        // to the widgets of our custom layout.
        @Override
        public void onBindViewHolder(
            @NonNull MyViewHolder holder,
            int position)
        {
            holder.textView.setText(text[position]);
        }
      
        @Override
        public int getItemCount()
        {
            return text.length;
        }
      
        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView textView;
            public MyViewHolder(@NonNull View itemView)
            {
                super(itemView);
                textView = itemView.findViewById(R.id.textView);
            }
        }
    }

    chevron_right

    
    

  6. Finally, in MainActivity.java add the following code.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    public class MainActivity
        extends AppCompatActivity {
      
        RecyclerView recyclerView;
        String text[]
            = { "Linear Search", "Binary Search", "Selection Sort",
                "Bubble Sort", "Insertion Sort",
                "Recursive Insertion Sort", "Merge Sort",
                "Iterative Merge Sort", "Quick Sort",
                "Heap Sort", "Counting Sort",
                "Klee’s Algorithm", "Karatsuba algorithm",
                "Dijkastra’s Shortest Path Algorithm",
                "Dial’s Algorithm",
                "Kruskal’s Minimum Spanning Tree",
                "Prim’s Minimum Spanning Tree",
                "Naive Pattern Searching",
                "KMP Algorithm", "Rabin-Karp Algorithm" };
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            recyclerView = findViewById(R.id.recycler_view);
      
            MyAdapter adapter = new MyAdapter(this, text);
            recyclerView.setLayoutManager(
                new LinearLayoutManager(this));
            recyclerView.setAdapter(adapter);
        }
    }

    chevron_right

    
    

Output:

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.