Android Create Object Dynamically - android

I want to create one button/layout object dynamically from the object that exist on the current layout and want to handle both differently..
Here is my code that I implemented..
declared globally
Button btnStart, btnButton1, btnButton2, btnButton3;
and in OnCreate()
btnStart = (Button) findViewById(R.id.btnStart);
btnButton1 = (Button) findViewById(R.id.Button1);
btnButton2 = (Button) findViewById(R.id.Button2);
btnButton3 = btnButton1; // Problem comes here.
I want to create btnButton3 dynamically same as btnButton1. On clicking the
btnStart I am running animations for btnButton1, btnButton2 and btnButton3.
Now the issue is btnButton2 running animation fine.. but btnButton1 is not animating
and instead btnButton3 is animating.
Here's my complete code..
public class TweenAnimationActivity extends Activity {
/** Called when the activity is first created. */
Button btnStart, btnButton1, btnButton2, btnButton3;
FrameLayout mainLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainLayout = (FrameLayout) findViewById(R.id.flMain);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Log.e("Dimension", "Width : " + width + " Height : " + height);
btnStart = (Button) findViewById(R.id.btnStart);
btnButton1 = (Button) findViewById(R.id.Button1);
btnButton2 = (Button) findViewById(R.id.Button2);
btnButton3 = btnButton1;
// btnButton3.setLayoutParams(relativeParams);
final Animation animation = new TranslateAnimation(0, 0, 0,
height - 220);
animation.setDuration(5000);
animation.setFillAfter(true);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
btnButton1.setText("Moving");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
btnButton1.setText("Moved");
}
});
final Animation animation1 = new TranslateAnimation(0, 0, 0,
-(height - 220));
// animation1.setStartOffset(1000);
animation1.setDuration(5000);
animation1.setFillAfter(true);
animation1.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
btnButton2.setText("Moving");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
btnButton2.setText("Moved");
}
});
// final Animation animation2 = new TranslateAnimation(0, 0, -(width - 220),
// height - 220);
final Animation animation2 = new TranslateAnimation(0, 0, (width - 220),
height - 220);
animation2.setDuration(5000);
animation2.setFillAfter(true);
animation2.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
btnButton3.setText("MovingCopied");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
btnButton3.setText("MovedCopied");
}
});
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
btnButton1.startAnimation(animation);
btnButton2.startAnimation(animation1);
btnButton3.startAnimation(animation2);
}
});
}
}
any here's my layout..
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/flMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="#+id/Button1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="top|right"
android:text="Button1" />
<Button
android:id="#+id/Button2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="bottom|right"
android:text="Button2" />
</FrameLayout>

In your code, btnButton3 = btnButton1, you have not created button3 object just created a reference of button1.
try
btnButton3 = new Button(this)
btnButton3.setLayoutParams(btnButton1.getLayoutParams());
mainLayout.addView(btnButton3);
and for better tutorial you can look here.

Related

Can we use Android TranslateAnimation for 2 different location?

TranslateAnimation is use when you want to move image in different possintion like
left to right
up to down
one XYscalse position to other XYscale position
Syntax
TranslateAnimation animation = new TranslateAnimation(StartinXscale,StartingYscale,EndXscale,EndYscale);
Methods
animation.setDuration(millisecond);//move speed.
animation.setRepeatCount(int value);//how many time you want to move it from starting to ending position.
animation.setRepeatMode(int value);//mode like goto destination and return back to main position.
imageView.startAnimation(animation);//Start animation on imageView
Can we use above code twice time in one activity?
MyCode
private void animationAction() {
float StartX = 500.0f;
float StartY = -300.0f;
float EndX = -300.0f;
float EndY = 500.0f;
int i = 0;
for (i = 0; i <2; i++) {
TranslateAnimation animation = new TranslateAnimation(StartX,StartX + EndX, StartY, StartY + EndY);
animation.setDuration(3000);
animation.setRepeatCount(5);
animation.setRepeatMode(2);
animation.setFillAfter(true);
img_animation.startAnimation(animation);
EndX = 300.0f;
}
In MyCode result was only execute one time
// Animation
private Animation animMove;
private Animation animFadein;
//Method to preform 2 animation in sequence
private void doAnimation() {
// load the animation
animMove = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move);
animFadein = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in);
// start move up animation on brnetwork text
imageView.startAnimation(animMove);
// set animation listener
animMove.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.background);
relMain.setBackgroundDrawable(drawable);
llInput.startAnimation(animFadein);
llInput.setVisibility(View.VISIBLE);
tvSignUp.startAnimation(animFadein);
tvVideo.startAnimation(animFadein);
tvSignUp.setVisibility(View.VISIBLE);
tvVideo.setVisibility(View.VISIBLE);
}
});
// set animation listener
animFadein.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
});
}
after MOVE animation i have to perform FEDIN animation
in your case you can replace FEDIN by your Next Animation.

