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:com.nikola.despotoski.drawerlayoutedgetoggle.DrawerLayoutEdgeToggle.java

public DrawerLayoutEdgeToggle(Activity a, DrawerLayout l, int drawerOpen, int drawerClose,
        boolean keepShadowOnHandle, int drawerGravity) {

    if (drawerGravity != GravityCompat.END && drawerGravity != GravityCompat.START
            && drawerGravity != Gravity.LEFT && drawerGravity != Gravity.RIGHT)
        throw new IllegalArgumentException(
                "Use: GravityCompat.END, GravityCompat.START, Gravity.LEFT or Gravity.RIGHT for drawerGravity parameter");
    mGravity = drawerGravity;//  w  w  w. j av a 2s. com
    mActivity = a;
    mDrawerLayout = l;
    mOpenDrawable = a.getResources().getDrawable(drawerOpen);
    mCloseDrawable = a.getResources().getDrawable(drawerClose);
    rootLayout = (FrameLayout) mActivity.findViewById(android.R.id.content);
    mHandle = new ImageView(a);
    final ViewTreeObserver viewTreeObserver = mHandle.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @SuppressLint("NewApi")
            @Override
            public void onGlobalLayout() {
                mDrawerLayout.measure(MeasureSpec.EXACTLY, MeasureSpec.EXACTLY);
                syncState();
                removeOnGlobalLayoutListener(mHandle, this);
            }
        });
    }

    mHandle.setOnClickListener(mHandleClickListenerInt);
    mHandle.setOnTouchListener(mHandleTouchListenerInt);
    mHandle.setSaveEnabled(true);
    mDrawerLayout.setOnTouchListener(mHandleTouchListenerInt);
    rootLayout.addView(mHandle,
            new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, drawerGravity));
    mKeepShadowOnHandle = keepShadowOnHandle;
    mCurrentDrawable = mCloseDrawable;
}

From source file:com.digitalvotingpass.camera.CameraFragment.java

/**
 * Setup the layout and setup the actions associated with the button.
 *///from w  ww  .j a  va 2 s.c  o  m
@Override
public void onViewCreated(final View view, final Bundle savedInstanceState) {
    mTextureView = (AutoFitTextureView) view.findViewById(R.id.texture);
    scanSegment = (ImageView) view.findViewById(R.id.scan_segment);
    manualInput = (Button) view.findViewById(R.id.manual_input_button);
    overlay = (Overlay) view.findViewById(R.id.overlay);
    manualInput.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), ManualInputActivity.class);
            getActivity().startActivityForResult(intent, MainActivity.GET_DOC_INFO);
        }
    });

    toggleTorchButton = (FloatingActionButton) view.findViewById(R.id.toggle_torch_button);
    toggleTorchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCameraHandler.toggleTorch();
        }
    });

    infoText = (TextView) view.findViewById(R.id.info_text);
    Typeface typeFace = Typeface.createFromAsset(getActivity().getAssets(), "fonts/ro.ttf");
    infoText.setTypeface(typeFace);
    manualInput.setTypeface(typeFace);
    controlPanel = view.findViewById(R.id.control);
    final ViewTreeObserver observer = view.findViewById(R.id.control).getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // Set the margins when the view is available.
            overlay.setMargins(0, 0, 0, controlPanel.getHeight());
            view.findViewById(R.id.control).getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
}

