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

  • Home
  • SEARCH
  • 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 9134143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:36:30+00:00 2026-06-17T08:36:30+00:00

Apologies for the confusing header. My problem is explained better in the following image:

  • 0

Apologies for the confusing header. My problem is explained better in the following image:enter image description here

I need the green Button to be aligned with the top of the Image, but the Image is inside another Layout. Is this possible?

It can be done in code if necessary; XML is not required. I am targeting Android 2.2 and newer.

EDIT:

My current implementation is to simply set the MarginTop-property of the Button, but this is inconvenient when I need to change the sizes of the text inside the LinearLayout, which I plan to do depending on the screen size.

I think it can be solved by somehow finding the Y coordinate of the Image, perhaps by adding the heights of the TextViews, and then setting this as the MarginTop for the Button, but this sounds cumbersome. Is there really no other option?

The LinearLayout is going to be placed inside a ViewPager (with multiple views, all having an image in the same position), which is why I can’t do it the way preeya explains.

  • 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-17T08:36:31+00:00Added an answer on June 17, 2026 at 8:36 am

    It’s possible but more complicated than including the button into the same layout. If you definitely don’t want to do that, you can’t use XML (which is always faster). You have to do 3 steps in your code:

    1.) Wait until the view is drawn

    private void waitForViewToBeDrawn(){
        // get your layout
        final RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
        ViewTreeObserver vto = mainLayout.getViewTreeObserver();
        // add a listener
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                // you also want to remove that listener
                mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                // go on to next step
                getPositionOfImageView();
    
            }
        }); 
    }
    

    That approach works best for me, but if you have troubles – here are some alternatives.
    There are also [more solutions][2] out there when you use API level 11 and higher…

    2.) Get the top-position of your imageView

        private void getPositionOfImageView(){                
            ImageView imageView = (ImageView) findViewById(R.id.imageView);
            // Top position view relative to parent (Button and ImageView have same parent)
            int topCoordinate = imageView.getTop();
            adjustButton(topCoordinate);
        }
    

    3.) Add or adjust the button in order to be aligned with the image

        public void adjustButton(int topCoordinate){
            Button button = (Button) findViewById(R.id.button);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.topMargin = topCoordinate;
            button.setLayoutParams(params);
        }
    

    This step would be smoother by using API 11: button.setTop(topCoordinate)

    Of course you can shorten all of it and put it in a singele method, just thought that 3 steps are better to explain. Hope that code helps to get started!

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

Sidebar

Related Questions

Apologies for the confusing title, didn't know how to express it better. This is
My apologies if that is a confusing question. I will include pictures to say
apologies for not asking a strictly programming question, but this will move on to
Apologies for the rather confusing title, I'm finding it rather difficult to phrase this
Confusing title...my apologies. What I've got is a table with two related rows. I
I've done a bit of googling round but this particular problem is a little
I have the following XML example that I need some help on. The sample
Apologies for the big block 'o' text, but I'm somewhat of a beginner having
My apologies if the title of this thread is a little confusing. What I'm
I apologise for the confusing title but I'm really not sure how to describe

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.