RelativeLayout child top is coming as zero

am facing a strange problem, I have generated my layout file using RelativeLayout. however while reading top of one of the child's present in it is being returned as zero,which is causing my whole animation logic go for toss.
My idea is to animate them from bottom of screen, to there initial position. below is my code, any help from you all will be highly appreciated.
My layout file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:paddingLeft="35dp"
android:paddingRight="35dp" >
<LinearLayout
android:id="#+id/msgs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="35dp"
android:layout_marginTop="25dp"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:gravity="center_horizontal"
android:text="#string/share"
android:textColor="#android:color/black"
android:textSize="26dp"
android:typeface="monospace" />
<TextView
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_gravity="top"
android:maxLines="3"
android:text="#string/share_message"
android:textColor="#android:color/black"
android:textSize="14dp"
android:typeface="sans" />
</LinearLayout>
<ImageView
android:id="#+id/view_twitter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_above="#id/msgs"
android:layout_marginEnd="25dp"
android:scaleType="fitCenter"
android:src="#drawable/circle_twitter" />
<ImageView
android:id="#+id/view_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/msgs"
android:layout_marginEnd="25dp"
android:scaleType="fitCenter"
android:layout_toStartOf="#id/view_twitter"
android:src="#drawable/facebook_circle" />
<ImageView
android:id="#+id/view_google"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/msgs"
android:layout_marginEnd="25dp"
android:layout_toEndOf="#id/view_twitter"
android:scaleType="fitCenter"
android:src="#drawable/google_circle" />
</RelativeLayout>
My Fragment code, where am animating them
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
public class IntroFragmentThird extends Fragment{
View google;
View twitter;
View facebook;
View messagePanel;
int screenHeight;
int screenWidth;
int offset;
float beginPoint;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
offset = screenHeight + 200; // start from 200px below screen height
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.intro3_layout, container, false);
google = view.findViewById(R.id.view_google);
facebook = view.findViewById(R.id.view_facebook);
twitter = view.findViewById(R.id.view_twitter);
messagePanel = view.findViewById(R.id.msgs);
google.setVisibility(View.GONE); // Initial view visibility will be gone
facebook.setVisibility(View.GONE);
twitter.setVisibility(View.GONE);
beginPoint = twitter.getTop(); // this is coming as ZERO
return view;
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("#### Top : " + beginPoint);
playScreenAnimation();
}
public void playScreenAnimation(){
facebook.clearAnimation();
facebook.setVisibility(View.GONE);
facebook.clearAnimation();
facebook.setVisibility(View.GONE);
facebook.clearAnimation();
facebook.setVisibility(View.GONE);
ObjectAnimator anim1 = ObjectAnimator.ofFloat(facebook, "y", offset , beginPoint);
anim1.setInterpolator(new AccelerateDecelerateInterpolator());
anim1.setDuration(1000);
anim1.setStartDelay(1000);
anim1.addListener(new AnimatorListener(){
#Override
public void onAnimationStart(Animator animation) {
facebook.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
});
ObjectAnimator anim2 = ObjectAnimator.ofFloat(twitter, "y", offset , beginPoint);
anim2.setInterpolator(new AccelerateDecelerateInterpolator());
anim2.setDuration(1000);
anim2.addListener(new AnimatorListener(){
#Override
public void onAnimationStart(Animator animation) {
twitter.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
});
ObjectAnimator anim3 = ObjectAnimator.ofFloat(google, "y", offset , beginPoint);
anim3.setInterpolator(new AccelerateDecelerateInterpolator());
anim3.setDuration(1000);
anim3.setStartDelay(1000);
anim3.addListener(new AnimatorListener(){
#Override
public void onAnimationStart(Animator animation) {
google.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
});
AnimatorSet set = new AnimatorSet();
set.playTogether(anim2,anim1,anim3);
set.start();
}
}
Here is screenshot of, initial state of views (those three fb, g+, twitter circles are being animated)
Alright I solved it, instead of using object animator, i ended up using view animation, and animating views considering there relative positions.
facebook.clearAnimation();
facebook.setVisibility(View.INVISIBLE);
google.clearAnimation();
google.setVisibility(View.INVISIBLE);
twitter.clearAnimation();
twitter.setVisibility(View.INVISIBLE);
TranslateAnimation anim1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0
,Animation.RELATIVE_TO_SELF,offset,Animation.RELATIVE_TO_SELF,0);
anim1.setDuration(1000);
anim1.setInterpolator(new AccelerateDecelerateInterpolator());
anim1.setAnimationListener(new AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {
facebook.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
});
TranslateAnimation anim2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0
,Animation.RELATIVE_TO_SELF,offset,Animation.RELATIVE_TO_SELF,0);
anim2.setDuration(750);
//anim2.setStartOffset(500);
anim2.setInterpolator(new AccelerateDecelerateInterpolator());
anim2.setAnimationListener(new AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {
twitter.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
});
TranslateAnimation anim3 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0
,Animation.RELATIVE_TO_SELF,offset,Animation.RELATIVE_TO_SELF,0);
anim3.setDuration(750);
anim3.setStartOffset(500);
anim3.setInterpolator(new AccelerateDecelerateInterpolator());
anim3.setAnimationListener(new AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {
google.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
});
twitter.startAnimation(anim2);
facebook.startAnimation(anim1);
google.startAnimation(anim3);

