Example usage for android.view View getRootView

List of usage examples for android.view View getRootView

Introduction

In this page you can find the example usage for android.view View getRootView.

Prototype

public View getRootView() 

Source Link

Document

Finds the topmost view in the current view hierarchy.

Usage

From source file:com.nextgis.mobile.fragment.MapFragment.java

protected void checkCompass(boolean showCompass) {
    int compassContainer = R.id.fl_compass;
    final FrameLayout compass = (FrameLayout) mMapRelativeLayout.findViewById(compassContainer);

    if (!showCompass) {
        compass.setVisibility(View.GONE);
        return;/*  w  w w  . ja v  a 2 s . co  m*/
    }

    FragmentManager fragmentManager = mActivity.getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    //get or create fragment
    CompassFragment compassFragment = (CompassFragment) fragmentManager.findFragmentByTag("NEEDLE_COMPASS");
    if (null == compassFragment)
        compassFragment = new CompassFragment();

    compass.setClickable(false);
    compassFragment.setStyle(true);
    if (!compassFragment.isAdded())
        fragmentTransaction.add(compassContainer, compassFragment, "NEEDLE_COMPASS")
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

    if (!compassFragment.isVisible()) {
        fragmentTransaction.show(compassFragment);
    }

    fragmentTransaction.commit();

    compass.setVisibility(View.VISIBLE);
    compass.setOnClickListener(this);
    compass.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            mIsCompassDragging = true;
            mVibrator.vibrate(5);
            return true;
        }
    });
    // Thanks to http://javatechig.com/android/how-to-drag-a-view-in-android
    compass.setOnTouchListener(new View.OnTouchListener() {
        private int _xDelta;
        private int _yDelta;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            final int X = (int) event.getRawX();
            final int Y = (int) event.getRawY();
            switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                _xDelta = X - lParams.leftMargin;
                _yDelta = Y - lParams.topMargin;
                return false;
            case MotionEvent.ACTION_UP:
                mIsCompassDragging = false;
                return false;
            case MotionEvent.ACTION_MOVE:
                if (!mIsCompassDragging)
                    return false;

                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                int width = v.getWidth();
                int height = v.getHeight();
                int toolbarHeight = 0;
                if (mActivity.getSupportActionBar() != null)
                    toolbarHeight = mActivity.getSupportActionBar().getHeight();
                if (X > width / 3 && X < v.getRootView().getWidth() - width / 3)
                    layoutParams.leftMargin = X - _xDelta;
                if (Y > height / 2 + toolbarHeight && Y < v.getRootView().getHeight() - height / 2)
                    layoutParams.topMargin = Y - _yDelta;

                v.setLayoutParams(layoutParams);
                break;
            }
            mMapRelativeLayout.invalidate();
            return true;
        }
    });
}