Skip to content
Related Articles

Related Articles

Improve Article
How to add Bullet list in a RecyclerView in Android?
  • Difficulty Level : Medium
  • Last Updated : 16 Apr, 2020

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.




    <dependency>
        implementation 'androidx.recyclerview:recyclerview:1.1.0'
    </dependency>
  2. Create a bullet.xml file in the drawable folder.




    <?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>
  3. In activity_main.xml, add the following 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>
  4. Create a new custom_layout.xml file with the following 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>
  5. Create a MyAdapter.java class and add the following 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);
            }
        }
    }
  6. Finally, in MainActivity.java add the following 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);
        }
    }

Output:

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!



My Personal Notes arrow_drop_up
Recommended Articles
Page :