3

I have an OnClickListener that listens to Button A clicks. I also have 2 TextViews below the Button.

I want that on every click on Button A, the 2 TextViews will switch their places between them, like this:

Before clicking:

TextView A
TextView B

After clicking:

TextView B
TextView A

How can I do it? Is there a special function that meant to do that? or some kind of a trick? Thanks!

actvity_main.xml:

<?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="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="start"
    tools:context="com.intap.tof.MainActivity">

    <TextView
        android:id="@+id/txtA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="20dp"
        android:visibility="visible"/>

    <TextView
        android:id="@+id/txtB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtA
        android:layout_marginTop="83dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:visibility="visible"
        android:textSize="20dp" />
</RelativeLayout>

MainActivity.java:

txtA = (TextView) findViewById(R.id.txtA);
txtB = (TextView) findViewById(R.id.txtB);

float mAX = txtA.getX();
float mAY = txtA.getY();
float mBX = txtB.getX();
float mBY= txtB.getY();

txtA.setX(mBX);
txtA.setY(mBY);

txtB.setX(mAX);
txtB.setY(mAY);
Ido Naveh
  • 2,442
  • 3
  • 26
  • 57

3 Answers3

3

Trick is changing the x and y axis of both views : Your xml code:

<?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="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="start"
    tools:context="com.intap.tof.MainActivity">

    <TextView
        android:id="@+id/txtA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="20dp"
        android:text="A"
        android:tag="A"
        android:clickable="true"
        android:visibility="visible"/>

    <TextView
        android:id="@+id/txtB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtA"
        android:text="B"
        android:tag="B"
        android:clickable="true"
        android:layout_marginTop="83dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:visibility="visible"
    android:textSize="20dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="switch"
        android:id="@+id/btn1"
        android:onClick="onClickButton"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Java Code :

public class MainActivity extends Activity {

    TextView txtA,txtB;

    boolean _isOnTxtA;
    Button btn1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtA = (TextView)findViewById(R.id.txtA);
        txtB = (TextView)findViewById(R.id.txtB);

        btn1 = (Button)findViewById(R.id.btn1);




    }
    public void onClickButton(View v)
    {

        float mPos1x,mPos1y,mPos2x,mPos2y;
        mPos1x =     txtB.getX();
        mPos1y =     txtB.getY();
        mPos2x =     txtA.getX();
        mPos2y=     txtA.getY();

        if(_isOnTxtA)
        {

            txtB.setX(mPos2x);
            txtB.setY(mPos2y);
            txtA.setX(mPos1x);
            txtA.setY(mPos1y);
            _isOnTxtA = false;
        }
        else
        {


            txtB.setX(mPos2x);
            txtB.setY(mPos2y);
            txtA.setX(mPos1x);
            txtA.setY(mPos1y);
            _isOnTxtA= true;
        }

    }
}
2

View.bringToFront method may be useful.

Where your layout is like:

<LinearLayout>
    <TextView A>
    <TextView B>
</LinearLayout>

To call bringToFront on TextView A will bring it front (the last position in the parent LinearLayout).

<LinearLayout>
    <TextView B>
    <TextView A>
</LinearLayout>

For more detailed example, below is layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/text_holder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_a" />

    <TextView
        android:id="@+id/text_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_b" />

</LinearLayout>

In your OnClickListener (in your Activity) call:

View textViewA = findViewById(R.id.text_a);
textViewA.bringToFront();

This should work.

Toggling behavior can be achieved by this application. For example:

ViewGroup textHolder = (ViewGroup) findViewById(R.id.text_holder);
textHolder.getChildAt(0).bringToFront();

ViewGroup.getChildAt(0) always returns the first child of the ViewGroup. So everytime you call bringToFront on the first child will be bring to front.

hata
  • 11,633
  • 6
  • 46
  • 69
  • I'm not sure I've understood your answer. Do you mean to create another layout? – Ido Naveh Sep 10 '15 at 20:09
  • No, you don't need. The latter layout is just an example and it is dynamic result. – hata Sep 10 '15 at 20:20
  • Now I understand it, but this isn't answering to my question. The TextViews aren't one on each other, Textview a is above textview b and i want to switch them on button click. Do you know how to do that? – Ido Naveh Sep 10 '15 at 20:23
  • @IdoNaveh I confirmed my sample works. There should be some problem in your code. If you don't post your related code and layout.xml, I can't have any idea... – hata Sep 11 '15 at 05:23
  • I'm not on my computer right now do I'll post the code in about half an hour – Ido Naveh Sep 11 '15 at 05:52
  • I have added the answer review it – Azfaar kabir Siddiqui Sep 11 '15 at 07:22
  • @IdoNaveh My solution is about using `LinearLayout` for parent `ViewGroup`. If you use `RelativeLayout`, other approach like Azfaar kabir Siddiqui's answer would be suitable. – hata Sep 11 '15 at 14:23
  • @hata Now I've understand how to use your solution. I'll try and Let you know if it works – Ido Naveh Sep 11 '15 at 15:42
1

All previous answers do not work because you use a relative layout where a linear layout will suffice. If you have to go RelativeLayout, do the following in your click listener

    TextView textA = (TextView) findViewById(R.id.txtA);
    TextView textB = (TextView) findViewById(R.id.txtB);

    RelativeLayout.LayoutParams textALayoutParams = (RelativeLayout.LayoutParams) textA.getLayoutParams();
    textALayoutParams.addRule(RelativeLayout.BELOW, R.id.txtB);
    textA.setLayoutParams(textALayoutParams);

    RelativeLayout.LayoutParams textBLayoutParams = (RelativeLayout.LayoutParams) textB.getLayoutParams();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        textBLayoutParams.removeRule(RelativeLayout.BELOW);
    } else {
        textBLayoutParams.addRule(RelativeLayout.BELOW,0);
    }
    textB.setLayoutParams(textBLayoutParams);

This will place A under B. You can do the same for reversing.

ibit
  • 316
  • 1
  • 6