position Relative Layout Child Underneath View - Android User Interface

Android examples for User Interface:Layout

Description

position Relative Layout Child Underneath View

Demo Code


//package com.java2s;

import android.graphics.Rect;

import android.view.View;
import android.view.ViewGroup;

public class Main {
    public static void positionRelativeLayoutChildUnderneathView(
            View child, View view) {
        Rect viewLocation = getViewLocationRelativeTo(view,
                (View) child.getParent());

        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) child
                .getLayoutParams();//from www  .  ja v  a 2 s .co  m
        params.topMargin = viewLocation.bottom;
        child.setLayoutParams(params);
    }

    public static Rect getViewLocationRelativeTo(View view,
            View relativeToView) {
        int[] viewLocation = new int[2];
        view.getLocationInWindow(viewLocation);

        int[] relativeToViewLocation = new int[2];
        relativeToView.getLocationInWindow(relativeToViewLocation);

        int x = viewLocation[0] - relativeToViewLocation[0];
        int y = viewLocation[1] - relativeToViewLocation[1];
        int w = view.getWidth();
        int h = view.getHeight();
        return new Rect(x, y, x + w, y + h);
    }
}

Related Tutorials