From source file:com.limewoodmedia.nsdroid.activities.World.java

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.world);// w w  w. j a  va  2 s .c o  m
    setTitle(R.string.world);

    // Fetch flag
    LoadingHelper.loadHomeFlag(this);
    imageLoader = Utils.getImageLoader(this);
    this.options = Utils.getImageLoaderDisplayOptions();

    ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true);

    Utils.setupNavigationDrawer(this);

    viewPager = (ViewPager) findViewById(R.id.pager);

    // WFE
    featured = (ViewGroup) getLayoutInflater().inflate(R.layout.world_featured, viewPager, false);
    regionName = (TextView) featured.findViewById(R.id.region_name);
    regionName.setMovementMethod(LinkMovementMethod.getInstance());
    flag = (ImageView) featured.findViewById(R.id.region_flag);
    delegate = (TextView) featured.findViewById(R.id.region_delegate);
    founder = (TextView) featured.findViewById(R.id.region_founder);
    text = (TextView) featured.findViewById(R.id.wfe);
    delegate.setMovementMethod(LinkMovementMethod.getInstance());
    founder.setMovementMethod(LinkMovementMethod.getInstance());
    text.setMovementMethod(LinkMovementMethod.getInstance());
    fieldset = (ViewGroup) featured.findViewById(R.id.fieldset);
    header = (TextView) featured.findViewById(R.id.wfe_header);
    ViewTreeObserver observer = header.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            fieldset.setPadding(fieldset.getPaddingLeft(),
                    header.getHeight() - (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15,
                            getResources().getDisplayMetrics()),
                    fieldset.getPaddingRight(), fieldset.getPaddingBottom());
        }
    });
    layout = (ViewGroup) featured.findViewById(R.id.region_layout);

    // Search
    search = (ViewGroup) getLayoutInflater().inflate(R.layout.world_search, viewPager, false);
    numbers = (TextView) search.findViewById(R.id.world_numbers);
    searchField = (EditText) search.findViewById(R.id.search_field);
    searchButton = (Button) search.findViewById(R.id.search_button);
    searchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            doSearch(searchField.getText().toString());
        }
    });
    searchResults = (LinearLayout) search.findViewById(R.id.search_results);

    // Set up view pager
    viewPager.setAdapter(new WorldPagerAdapter());
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            // Update context menu
            supportInvalidateOptionsMenu();
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });

    // Check if going straight to a page
    if (getIntent().hasExtra("page")) {
        showPage(getIntent().getIntExtra("page", 0));
    }

    //        if(savedInstanceState == null) {
    loadRegion();
    //        } else {
    //           // Restore state
    //           rData = savedInstanceState.getParcelable("region_data");
    //           doSetup();
    //        }
}

From source file:org.openbmap.activities.WifiDetailsMap.java

/**
 * Initializes map components//from   w  ww . j  av  a  2s  .c  o m
 */
@SuppressLint("NewApi")
private void initMap() {
    this.mTileCache = createTileCache();

    if (MapUtils.isMapSelected(this.getActivity())) {
        final Layer offlineLayer = MapUtils.createTileRendererLayer(this.mTileCache,
                this.mMapView.getModel().mapViewPosition, getMapFile(), null, getRenderTheme());
        if (offlineLayer != null)
            this.mMapView.getLayerManager().getLayers().add(offlineLayer);
    } else {
        //this.mMapView.getModel().displayModel.setBackgroundColor(0xffffffff);
        Toast.makeText(this.getActivity(), R.string.info_using_online_map, Toast.LENGTH_LONG).show();

        final OnlineTileSource onlineTileSource = new OnlineTileSource(
                new String[] { "otile1.mqcdn.com", "otile2.mqcdn.com", "otile3.mqcdn.com", "otile4.mqcdn.com" },
                80);
        onlineTileSource.setName("MapQuest").setAlpha(false).setBaseUrl("/tiles/1.0.0/map/").setExtension("png")
                .setParallelRequestsLimit(8).setProtocol("http").setTileSize(256).setZoomLevelMax((byte) 18)
                .setZoomLevelMin((byte) 0);

        mapDownloadLayer = new TileDownloadLayer(mTileCache, mMapView.getModel().mapViewPosition,
                onlineTileSource, AndroidGraphicFactory.INSTANCE);
        mMapView.getLayerManager().getLayers().add(mapDownloadLayer);
        mapDownloadLayer.onResume();
    }

    // register for layout finalization - we need this to get width and height
    final ViewTreeObserver vto = mMapView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @SuppressLint("NewApi")
        @Override
        public void onGlobalLayout() {

            layoutInflated = true;
            proceedAfterHeatmapCompleted();

            final ViewTreeObserver obs = mMapView.getViewTreeObserver();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                obs.removeOnGlobalLayoutListener(this);
            } else {
                obs.removeGlobalOnLayoutListener(this);
            }
        }
    });

    // Register for zoom changes
    this.mapObserver = new Observer() {
        @Override
        public void onChange() {

            final byte newZoom = mMapView.getModel().mapViewPosition.getZoomLevel();
            if (newZoom != currentZoom) {
                // update overlays on zoom level changed
                Log.i(TAG, "New zoom level " + newZoom + ", reloading map objects");
                Log.i(TAG, "Update" + updatePending);
                // cancel pending heat-maps
                //if (builder != null) {
                //   builder.cancel(true);
                //}

                // if another update is pending, wait for complete
                if (!updatePending) {
                    clearLayer();
                    proceedAfterHeatmapCompleted();
                }
                currentZoom = newZoom;
            }
        }
    };
    this.mMapView.getModel().mapViewPosition.addObserver(mapObserver);

    this.mMapView.setClickable(true);
    this.mMapView.getMapScaleBar().setVisible(true);

    /*
       Layers layers = layerManager.getLayers();
       // remove all layers including base layer
       layers.clear();
     */

    this.mMapView.getModel().mapViewPosition.setZoomLevel((byte) 16);

}

