Question

Hi I am creating a custom view in android.I have a LinerarLayout to which I am adding the custom views.I manage to add one custom view programmatically but if I add another it's not getting added into the layout.I don't know where am going wrong.My Main activity.

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

        LinearLayout container = (LinearLayout) findViewById(R.id.container);
        final MyAnimationView animView = new MyAnimationView(this); 
        container.addView(animView);
        final MyAnimationView animView1 = new MyAnimationView(this);        
        container.addView(animView1);       
    }

and my custom view class

public class MyAnimationView extends TextView implements ValueAnimator.AnimatorUpdateListener {

public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
AnimatorSet animation = null;
private float mDensity;

public MyAnimationView(Context context) {
    super(context);        
    mDensity = getContext().getResources().getDisplayMetrics().density;
    ShapeHolder ball0 = addBall(75f, 400f);             

}
public MyAnimationView(Context context,float x,float y) {
    super(context);     
    mDensity = getContext().getResources().getDisplayMetrics().density;
    ShapeHolder ball0 = addBall(105f, 400f);         



}

private void createAnimation() {
    if (animation == null) {                
        ObjectAnimator anim0=ObjectAnimator.ofFloat(balls.get(0),"y",getHeight(),500f).setDuration(500); 
        animation = new AnimatorSet();
        animation.playTogether(anim0);              
        anim0.addUpdateListener(this);


  }
}

private ShapeHolder addBall(float x, float y) {
    OvalShape circle = new OvalShape();
    circle.resize(100f * mDensity, 100f * mDensity);
    ShapeDrawable drawable = new ShapeDrawable(circle);
    ShapeHolder shapeHolder = new ShapeHolder(drawable);
    shapeHolder.setX(x - 25f);
    shapeHolder.setY(y - 25f);
    int red = (int)(100 + Math.random() * 155);
    int green = (int)(100 + Math.random() * 155);
    int blue = (int)(100 + Math.random() * 155);
    int color = 0xff000000 | red << 16 | green << 8 | blue;
    Paint paint = drawable.getPaint(); //new Paint(Paint.ANTI_ALIAS_FLAG);
    int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4;
    RadialGradient gradient = new RadialGradient(37.5f, 12.5f,
            50f, color, darkColor, Shader.TileMode.CLAMP);
    paint.setShader(gradient);
    shapeHolder.setPaint(paint);
    balls.add(shapeHolder);
    return shapeHolder;
}

@Override
protected void onDraw(Canvas canvas) {
    for (int i = 0; i < balls.size(); ++i) {
        ShapeHolder shapeHolder = balls.get(i);
        canvas.save();
        canvas.translate(shapeHolder.getX(), shapeHolder.getY());
        shapeHolder.getShape().draw(canvas);
        canvas.restore();
    }
}

public void startAnimation() {
    createAnimation();
    animation.start();
}

public void onAnimationUpdate(ValueAnimator animation) {
    invalidate();
}
}

EDIT:here is my linear layout

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

<Button
    android:id="@+id/startButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />   

any help will be greatly appreciated.

Was it helpful?

Solution 2

No it will be added but you can't see it. because your first view will take the full space of your screen. And your second will be added in below of your first view so it will be hidden from visible part of your device. I also met the same problem. I solved this with two layouts. see here for my solution.

I assume this is the problem that is why I provided this solution if not kindly let me know..

OTHER TIPS

Just an assumption, don´t know if it works. Try to do this in Your main:

    LinearLayout container = (LinearLayout) findViewById(R.id.container);

    final MyAnimationView animView = new MyAnimationView(this); 

    animView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.
                              WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));

    container.addView(animView);

    final MyAnimationView animView1 = new MyAnimationView(this); 

     animView1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.
                                 WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));

    container.addView(animView1);

does it work?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top