Example usage for android.view ViewTreeObserver addOnGlobalLayoutListener

List of usage examples for android.view ViewTreeObserver addOnGlobalLayoutListener

Introduction

In this page you can find the example usage for android.view ViewTreeObserver addOnGlobalLayoutListener.

Prototype

public void addOnGlobalLayoutListener(OnGlobalLayoutListener listener) 

Source Link

Document

Register a callback to be invoked when the global layout state or the visibility of views within the view tree changes

Usage

From source file:fr.tvbarthel.apps.sayitfromthesky.activities.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);

    mActionBarTitleColorSpan = new AlphaForegroundColorSpan(getResources().getColor(R.color.material_grey_300));
    mActionBarTitleSpannable = new SpannableString(getString(R.string.app_name));
    setActionBarTitleAlpha(1);/*from www.  j  a va  2  s  .  c o m*/

    // Compute the action bar size
    mActionBarSize = ActionBarHelper.getActionBarSize(this);

    // Set the height of the header container to 1/3.5 of the root height.
    ViewTreeObserver vto = mRootView.getViewTreeObserver();
    if (vto.isAlive()) {
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ViewHelper.removeOnGlobalLayoutListener(mRootView, this);
                final int headerHeight = (int) (mRootView.getHeight() / 3.5);
                mHeaderContainer.getLayoutParams().height = headerHeight;
                mListView.setPadding(0, headerHeight - mActionBarSize, 0, 0);
                final int actionBarLogoSize = getResources()
                        .getDimensionPixelSize(R.dimen.action_bar_logo_size);
                final int actionBarMarginLeft = getResources()
                        .getDimensionPixelOffset(R.dimen.action_bar_margin_left);
                mHeaderLogoMaxTranslationX = (mHeaderContainer.getWidth() - actionBarLogoSize) / 2
                        - actionBarMarginLeft;
                mHeaderLogoFinalScale = ((float) actionBarLogoSize) / mHeaderLogo.getWidth();
            }

        });
    }

    mDrawingAdapter = new DrawingAdapter(this);
    initListView();
    getLoaderManager().initLoader(LOADER_ID_DRAWINGS, null, this);
    ViewHelper.slideFromBottom(mBtnNewDrawing);
}

From source file:com.limewoodmedia.nsdroid.fragments.IssueDetailFragment.java

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    root = inflater.inflate(R.layout.issue_detail, null, false);
    title = (TextView) root.findViewById(R.id.issue_detail_title);
    ViewTreeObserver observer = title.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override/* w w  w .j  a  v a2  s  . co m*/
        public void onGlobalLayout() {
            scrollView.setPadding(scrollView.getPaddingLeft(),
                    title.getHeight() - (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15,
                            getResources().getDisplayMetrics()),
                    scrollView.getPaddingRight(), scrollView.getPaddingBottom());
        }
    });
    theIssue = (TextView) root.findViewById(R.id.the_issue_title);
    text = (TextView) root.findViewById(R.id.issue_text);
    theDebate = (TextView) root.findViewById(R.id.the_debate_title);
    choicesArea = (ViewGroup) root.findViewById(R.id.issue_choices_area);
    scrollView = (ScrollView) root.findViewById(R.id.issue_scroll_view);
    layout = (ViewGroup) root.findViewById(R.id.layout);

    ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

    return root;
}

From source file:org.zywx.wbpalmstar.plugin.uexscrawl.PhotoScrawlActivity.java

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.plugin_uexscrawl_photo_edit_layout);
    initViews();//from   w  w  w  . j a  v a  2 s.  c om
    Intent intent = getIntent();
    mPath = intent.getStringExtra(KEY_INTENT_IMAGE_PATH);
    ViewTreeObserver vto2 = mImageContentLayout.getViewTreeObserver();
    vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT < 16) {
                mImageContentLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } else {
                mImageContentLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
            resizeBitmap();
        }
    });
}

From source file:com.example.linhdq.test.MonitoredActivity.java

/**
 * position the app icon at the bottom of the action bar and start animation
 *//*from  w ww.j a  v a 2s  .c  o m*/
private void initAppIcon(final int dialogId, ImageView appIcon) {
    setDialogId(dialogId);
    mAppIcon = appIcon;
    final ViewTreeObserver viewTreeObserver = appIcon.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(this);

    }
}

From source file:com.givon.baseproject.xinlu.fragment.FraHome.java

private void InitViewPager(View parentView) {
    mPager = (MyViewPager) parentView.findViewById(R.id.vPager);
    mPager.setOffscreenPageLimit(2);//w  w  w . j  a v a 2s.c  o  m
    final int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    final int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    ViewTreeObserver vto = mPager.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        public void onGlobalLayout() {
            mPager.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            View view = mPager.getChildAt(mPager.getCurrentItem());
            view.measure(w, h);
            LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            // params.height = view.getMeasuredHeight();
            params.height = BaseApplication.mHeight;
            mPager.setLayoutParams(params);
        }
    });
    fragmentsList = new ArrayList<Fragment>();

    home1 = new FraTuiJian(this);
    home2 = new FraFriend(this);
    home3 = new FraActive(this);

    fragmentsList.add(home1);
    fragmentsList.add(home2);
    fragmentsList.add(home3);

    mPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentsList));
    mPager.setOnPageChangeListener(new MyOnPageChangeListener());
    mPager.setCurrentItem(0);

}

