i am moving my button by animation and button moving visually but when i press the button at new position i don't find it there.
final Animation animation1 = new TranslateAnimation(0,0,0,100);
animation1.setDuration(5000);
animation1.setFillAfter(true);
alphabet.startAnimation(animation1);
humanbody.startAnimation(animation1);
poem.startAnimation(animation1);
game.startAnimation(animation1);
When I press alphabet button,it took me to human-body activity And I press somewhere top of alphabet button it took me to alphabet activity
I'm still a beginner at this, but I think that using the animation class only animates the view's drawable state, not the its bound/touch area.
Try using this:
int t = 5000;
int y = 100;
alphabet.animate().setDuration(t).translateY(y).start();
humanbody.animate().setDuration(t).translateY(y).start();
poem.animate().setDuration(t).translateY(y).start();
game.animate().setDuration(t).translateY(y).start();
Related
I want a Button (or any View for that matter) to expand / stretch out sideways with an animation and I want it to happen at the click of another Button. For example, before Button 1 is pressed, the Button 2 shouldn't exist. But when Button 1 is pressed, the Button 2 should slowly come into existence with both its sides smoothly expanding to left and right to a certain size.
The following is a photo I drew to explain what I'm trying to achieve
I have looked at several tutorials, but most of them had sliding animation from one fixed end to the other. I have also tried scaling the X & Y of the button from value 0 to 1, but they just don't give the smooth animation effect (they just appear there out of nowhere) and the button doesn't expand / stretch sideways.
Any idea how this can be achieved?
Thank you for your time!!
the smooth animation effect ,you can use ObjectAnimtor.
// scale down button2 at the start of an activity
btn2.setScaleX(0.1f);
// on the click of button 1 let the button2 scale
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btn2.animate()
.scaleX(4.0f)
.setDuration(1000)
.setInterpolator(new OvershootInterpolator(2.0f)) //Will give bounce effect to the scaling
.start();
}
});
I think you can use this library for Button 2 animation.
In order to it happen at the click of another Button just call View.performClick() for Button 2 from Button 1 onClick() method.
first define 2 objects of Animation and Button then create a On Click listener for Button and write these codes in it:
//here i define :Button btnoff
//Animation animation1
//set on click listener in oncreate
btnOff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
animation1 = new ScaleAnimation(0,2,1,1,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation1.setDuration(1200);
btnOff.startAnimation(animation1);}
});
you can learn more in this link.
I've been trying to get the position from a button inside a dialog so that i can use it to place a button below that button in the activity.
I've tried a few things but none of them seem to work
int loc1[] = new int[2];
button.getLocationInWindow(loc1);
int loc2[] = new int[2];
button.getLocationOnScreen(loc2);
I've also tried
button.getX();
button.getY();
The button was never placed below the button in the dialog.
(I should note that i only mentioned the methods to get the postiion)
Can anybody help me ?
Thanks !
You need to wait for the callback when the Button has placed. The code you are using not returning the proper value because the value is returned before the Button is placed. So add the Button in a layout with height/width as wrap_content and Use this code:
layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
getViewTreeObserver().removeGlobalOnLayoutListener(this);
int[] locations = new int[2];
layout.getLocationOnScreen(locations);
int x = locations[0];
int y = locations[1];
}
});
Make layout params, they depend on the parent, so you should probably use LinearLayout.LayoutParams or RelativeLayout.LayoutParams (there are other cases).
And add layout_below(button1) to your LayoutParams object. And set that LayoutParams to new button you want to appear below first button1.
Java Android question
I have x, say 5, buttons in a row.
Each button has a different number value displayed on the button.
Button one is active, the rest are not- not clickable. They are greyed out.
To show Button 1 is active it fades up and down.
Once clicked the button pops up a message. The user Ok's that, this activates Button 2, and deactivates Button 1.
Then it happens through all buttons, one by one. The final button doesn't produce the pop up message.
My question...
I want to create a method that sets the first button as current and then once clicked sets the next as current, and so on.
Can anyone tell me how to do this? I don't need to know how to fade buttons etc, its literally how to set button as current, and within that method the user click sets the next button as current.
Many thanks in advance.
EDIT
OK, I've had a go...its not working, but it seems so close...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workout_one);
int[] buttonIds = new int[] {R.id.button_1,R.id.button_2,R.id.button_3,R.id.button_4,R.id.button_5};
setButton(buttonIds);
}
private void setButton(int[] buttId){
int isCurrent = 0;
while(isCurrent < 5) {
Button currentButton = (Button) findViewById(buttId[isCurrent]);
//TODO Make current button pulse
currentButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.clearAnimation();
v.setBackgroundColor(0xFF00FF00);
v.setFocusable(false);
v.setFocusableInTouchMode(false);
v.setClickable(false);
setTimer();
isCurrent++;
}
});
I know that the problem is the isCurrent++ is not accessible outside the onClick method. How do I right this? Am I close or is this a major funk up and do I have to rethink?
Just use a global variable which track the current button, and check this variable to identify the current active button for determining the action in onClickListener. To fade out a button try this code snippet
button.setClickable(false);
button.setBackgroundColor(Color.parseColor("#808080"));
You need something like this:
private int activeButton = 1;
private void buttonClickHandler(){
switch(activeButton++){
case : 1
button1.setEnabled(true):
// show popup, hide/animate for button 1
break;
case : 2
button2.setEnabled(true);
// same for button 2
case : 3
// same for button 3
case : 4
// same for button 4
}
I am creating some buttons in a tablelayout. I am creating the buttons programmatically.
For example the following code is in the "onresume" method...
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(0,android.widget.TableRow.LayoutParams.MATCH_PARENT,3f);
TableLayout tableLayout = (TableLayout) findViewById(R.id.sometablelayoutivealreadydefinedinxml);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(tableParams);
Button btnOne = new Button(this);
btnOne.setId(100);
btnOne.setLayoutParams(rowParams);
btnTwo.setId(200);
btnTwo.setLayoutParams(rowParams);
tableRow.addView(btnOne);
tableRow.addView(btnTwo);
tableLayout.addView(tableRow);
I want to set the height of the button to be the SAME as the width. The width is determined by the size of the screen. The problem as I understand it,is that until the buttons are actually drawn to the screen they don't exist so any width returned will be zero. So I tried the following code.
Button yourBtn = (Button)findViewById(100);
int btnSize;
btnSize = yourBtn.getWidth();
yourBtn.setLayoutParams(new TableRow.LayoutParams(btnSize, btnSize));
Now the problem is that if I run this code immediately after creating the buttons the width of the button is returned as 0 as the button hasn't been drawn yet. However, if I assign the code to a test button then it resize the button as I want it to. However, I don't want the user to have to press a button after the screen has loaded to resize the buttons properly.
So, the question is when is the appropriate time to call this to resize the buttons properly when the activity is created but after the buttons have been drawn and how do I do this?
How can I as easy as possible set the position of my Button to a random place?
I've tryed to use this: (My screen resolution is 800x480)
Button btn = (Button) findViewById(R.id.button1);
Random r = new Random();
int x = r.nextInt(480);
int y = r.nextInt(800);
btn.setX(x);
btn.setY(y);
But when i use this, it seems that the button some times get located outside my application or so? is it possible to prevent this and keep the button inside the app?
Pretty sure your problem is you are not accounting for the width and height of the button, and so if it randoms to 480*800 it will be off the screen. Try something similar to:
int x = r.nextInt(480 - buttonWidth);
int y = r.nextInt(800 - buttonHeight);