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:-
- Add the support Library in build.gradle file for Recycler View in the dependencies section.
<dependency>implementation 'androidx.recyclerview:recyclerview:1.1.0'</dependency> - Create a bullet.xml file in the drawable folder.
<?xmlversion="1.0"encoding="utf-8"?><shapeandroid:shape="oval"><paddingandroid:left="8dp"android:right="8dp"/><sizeandroid:width="6dp"android:height="6dp"/><solidandroid:color="#219806"/></shape> - In activity_main.xml, add the following code.
<?xmlversion="1.0"encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><androidx.recyclerview.widget.RecyclerViewandroid: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.
<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutandroid:layout_width="match_parent"android:id="@+id/show_name_layout"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_margin="5dp"><TextViewandroid:drawableLeft="@drawable/bullet"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid: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.
publicclassMyAdapterextendsRecyclerView.Adapter<MyAdapter.MyViewHolder> {String text[];Activity activity;publicMyAdapter(Activity activity,String text[]){this.activity = activity;this.text = text;}// This method is used to attach// custom layout to the recycler view@NonNull@OverridepublicMyViewHolder onCreateViewHolder(@NonNullViewGroup parent,intviewType){View view= activity.getLayoutInflater().inflate(R.layout.custom_layout,parent,false);returnnewMyViewHolder(view);}// This method is used to set the action// to the widgets of our custom layout.@OverridepublicvoidonBindViewHolder(@NonNullMyViewHolder holder,intposition){holder.textView.setText(text[position]);}@OverridepublicintgetItemCount(){returntext.length;}classMyViewHolderextendsRecyclerView.ViewHolder {TextView textView;publicMyViewHolder(@NonNullView itemView){super(itemView);textView = itemView.findViewById(R.id.textView);}}} - Finally, in MainActivity.java add the following code.
publicclassMainActivityextendsAppCompatActivity {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"};@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);recyclerView = findViewById(R.id.recycler_view);MyAdapter adapter =newMyAdapter(this, text);recyclerView.setLayoutManager(newLinearLayoutManager(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!