From source file:com.afollestad.polar.ui.MainActivity.java

void moveTabsIfNeeded() {
    final CharSequence currentTitle = getTitle();

    String longestTitle = null;/*from   w  w  w.  j  a v  a2  s  .co m*/
    for (PagesBuilder.Page page : mPages) {
        String title = getString(page.titleRes);
        if (longestTitle == null || title.length() > longestTitle.length()) {
            longestTitle = title;
        }
    }
    setTitle(longestTitle);

    if (mTabs != null) {
        ViewTreeObserver vto = mToolbar.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {
                if (mToolbar.isTitleTruncated() && mTabs.getParent() == mToolbar) {
                    mToolbar.removeView(mTabs);
                    //noinspection ConstantConditions
                    mAppBarLinear.addView(mTabs);
                }

                setTitle(currentTitle);

                mToolbar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }
}

From source file:de.manumaticx.crouton.Manager.java

/**
 * Adds a {@link Crouton} to the {@link ViewParent} of it's {@link Activity}.
 *
 * @param crouton//w w w.java 2 s.  com
 *     The {@link Crouton} that should be added.
 */
private void addCroutonToView(final Crouton crouton) {
    // don't add if it is already showing
    if (crouton.isShowing()) {
        return;
    }

    final View croutonView = crouton.getView();
    if (null == croutonView.getParent()) {
        ViewGroup.LayoutParams params = croutonView.getLayoutParams();
        if (null == params) {
            params = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        // display Crouton in ViewGroup is it has been supplied
        if (null != crouton.getViewGroup()) {
            // TODO implement add to last position feature (need to align with how this will be requested for activity)
            if (crouton.getViewGroup() instanceof FrameLayout) {
                crouton.getViewGroup().addView(croutonView, params);
            } else {
                crouton.getViewGroup().addView(croutonView, 0, params);
            }
        } else {
            Activity activity = crouton.getActivity();
            if (null == activity || activity.isFinishing()) {
                return;
            }
            handleTranslucentActionBar((ViewGroup.MarginLayoutParams) params, activity);

            if (Looper.myLooper() == Looper.getMainLooper()) {
                activity.addContentView(croutonView, params);
            } else {
                final Activity mActivity = activity;
                final ViewGroup.LayoutParams mParams = params;
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mActivity.addContentView(croutonView, mParams);
                    }
                });
            }
        }
    }

    croutonView.requestLayout(); // This is needed so the animation can use the measured with/height
    ViewTreeObserver observer = croutonView.getViewTreeObserver();
    if (null != observer) {
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            @TargetApi(16)
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                    croutonView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } else {
                    croutonView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }

                croutonView.startAnimation(crouton.getInAnimation());
                announceForAccessibilityCompat(crouton.getActivity(), crouton.getText());
                if (Configuration.DURATION_INFINITE != crouton.getConfiguration().durationInMilliseconds) {
                    sendMessageDelayed(crouton, Messages.REMOVE_CROUTON,
                            crouton.getConfiguration().durationInMilliseconds
                                    + crouton.getInAnimation().getDuration());
                }
            }
        });
    }
}

From source file:org.ciasaboark.tacere.activity.fragment.EventsFragment.java

private void showFirstRunWizardIfNeeded() {
    if (prefs.isFirstRun()) {
        prefs.disableFirstRun();/*from w  ww .java  2s  . co  m*/
        final ViewTreeObserver viewTreeObserver = ((Activity) context).getWindow().getDecorView()
                .getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (!showingTutorial) {
                    showingTutorial = true;
                    startActivity(new Intent(context.getApplicationContext(), TutorialActivity.class));
                }
                if (viewTreeObserver.isAlive()) {
                    if (Build.VERSION.SDK_INT >= 16) {
                        viewTreeObserver.removeOnGlobalLayoutListener(this);
                    } else {
                        viewTreeObserver.removeGlobalOnLayoutListener(this);
                    }
                }
            }
        });
    }
}

From source file:com.atlas.mars.weatherradar.MainActivity.java