Android animation when scroll

I want do a animate scroll like Jquery´s (in this web page works when you click in top menu´s options) in android. is it possible? I have seen that exists scrollTo but is possible to do with animation and not like jumps. Thank you very much.
Using ObjectAnimator, This is a sample for scrolling to top :
public void scroolToTop() {
int x = 0;
int y = 0;
ObjectAnimator xTranslate = ObjectAnimator.ofInt(mScrollView, "scrollX", x);
ObjectAnimator yTranslate = ObjectAnimator.ofInt(mScrollView, "scrollY", y);
AnimatorSet animators = new AnimatorSet();
animators.setDuration(1000L);
animators.playTogether(xTranslate, yTranslate);
animators.addListener(new AnimatorListener() {
#Override
public void onAnimationStart(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationCancel(Animator arg0) {
// TODO Auto-generated method stub
}
});
animators.start();
}

Drag One Button To Other Button Position

i want to touch button and drag into other button position you can get clear idea by this image.
Guiding Image
Please help me
I Also Found This very helpful.
Touch and drag image in android
but i want to drag to other button position not some were else on screen.i don't have any idea how i would detect button is entered in other button dimensions.
So far i am doing is just animation but i don't want just straight forward moving button by click.
i want user to move by himself button to other button position.
Code
public class AnimationActivity extends Activity implements OnClickListener {
public Button btn_a1, btn_a2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainsec);
btn_a1 = (Button) findViewById(R.id.btn_a1);
btn_a2 = (Button) findViewById(R.id.btn_a2);
btn_a1.setOnClickListener(this);
btn_a2.setOnClickListener(this);
}
#Override
public void onClick(final View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_a1: {
int direction = -1;
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
RelativeLayout.LayoutParams absParams = (RelativeLayout.LayoutParams) btn_a1
.getLayoutParams();
final float xDelta = (displaymetrics.widthPixels / 2)
- absParams.leftMargin - (btn_a1.getWidth() / 2);
final Animation animation = new TranslateAnimation(0, xDelta
* direction, 0, 0);
AnimationListener animationOutListener = new AnimationListener() {
public void onAnimationEnd(Animation animation) {
btn_a2.setBackgroundDrawable(R.id.blank);// Unselect
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
btn_a2.setBackgroundDrawable(R.id.red);// Select
}
};
animation.setAnimationListener(animationOutListener);
animation.setDuration(1000);
btn_a2.startAnimation(animation);
break;
}
case R.id.btn_a2: {
int direction = 1;
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
RelativeLayout.LayoutParams absParams = (RelativeLayout.LayoutParams) btn_a1
.getLayoutParams();
final float xDelta = (displaymetrics.widthPixels / 2)
- absParams.leftMargin - (btn_a1.getWidth() / 2);
final Animation animation = new TranslateAnimation(0, xDelta
* direction, 0, 0);
AnimationListener animationOutListener = new AnimationListener() {
public void onAnimationEnd(Animation animation) {
btn_a1.setBackgroundDrawable(R.id.blank);// Unselect
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
btn_a1.setBackgroundDrawable(R.id.red);// Select
}
};
animation.setDuration(1000);
btn_a1.startAnimation(animation);
break;
}
}
}
How about this:
You would have to evaluate the buttons parentViews onTouchEvents, and if you hit it, you can start the onClick method associated with the button, or when you lift your finger, what ever suits your app.
But while finger down/move you can just change the buttons coordinates and trigger a redraw ont he parent view.
You might have to create a new View that supports moving Buttons. I dont know if you can do that inside of a layout.

