0

I am building a game which has X number of levels.

  • The "Levels" view will display a button for each level, with that button's value equaling to the "value" of the level (iteration), beginning at (0), and each level [value] incrementing by (1).
  • The number of buttons [actual] will correlate to the number of levels in the game.
  • The number of levels can be changed at any time (per the developer)

Please refer to the attached image. Which dynamic view/layout should I use?

  • Gridview
  • gridlayout
  • tableview

...etc

If I change the number of levels [X], the number of buttons (and their values) should be dynamically created automatically.

This is how i want to do

Rob Scott
  • 7,921
  • 5
  • 38
  • 63
Krish gogar
  • 47
  • 2
  • 6
  • @Rob Scott thank you very much. this helped me alot. but i have a little doubt. How can i change background color of already played level. – Krish gogar May 14 '20 at 12:47

1 Answers1

0

I would use RecyclerView with GridLayoutManager. It have several advantages over GridView, etc.

If you don't know how to use RecyclerView, there's plenty of great tutorials out there. After you set up your RecyclerView, you will have:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Then in your code add this one line

recycler_view.apply {
    //...
    layoutManager = GridLayoutManager(context, 5)
}
  • i used the @rob scott suggested method and i got my desire look but one thing i want to know more. For example a player already crossed 10 level and currently playing 11th level than how can i change background color of played level so that it differentiate with level still not played. – Krish gogar May 14 '20 at 13:16
  • @Krishgogar in the data model, you should have a boolean like `isCompleted`. Then you just need to check in the adapter, and change bg color accordingly. – Michael Udjiawan May 14 '20 at 13:47