Issue
So, I have 2 buttons and I wanna swap them by using a preference but Yeah, I can't make them switch properly
Here is my code for preference
// In MainActivity.kt
override fun onResume() {
super.onResume()
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val shouldFlipButtonPosition = sharedPreferences.getBoolean("buttonpositions", false)
if (isButtonPositionFlipped != shouldFlipButtonPosition) {
isButtonPositionFlipped = shouldFlipButtonPosition
val cleanBtn = findViewById<Button>(R.id.cleanBtn)
val analyzeBtn = findViewById<Button>(R.id.analyzeBtn)
if (isButtonPositionFlipped) {
// Buttons swaped.
} else {
// Buttons back to normal.
}
}
I tried again to make them swap but I got some ugly results like this:
override fun onResume() {
super.onResume()
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val shouldFlipButtonPosition = sharedPreferences.getBoolean("buttonpositions", false)
if (isButtonPositionFlipped != shouldFlipButtonPosition) {
isButtonPositionFlipped = shouldFlipButtonPosition
val cleanBtn = findViewById<Button>(R.id.cleanBtn)
val analyzeBtn = findViewById<Button>(R.id.analyzeBtn)
if (isButtonPositionFlipped) {
val posX = cleanBtn.x
val posY = cleanBtn.y
analyzeBtn.x = posX
analyzeBtn.y = posY
} else {
cleanBtn.setPadding(0, 0, 0, 0)
analyzeBtn.setPadding(0, 0, 0, 0)
}
}
}
override fun onResume() {
super.onResume()
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val shouldFlipButtonPosition = sharedPreferences.getBoolean("buttonpositions", false)
if (isButtonPositionFlipped != shouldFlipButtonPosition) {
isButtonPositionFlipped = shouldFlipButtonPosition
val cleanBtn = findViewById<Button>(R.id.cleanBtn)
val analyzeBtn = findViewById<Button>(R.id.analyzeBtn)
if (isButtonPositionFlipped) {
cleanBtn.setPadding(640, 0, 0, 0)
analyzeBtn.setPadding(0, 0, 640, 0)
} else {
cleanBtn.setPadding(0, 0, 0, 0)
analyzeBtn.setPadding(0, 0, 0, 0)
}
}
}
I have to mention I tried to use set.Margins
but isn't working
How can I swap them by using this method?
Solution
If they're in a LinearLayout and they're the only two children views of that LinearLayout, you can reverse the order of the children like this:
findViewById<LinearLayout>(R.id.theParentLinearLayout).apply {
val children = children.toList()
removeAllViews()
for (child in children.asReversed()) {
addView(child)
}
}
If they need different sized margins, you can swap their layout params too:
findViewById<LinearLayout>(R.id.theParentLinearLayout).apply {
val children = children.toList()
children[0].layoutParams = children[1].layoutParams
.also { children[1].layoutParams = children[0].layoutParams }
removeAllViews()
for (child in children.asReversed()) {
addView(child)
}
}
If there are more than two children views, you can search for and swap them in a mutable list before adding them all back to the parent.
findViewById<LinearLayout>(R.id.theParentLinearLayout).apply {
val children = children.toMutableList()
val buttonAIndex = children.indexOf(findViewById(R.id.buttonA))
val buttonBIndex = children.indexOf(findViewById(R.id.buttonB))
children[buttonAIndex] = children[buttonBIndex]
.also { children[buttonBIndex] = children[buttonAIndex] }
children[buttonAIndex].layoutParams = children[buttonBIndex].layoutParams
.also { children[buttonBIndex].layoutParams = children[buttonAIndex].layoutParams }
removeAllViews()
for (child in children) {
addView(child)
}
}
Answered By - Tenfour04
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.