ViewHelper in NinaOldAndroid does not work outside of onCreate() method

I try to set ViewHelper.setPivotY(test, 0); in method onClick() but it doesn't work. The animation still scale to the centerY. Here is my code. I don't know where I am wrong?
public class NewClassTest extends Activity{
LinearLayout test;
FrameLayout content;
Button btn;
boolean toggle = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
test = (LinearLayout) findViewById(R.id.test);
content = (FrameLayout) findViewById(R.id.content);
content.setVisibility(View.INVISIBLE);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// ViewHelper.setPivotY(test, 0); does not work
runAnimation(toggle);
toggle = !toggle;
}
});
ViewHelper.setPivotY(test, 0); // work
}
private void runAnimation(boolean in){
AnimatorSet animSet = new AnimatorSet();
ObjectAnimator anim = null, anim2 = null;
// ViewHelper.setPivotY(test, 0); also does not work
if(in){
anim = ObjectAnimator.ofFloat(test, "scaleY", 1, 1 - (((float)content.getHeight()) / ((float) test.getHeight())));
anim2 = ObjectAnimator.ofFloat(content, "translationY", content.getHeight(), 0);
animSet.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
// TODO Auto-generated method stub
content.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
});
}else {
anim = ObjectAnimator.ofFloat(test, "scaleY", 1 - ((float)content.getHeight()) / ((float) test.getHeight()), 1);
anim2 = ObjectAnimator.ofFloat(content, "translationY", 0, content.getHeight());
animSet.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator animation) {
// TODO Auto-generated method stub
content.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
});
}
animSet.setDuration(1000).play(anim2).with(anim);
animSet.start();
}
Thank for your help!.
Happen to me also. test it on HTC One S with android 4.1 and that wasn't worked, but in Samsung Galaxy I with android 2.2 it worked as expected.
Changed it to ViewHelper.setPivotY(test, 0.00000001f); solved it =\
Don't know if it's a bug of HTC, android or nineoldandroids

Categories

Resources