private void setSisze() {
    final MainActivity mainActivity = this;
    /* ViewTreeObserver observer = ((LinearLayout)buttonReload.getParent()).getViewTreeObserver();
     observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override// ww w.  j  a  v a 2 s .c o m
    public void onGlobalLayout() {
        buttonReload.setLayoutParams(new  LinearLayout.LayoutParams (buttonReload.getHeight(),buttonReload.getHeight() ));
        buttonMenu.setLayoutParams(new  LinearLayout.LayoutParams ((int)(buttonReload.getHeight()/1.5),buttonMenu.getHeight() ));
    }
     });*/

    ViewTreeObserver observer1 = (scrollView).getViewTreeObserver();
    observer1.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            //  scrollView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

            if (Build.VERSION.SDK_INT < 16) {
                scrollView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } else {
                scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }

            Log.d(TAG, "" + scrollView.getHeight() + " : " + scrollView.getChildAt(0).getHeight());
            scrollSliderSize = scrollView.getChildAt(0).getHeight() - scrollView.getHeight();
            new ScrollObserv(mainActivity, scrollView, scrollSliderSize);
            //todo ?  ?  ?
            //  scrollView.scrollTo(0, scrollView.getChildAt(0).getHeight() - scrollView.getHeight());
        }
    });
}

From source file:de.uulm.graphicalpasswords.openmiba.MIBACreatePasswordActivity.java

private void setViews() {
    tblLayout = (TableLayout) findViewById(R.id.tableLayout);
    btnBack = (Button) findViewById(R.id.miba_btnBack);
    btnBack.setOnClickListener(new View.OnClickListener() {

        @Override//from w  ww  .j av  a  2 s  .  c  o m
        public void onClick(View v) {
            back();
        }
    });
    btnDone = (Button) findViewById(R.id.miba_btnCreateMasterKey);

    tvRound = (TextView) this.findViewById(R.id.tvRound);
    tvRound.setText(getString(R.string.label_round) + " 1");

    linlGrid = new LinearLayout[2][4];
    linlGrid[0][0] = (LinearLayout) this.findViewById(R.id.square1);
    linlGrid[1][0] = (LinearLayout) this.findViewById(R.id.square2);
    linlGrid[0][1] = (LinearLayout) this.findViewById(R.id.square3);
    linlGrid[1][1] = (LinearLayout) this.findViewById(R.id.square4);
    linlGrid[0][2] = (LinearLayout) this.findViewById(R.id.square5);
    linlGrid[1][2] = (LinearLayout) this.findViewById(R.id.square6);
    linlGrid[0][3] = (LinearLayout) this.findViewById(R.id.square7);
    linlGrid[1][3] = (LinearLayout) this.findViewById(R.id.square8);

    for (int y = 0; y < 4; y++) {
        for (int x = 0; x < 2; x++) {
            linlGrid[x][y].setBackgroundColor(colors_off[x][y]);
        }

    }

    // get width and height from mainpanel
    // can not use display width/height because of notification bar
    ViewTreeObserver vto = tblLayout.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            // get values of portrait mode

            Display display = getWindowManager().getDefaultDisplay();

            if (display.getRotation() == Surface.ROTATION_0) {
                width = tblLayout.getWidth();
                height = tblLayout.getHeight();
            } else {
                height = tblLayout.getWidth();
                width = tblLayout.getHeight();
            }

            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ccp000);
            bmp = Bitmap.createScaledBitmap(bmp, width, height, true);
            Drawable d = new BitmapDrawable(getResources(), bmp);
            tblLayout.setBackgroundDrawable(d);

            touchlistener = new TouchListener(linlGrid, width, height, handler);
            tblLayout.setOnTouchListener(touchlistener);
            // remove listener again otherwise it gets called twice
            tblLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    });

    btnDone.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            DialogFragment dialog = new RememberPasswordDialogFragment();
            dialog.show(getFragmentManager(), "remember");
        }
    });
    btnDone.setVisibility(View.VISIBLE);
}

From source file:com.italankin.dictionary.ui.main.MainActivity.java

private void setupInputLayout() {
    mInputLayout.setOnClickListener(new View.OnClickListener() {
        @Override//from   w  ww  .  j  a  v  a2s . co  m
        public void onClick(View v) {
            mInput.requestFocus();
            mInputManager.showSoftInput(mInput, 0);
        }
    });
    ViewTreeObserver vto = mInputLayout.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT >= 16) {
                ViewTreeObserver vto = mInputLayout.getViewTreeObserver();
                vto.removeOnGlobalLayoutListener(this);
            }
            updateInputLayoutPosition();
        }
    });

    mInput.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                lookupNext(mInput.getText().toString());
                return true;
            }
            return false;
        }
    });
}