'How to swap 2 button positions in Kotlin programatically by preference?
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 1:[1]
Try use RelativeLayout(or ConstraintLayout):
override fun onResume() {
super.onResume()
val cleanBtn = findViewById<Button>(R.id.cleanBtn);
val analyzeBtn = findViewById<Button>(R.id.analyzeBtn);
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val shouldFlipButtonPosition = sharedPreferences.getBoolean("buttonpositions", false)
var isButtonPositionFlipped = true;
if (isButtonPositionFlipped != shouldFlipButtonPosition) {
isButtonPositionFlipped = shouldFlipButtonPosition;
(cleanBtn.layoutParams as RelativeLayout.LayoutParams).leftMargin =
dipToPixels(this, 100f);//100dp?
(analyzeBtn.layoutParams as RelativeLayout.LayoutParams).leftMargin = 0;
} else {
(cleanBtn.layoutParams as RelativeLayout.LayoutParams).leftMargin = 0;
(analyzeBtn.layoutParams as RelativeLayout.LayoutParams).leftMargin =
dipToPixels(this, 100f);//100dp?
}
}
private fun dipToPixels(context: Context, dipValue: Float): Int {
val metrics: DisplayMetrics = context.resources.displayMetrics
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics).toInt()
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".TestActivity">
<Button
android:id="@+id/cleanBtn"
android:layout_width="wrap_content"
android:text="111"
android:layout_height="wrap_content"></Button>
<Button
android:id="@+id/analyzeBtn"
android:layout_marginLeft="100dp"
android:layout_width="wrap_content"
android:text="222"
android:layout_height="wrap_content"></Button>
</RelativeLayout>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | simon5678 |
