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 7934357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:37:30+00:00 2026-06-03T21:37:30+00:00

I have one ImageView in my application which can be situated anywhere on screen

  • 0

I have one ImageView in my application which can be situated anywhere on screen

On touch I want to move this view at the center of the Screen. I tried this functionality with Translate Animation and its RELATIVE_TO_PARENT functionality as follows

TranslateAnimation translateAnimation1 = new TranslateAnimation(
      TranslateAnimation.RELATIVE_TO_PARENT,0.0f,
      TranslateAnimation.RELATIVE_TO_PARENT,0.5f,
      TranslateAnimation.RELATIVE_TO_PARENT,0.0f,
      TranslateAnimation.RELATIVE_TO_PARENT,0.5f);

but ImageView moves 50% (of the screen) down from its current position.

Is there any way to move this view to the center of the screen regardless of its current position?

  • 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-06-03T21:37:31+00:00Added an answer on June 3, 2026 at 9:37 pm

    In order to move a View anywhere on the screen, I would recommend placing it in a full screen layout. By doing so, you won’t have to worry about clippings or relative coordinates.

    You can try this sample code:

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" android:id="@+id/rootLayout">
    
        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MOVE" android:layout_centerHorizontal="true"/>
    
        <ImageView
            android:id="@+id/img1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" android:layout_marginLeft="10dip"/>
        <ImageView
            android:id="@+id/img2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" android:layout_centerVertical="true" android:layout_alignParentRight="true"/>
        <ImageView
            android:id="@+id/img3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" android:layout_marginLeft="60dip" android:layout_alignParentBottom="true" android:layout_marginBottom="100dip"/>
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" android:clipChildren="false" android:clipToPadding="false">
    
            <ImageView
                android:id="@+id/img4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" android:layout_marginLeft="60dip" android:layout_marginTop="150dip"/>
        </LinearLayout>
    
    </RelativeLayout>
    

    Your activity

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        ((Button) findViewById( R.id.btn1 )).setOnClickListener( new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                ImageView img = (ImageView) findViewById( R.id.img1 );              
                moveViewToScreenCenter( img );
                img = (ImageView) findViewById( R.id.img2 );
                moveViewToScreenCenter( img );
                img = (ImageView) findViewById( R.id.img3 );                
                moveViewToScreenCenter( img );
                img = (ImageView) findViewById( R.id.img4 );
                moveViewToScreenCenter( img );
            }
        });
    }
    
    private void moveViewToScreenCenter( View view )
    {
        RelativeLayout root = (RelativeLayout) findViewById( R.id.rootLayout );
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics( dm );
        int statusBarOffset = dm.heightPixels - root.getMeasuredHeight();
    
        int originalPos[] = new int[2];
        view.getLocationOnScreen( originalPos );
    
        int xDest = dm.widthPixels/2;
        xDest -= (view.getMeasuredWidth()/2);
        int yDest = dm.heightPixels/2 - (view.getMeasuredHeight()/2) - statusBarOffset;
    
        TranslateAnimation anim = new TranslateAnimation( 0, xDest - originalPos[0] , 0, yDest - originalPos[1] );
        anim.setDuration(1000);
        anim.setFillAfter( true );
        view.startAnimation(anim);
    }
    

    The method moveViewToScreenCenter gets the View’s absolute coordinates and calculates how much distance has to move from its current position to reach the center of the screen. The statusBarOffset variable measures the status bar height.

    I hope you can keep going with this example. Remember that after the animation your view’s position is still the initial one. If you tap the MOVE button again and again the same movement will repeat. If you want to change your view’s position do it after the animation is finished.

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

Sidebar

Related Questions

i have develop one application in which i want to play gif animation. for
i am working on one application in which i have created one table view
I won't create wallpaper-application but have one problem. Please tell me how add ImageView
i have developed one application in which getting install app information like name,package name,
I have a linearlayout in which i have one image view, one textview ,
In My application XML layout, I have one Relativelayout, In which there are two
I have a simple LinearLayout with one TextView and one ImageView. I want the
I already developed one application which works with 480x800 screen size. But when I
I want to provide an animation when my application starts. I have just one
I have a layout that contains two ImageViews. I want one of them to

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.