jwatts1980
jwatts1980

Reputation: 7346

Android: Swap the position of 2 views

On my TableLayout, one TableRow has a TextView, and another has a Spinner. When the user checks a checkbox, I want to swap the positions of these 2 views. The Spinner needs to move to the position of the TextView, and the TextView needs to move to the position of Spinner.

Animation doesn't matter. The preference would be that it happens instantly, but I could make it a super-fast animation if necessary.

I have been surprised at how much trouble I've had trying to locate a solution to this problem :/ On a similar note, the Android dev ref could really use some simple examples a la the MSDN library.

Thanks!

Upvotes: 2

Views: 5750

Answers (4)

Hussnain Mehdi
Hussnain Mehdi

Reputation: 119

Swap two views with animation in Kotlin.

I you want to Swap to views in Kotlin you can you use below method. When you click the button it will swap the position of you views and if you again click the it will set the views to original position.

 private fun swapViewPositions(viewFrom: View, viewTo: View) {
    val translationX1: Float
    val translationY1: Float
    val translationX2: Float
    val translationY2: Float

    if (isSwapped) {
        translationX1 = 0f
        translationY1 = 0f
        translationX2 = 0f
        translationY2 = 0f
    } else {
        translationX1 = viewTo.x - viewFrom.x
        translationY1 = viewTo.y - viewFrom.y
        translationX2 = viewFrom.x - viewTo.x
        translationY2 = viewFrom.y - viewTo.y
    }
    val translationAnimator1 = ObjectAnimator.ofFloat(viewFrom, "translationX", translationX1)
    val translationAnimator2 = ObjectAnimator.ofFloat(viewFrom, "translationY", translationY1)
    val translationAnimator3 = ObjectAnimator.ofFloat(viewTo, "translationX", translationX2)
    val translationAnimator4 = ObjectAnimator.ofFloat(viewTo, "translationY", translationY2)
    val animatorSet = AnimatorSet()
    animatorSet.playTogether(
        translationAnimator1,
        translationAnimator2,
        translationAnimator3,
        translationAnimator4
    )
    animatorSet.duration = 500 //you can change duration
    animatorSet.start()
}

Here is how to call it

 binding.apply {
        cvConversionCurrency.setOnClickListener {
            swapViewPositions(clFrom, clTo)
            isSwapped = !isSwapped
        }
    }

Upvotes: 0

Peter
Peter

Reputation: 13485

Here is a generic Kotlin function to swap the positions of 2 views - I have not tested whether it works for your case of Spinner and TextView, but I don't currently see why not

fun swapViews(view1: View, view2: View){
    val view1Parent = view1.parent as ViewGroup
    val view1Params = view1.layoutParams
    val view2Parent = view2.parent as ViewGroup
    val view2Params = view2.layoutParams
    view1Parent.removeView(view1)
    view2Parent.removeView(view2)
    view2Parent.addView(view1)
    view1Parent.addView(view2)
    view1.layoutParams = view2Params
    view2.layoutParams = view1Params
}

Upvotes: 0

jwatts1980
jwatts1980

Reputation: 7346

Here is the code I ended up using to swap the views:

import android.widget.TableRow;

TableRow trI = (TableRow) findViewById(R.id.tr_input_row);
TableRow trO = (TableRow) findViewById(R.id.tr_output_row);
View v1 = (View) findViewById(R.id.View1);
View v2 = (View) findViewById(R.id.View2);

if (isInput()) {
    trO.removeView(v1);
    trI.removeView(v2); 
    trO.addView(v2);
    trI.addView(v1);
} else {
    trO.removeView(v2);
    trI.removeView(v1); 
    trO.addView(v1);
    trI.addView(v2);
}

In my case, the views were in two different rows of a TableLayout. I'm using TableRow because it is the type of the parent of the views I want to swap. I assume this could be updated to be whatever type of parent the target views have.

Upvotes: 3

Jordi Coscolla
Jordi Coscolla

Reputation: 1066

You can try to remove the first TextView and then add back to the end of the linear layout using addView.

TextView tv = (TextView)layout.getChildAt(0);
layout.removeView(tv);
layout.addView(tv);

It's not a really clean code, but I thing might work :)

Upvotes: 4

Related Questions