Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7581655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:14:57+00:00 2026-05-30T18:14:57+00:00

I want to create one button/layout object dynamically from the object that exist on

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:14:58+00:00Added an answer on May 30, 2026 at 6:14 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I want to create one large object of many object in JavaScript. Something
I want to create a custom property on one of my entities mapped from
I want to create a view in a one-to-many relation. Here are my relations:
I have one ImageButton in my xml layout like that : <ImageButton android:id=@+id/tabsButton android:background=@drawable/button
How would one create a layout with a button on either side of a
In this example I create three buttons 'one' 'two' 'three'. When clicked I want
I want to create one proc like below but it has error on syntax.
I want to create a one to one mapping between the Entity Customer and
I want to create 4 rectangles stacked to one row. |----|--|------|------------| The width of
I want to create unit tests for one of my project's classes but I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.