Skip to content

Commit

Permalink
Fixed game level spinner width bug
Browse files Browse the repository at this point in the history
The bug was happening because of how the Spinner's width measurement is implemented. It only measures 15 items at a time and sets the width of that section based on the widest item from within it. To overcome this, I had to add spaces to the digits in order to make their length as a string be equal to that of the word "Select".
  • Loading branch information
SpiritualForest committed Jun 5, 2022
1 parent 0848742 commit 1a11d51
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions app/src/main/java/com/androidtetris/activity/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,20 @@ class MainActivity : AppCompatActivity(), OnItemSelectedListener {

// Game level
val gameLevels: MutableList<String> = mutableListOf(defaultSpinnerSelection)
for(i in 1 until 15) { gameLevels.add(i.toString()) }
val defaultSelectionLength = defaultSpinnerSelection.length
for(i in 1 until 20) {
// Due to the Spinner's 15 item width measurement cap, we must
// add extra empty spaces to the number in order to increase its width.
val iString = i.toString()
val digitLengthIncrement = ((defaultSelectionLength - iString.length) * 2) - 1
val finalDigit = iString + " ".repeat(digitLengthIncrement)
gameLevels.add(finalDigit)
}
// Set the adapter
setAdapter(gameLevelSpinner, gameLevels)
val gameLevelSetValue: Int = SettingsHandler.getGameLevel() // Returns 1 by default
gameLevelSpinner.setSelection(getSpinnerIndex(gameLevelSpinner, gameLevelSetValue.toString()))
val widthIncrement = ((defaultSelectionLength - gameLevelSetValue.toString().length) * 2) - 1
gameLevelSpinner.setSelection(getSpinnerIndex(gameLevelSpinner, gameLevelSetValue.toString() + " ".repeat(widthIncrement)))

// Starting height
val startingHeights: MutableList<String> = mutableListOf(defaultSpinnerSelection)
Expand Down Expand Up @@ -102,7 +111,9 @@ class MainActivity : AppCompatActivity(), OnItemSelectedListener {
}
R.id.spinner_gameLevel -> {
// Game level selection
SettingsHandler.setGameLevel(parent.getItemAtPosition(position).toString().toInt())
// This spinner gets special handling because its values are appended with spaces.
// We must strip those spaces with .trim()
SettingsHandler.setGameLevel(parent.getItemAtPosition(position).toString().trim().toInt())
}
R.id.spinner_startingHeight -> {
// Starting height
Expand Down

0 comments on commit 1a11d51

Please sign in to comment.