0

I was following an online tutorial to make a button change position (from top left to bottom right) while simultaneously increasing size. I am doing this using transitionmanager.

TransitionManager.beginDelayedTransition(mylayout);

Then I wondered if I could do the animation from bottom right to the top left also (this was not included in the tutorial, and I only attempted this out of pure curiosity). I created another method called

moveButtonBack();

The x variable gets incremented by one every time moveButton or moveButtonBack() is called, and moveButton gets called if x is even while moveButtonBack gets called if x is odd. By the way, I have added lots of comments in the code to keep track of things. To change the button size back in moveButtonBack, I used variables H and W for the first height and width of the button shown.

THE ISSUE

To me the code seems fine, and I can't identify the problem. When I run this on my phone, the button acts crazy. The text rapidly changes on the button, and it moves in an unorganized way, either it moves to the center when I touch, and to the top/bottom when I let go, or it stays in the center and looks as if it vibrating (rapidly moving up and down). It has a constant height of 300 and width of 450, instead of shrinking and enlarging. I don't know what the problem is, and would really appreciate it if I got help. I have spent over an hour inserting comments in the code and trying to solve the issue by changing things like listeners, heights, etc.

    package com.example.my.transitions;

import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.*;
import android.widget.*;
import android.transition.*;
import android.support.v7.app.*;

public class MainActivity extends AppCompatActivity {

ViewGroup myslayout;
View mysButton; //Used in move and moveback
int x = 0;
double h;
double w;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myslayout = (ViewGroup) findViewById(R.id.myslayoutid);

    myslayout.setOnTouchListener(
            new RelativeLayout.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {

                        if (x%2 == 0) {

                            moveButton();
                            return true;

                        }
                    else{

                            moveButtonBack();
                            return true;
                        }

                    return true; //touch listener as been handled
                }
            }
    );
}

public void moveButton() {
   Button mysButton = (Button) findViewById(R.id.mysbuttonid);

    h = mysButton.getHeight();
    w = mysButton.getWidth();


    TransitionManager.beginDelayedTransition(myslayout);

    //////////////////////Change position of button////////////////////////////


    RelativeLayout.LayoutParams positionRules = new RelativeLayout.LayoutParams(

            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

  positionRules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.ALIGN_PARENT_BOTTOM);

    positionRules.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.ALIGN_PARENT_RIGHT);


    mysButton.setLayoutParams(positionRules); 

    /////////////////////////Change size of button////////////////////////////


    ViewGroup.LayoutParams sizerules = mysButton.getLayoutParams();
    sizerules.width = 450;
    sizerules.height = 300;

    mysButton.setLayoutParams(sizerules);

    mysButton.setText("Height: " + sizerules.height + "Width: " + sizerules.width);
    x++; //Make x odd, to be called as moveButtonBack next time
}

    public void moveButtonBack(){
        Button mysButton =(Button) findViewById(R.id.mysbuttonid);
       TransitionManager.beginDelayedTransition(myslayout);

        RelativeLayout.LayoutParams positionBackRules = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        positionBackRules.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.ALIGN_PARENT_TOP);
        positionBackRules.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.ALIGN_PARENT_LEFT); //Move back to top left

        mysButton.setLayoutParams(positionBackRules);

        ViewGroup.LayoutParams sizebackrules = mysButton.getLayoutParams();
        sizebackrules.height = (int) h;
        sizebackrules.width = (int) w;
        //I am typcasting h and w into ints in case it is a double, since .width and .height only take ints.

    mysButton.setLayoutParams(sizebackrules);

        mysButton.setText("Height: " + sizebackrules.height + "Width: " + sizebackrules.width + "h " + h + "w" + w);

        x++; //Make x even again


    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

Again, I would really appreciate an answer, as I have been at this issue for hours now!

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • Are you using `x` for maintaining the current position? Also onTouchListener is called more than once - on touch down and touch up. Hence `x` alternates between odd and even. – ntsh Oct 17 '15 at 22:29
  • @ntsh Yes, I am using x to maintain the current position. Basically, every time the button is moved down and enlarged, x becomes odd, and everytime it moves up and is shrunk, x becomes even. Also, what should I do to make onTouchListener only get called once, because I suspect that might be the problem. I am sorry for the late responce, and I really appreciate your time. – Ruchir Baronia Oct 17 '15 at 22:58

1 Answers1

1

In your onTouch method check if it's a touch down or touch up. Write your move button logic only for one of these events.

For example:

if (event.getAction() == MotionEvent.ACTION_DOWN) {
    // Code for Move button / Move button back 
}

More details on Android Documentation

Regarding the button size not changing problem: Moving the calculation of h and w to Activity's onCreate method might help.

ntsh
  • 759
  • 5
  • 19
  • WOW! You are amazing. Thank you for solving the problem that I have been stuck on **all day** I have marked you as best answer and liked! I just have one question, would this code work better if I changed the pixels into dp? How would I do that? Thank you – Ruchir Baronia Oct 17 '15 at 23:32
  • @Rich You're welcome. [This](http://stackoverflow.com/a/9563438/1389997) might help. – ntsh Oct 17 '15 at 23:41