From source file:de.lmu.ifi.medien.probui.system.ProbUIManager.java

public ProbUIManager(View view, int containerID) {

    this.view = view;
    container = (ProbUIContainer) view.findViewById(containerID);
    container.registerProbUIManager(this);
    // Call finaliseSetup() after Android has finished layouting the container:
    ViewTreeObserver vto = container.getViewGroup().getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override// www. java2  s .  c o m
        public void onGlobalLayout() {
            container.getViewGroup().getViewTreeObserver().removeGlobalOnLayoutListener(this);
            screenWidth = container.getViewGroup().getMeasuredWidth();
            screenHeight = container.getViewGroup().getMeasuredHeight();
            Log.d("ProbUIManager", "onGlobalLayout: " + screenWidth + ", " + screenHeight);
            finaliseSetup();
        }
    });

    // init probInteractors list:
    this.probInteractors = new ArrayList<ProbInteractor>();

    this.nonProbInteractors = new ArrayList<View>();

    // init mediator:
    this.mediator = new ProbUIMediatorImpl();
}

From source file:quickbeer.android.features.barcodescanner.BarcodeScanActivity.java

/**
 * Initializes the UI and creates the detector pipeline.
 *//*w  w w . ja  va  2s . com*/
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    analytics = new Analytics(this);
    analytics.createEvent(Events.Screen.BARCODE_SCANNER);

    setContentView(R.layout.barcode_scan_activity);

    mPreview = (CameraSourcePreview) findViewById(R.id.preview);
    mGraphicOverlay = (GraphicOverlay<BarcodeGraphic>) findViewById(R.id.graphicOverlay);

    ViewTreeObserver viewTreeObserver = mPreview.getViewTreeObserver();

    if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mPreview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                viewWidth = mPreview.getWidth();
                viewHeight = mPreview.getHeight();

                // Check for the camera permission before accessing the camera.  If the
                // permission is not granted yet, request permission.
                int rc = ContextCompat.checkSelfPermission(BarcodeScanActivity.this,
                        Manifest.permission.CAMERA);
                if (rc == PackageManager.PERMISSION_GRANTED) {
                    createCameraSource();
                    startCameraSource();
                } else {
                    requestCameraPermission();
                }
            }
        });
    }
}

From source file:com.freegeek.android.materialbanner.MaterialBanner.java

public MaterialBanner setIndicator(PageIndicator pageIndicator) {
    if (mPageIndicator == pageIndicator)
        return this;

    //remove old indicator view first;
    if (mPageIndicator != null)
        mIndicatorContainer.removeView(mPageIndicator.getView());
    mPageIndicator = pageIndicator;//  ww w .ja  va 2  s.  co m
    mPageIndicator.setViewPager(mViewPager);
    mPageIndicator.setCurrentItem(getCurrentItem());
    mIndicatorContainer.addView(mPageIndicator.getView(), mIndicatorParams);
    //update listener
    setOnPageChangeListener(mOnPageChangeListener);
    //get the real height then update margin;
    ViewTreeObserver observer = mIndicatorContainer.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            updateMargin();
        }
    });
    return this;
}

From source file:com.cypress.cysmart.BLEServiceFragments.RGBFragment.java

private void setDefaultColorPickerPositionColor() {
    ViewTreeObserver observer = mcolorpicker.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override/*from   www.ja  va2  s  .co m*/
        public void onGlobalLayout() {
            mcolorpicker.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            int[] locations = new int[2];
            mcolorpicker.getLocationOnScreen(locations);
            int x = locations[0];
            int y = locations[1];
            if (x < mBmp.getWidth() && y < mBmp.getHeight()) {
                int p = mBmp.getPixel(x, y);
                if (p != 0) {
                    r = Color.red(p);
                    g = Color.green(p);
                    b = Color.blue(p);
                    Logger.i("r--->" + r + "g-->" + g + "b-->" + b);
                    UIupdation();
                }
            }
        }
    });

}

From source file:com.luke.lukef.lukeapp.fragments.NewSubmissionFragment.java

@Nullable
@Override//from  w w w . jav a  2 s  .  co m
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    this.fragmentView = inflater.inflate(R.layout.fragment_new_submission, container, false);
    // find the views and add listeners
    findViews();
    setupClickListeners();
    fetchBundleFromArguments();

    ArrayList<String> selectedCategories = new ArrayList<>();
    this.categorySelectButton.setOnClickListener(this);
    this.confirmedCategories = new ArrayList<>();
    this.tempCategories = new ArrayList<>();

    ViewTreeObserver vto = this.mapThumbnail.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            final LukeNetUtils lukeNetUtils = new LukeNetUtils(getMainActivity());
            getMainActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        mapThumbnail.setImageBitmap(lukeNetUtils.getMapThumbnail(location,
                                mapThumbnail.getWidth(), mapThumbnail.getHeight()));
                    } catch (ExecutionException | InterruptedException e) {
                        Log.e(TAG, "run: ERROR SETTING IMAGE", e);
                    }
                }
            });
            photoThumbnail.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            Log.e(TAG, "onGlobalLayout: photothumnailImageview dimensions:" + photoThumbnail.getWidth() + " x "
                    + photoThumbnail.getHeight());
        }
    });
    return fragmentView;
}