Open In App

How to add Bullet list in a RecyclerView in Android?

Improve
Improve
Like Article
Like
Save
Share
Report

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:-

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

XML




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


Create a bullet.xml file in the drawable folder

XML




<?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>


In activity_main.xml, add the following code. 

XML




<?xml version="1.0" encoding="utf-8"?>
 
<androidx.constraintlayout.widget.ConstraintLayout
    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>


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

XML




<?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>


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

Java




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);
        }
    }
}


Finally, in MainActivity.java add the following code. 

Java




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",
            "Dijkstra’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);
    }
}


Output:



Last Updated : 08 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads