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!