Home > Blockchain >  How to swap 2 button positions in Kotlin programatically by preference?
How to swap 2 button positions in Kotlin programatically by preference?

Time:02-11

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?

CodePudding user response:

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)
    }
}

CodePudding user response:

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>
  • Related