Example usage for android.widget RelativeLayout getY

List of usage examples for android.widget RelativeLayout getY

Introduction

In this page you can find the example usage for android.widget RelativeLayout getY.

Prototype

@ViewDebug.ExportedProperty(category = "drawing")
public float getY() 

Source Link

Document

The visual y position of this view, in pixels.

Usage

From source file:com.hybris.mobile.lib.ui.view.Alert.java

/**
 * Show the alert//from w w  w.j a v  a2  s  .c o  m
 *
 * @param context                 application-specific resources
 * @param configuration           describes all device configuration information
 * @param text                    message to be displayed
 * @param forceClearPreviousAlert true will clear previous alert else keep it
 */
@SuppressLint("NewApi")
private static void showAlertOnScreen(final Activity context, final Configuration configuration,
        final String text, boolean forceClearPreviousAlert) {

    final ViewGroup mainView = ((ViewGroup) context.findViewById(android.R.id.content));
    boolean currentlyDisplayed = false;
    int viewId = R.id.alert_view_top;
    final TextView textView;
    boolean alertAlreadyExists = false;

    if (configuration.getOrientation().equals(Configuration.Orientation.BOTTOM)) {
        viewId = R.id.alert_view_bottom;
    }

    // Retrieving the view
    RelativeLayout relativeLayout = (RelativeLayout) mainView.findViewById(viewId);

    if (forceClearPreviousAlert) {
        mainView.removeView(relativeLayout);
        relativeLayout = null;
    }

    // Creating the view
    if (relativeLayout == null) {

        // Main layout
        relativeLayout = new RelativeLayout(context);
        relativeLayout.setId(viewId);
        relativeLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, configuration.getHeight()));
        relativeLayout.setGravity(Gravity.CENTER);

        // Textview
        textView = new TextView(context);
        textView.setId(R.id.alert_view_text);
        textView.setGravity(Gravity.CENTER);
        textView.setLayoutParams(
                new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        relativeLayout.addView(textView);

        setIcon(context, configuration, relativeLayout, textView);

        if (configuration.getOrientation().equals(Configuration.Orientation.TOP)) {
            relativeLayout.setY(-configuration.getHeight());
        } else {
            relativeLayout.setY(mainView.getHeight());
        }

        // Adding the view to the global layout
        mainView.addView(relativeLayout, 0);
        relativeLayout.bringToFront();
        relativeLayout.requestLayout();
        relativeLayout.invalidate();
    }
    // View already exists
    else {
        alertAlreadyExists = true;
        textView = (TextView) relativeLayout.findViewById(R.id.alert_view_text);

        if (configuration.getOrientation().equals(Configuration.Orientation.TOP)) {
            if (relativeLayout.getY() == 0) {
                currentlyDisplayed = true;
            }
        } else {
            if (relativeLayout.getY() < mainView.getHeight()) {
                currentlyDisplayed = true;
            }
        }

        // The view is currently shown to the user
        if (currentlyDisplayed) {

            // If the message is not the same, we hide the current message and display the new one
            if (!StringUtils.equals(text, textView.getText())) {
                // Anim out the current message
                ViewPropertyAnimator viewPropertyAnimator = animOut(configuration, mainView, relativeLayout);

                final RelativeLayout relativeLayoutFinal = relativeLayout;

                if (viewPropertyAnimator != null) {
                    // Anim in the new message after the animation out has finished
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                        viewPropertyAnimator.setListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                setIcon(context, configuration, relativeLayoutFinal, textView);
                                animIn(configuration, relativeLayoutFinal, textView, mainView, text);
                            }
                        });
                    } else {
                        viewPropertyAnimator.withEndAction(new Runnable() {
                            @Override
                            public void run() {
                                setIcon(context, configuration, relativeLayoutFinal, textView);
                                animIn(configuration, relativeLayoutFinal, textView, mainView, text);
                            }
                        });
                    }
                } else {
                    setIcon(context, configuration, relativeLayoutFinal, textView);
                    animIn(configuration, relativeLayoutFinal, textView, mainView, text);
                }
            }
        }
    }

    final RelativeLayout relativeLayoutFinal = relativeLayout;

    // Close the alert by clicking the layout
    if (configuration.isCloseable()) {
        relativeLayout.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                animOut(configuration, mainView, relativeLayoutFinal);
                v.performClick();
                return true;
            }
        });
    }

    if (!currentlyDisplayed) {
        // Set the icon in case the alert already exists but it's not currently displayed
        if (alertAlreadyExists) {
            setIcon(context, configuration, relativeLayoutFinal, textView);
        }

        // We anim in the alert
        animIn(configuration, relativeLayoutFinal, textView, mainView, text);
    }
}