Code example for View

Methods: getHeight, getLocationOnScreen, getParent

0
 
	public View getScrollOrListParent(View view) {
 
	    if (!(view instanceof android.widget.AbsListView) && !(view instanceof android.widget.ScrollView) && !(view instanceof WebView)) {
	        try{ 
	            return getScrollOrListParent((View) view.getParent());
	        }catch(Exception e){
	            return null; 
	        } 
	    } else { 
	        return view;
	    } 
	} 
 
	/** 
	 * Returns views from the shown DecorViews. 
	 * 
	 * @param onlySufficientlyVisible if only sufficiently visible views should be returned 
	 * @return all the views contained in the DecorViews 
	 */ 
 
	public ArrayList<View> getAllViews(boolean onlySufficientlyVisible) {
		final View[] views = getWindowDecorViews();
		final ArrayList<View> allViews = new ArrayList<View>();
		final View[] nonDecorViews = getNonDecorViews(views);
		View view = null;
 
		if(nonDecorViews != null){
			for(int i = 0; i < nonDecorViews.length; i++){
				view = nonDecorViews[i];
				try { 
					addChildren(allViews, (ViewGroup)view, onlySufficientlyVisible);
				} catch (Exception ignored) {}
				if(view != null) allViews.add(view);
			} 
		} 
 
		if (views != null && views.length > 0) {
			view = getRecentDecorView(views);
			try { 
				addChildren(allViews, (ViewGroup)view, onlySufficientlyVisible);
			} catch (Exception ignored) {}
 
			if(view != null) allViews.add(view);
		} 
 
		return allViews;
	} 
 
	/** 
	 * Returns the most recent DecorView 
	 * 
	 * @param views the views to check 
	 * @return the most recent DecorView 
	 */ 
 
	 public final View getRecentDecorView(View[] views) {
		 if(views == null)
			 return null; 
		  
		 final View[] decorViews = new View[views.length];
		 int i = 0;
		 View view;
 
		 for (int j = 0; j < views.length; j++) {
			 view = views[j];
			 if (view != null && view.getClass().getName()
					 .equals("com.android.internal.policy.impl.PhoneWindow$DecorView")) {
				 decorViews[i] = view;
				 i++;
			 } 
		 } 
		 return getRecentContainer(decorViews);
	 } 
 
	/** 
	 * Returns the most recent view container 
	 * 
	 * @param views the views to check 
	 * @return the most recent view container 
	 */ 
 
	 private final View getRecentContainer(View[] views) {
		 View container = null;
		 long drawingTime = 0;
		 View view;
 
		 for(int i = 0; i < views.length; i++){
			 view = views[i];
			 if (view != null && view.isShown() && view.hasWindowFocus() && view.getDrawingTime() > drawingTime) {
				 container = view;
				 drawingTime = view.getDrawingTime();
			 } 
		 } 
		 return container;
	 } 
 
	 /** 
	  * Returns all views that are non DecorViews 
	  * 
	  * @param views the views to check 
	  * @return the non DecorViews 
	  */ 
 
	 private final View[] getNonDecorViews(View[] views) {
		 View[] decorViews = null;
 
		 if(views != null) {
			 decorViews = new View[views.length];
 
			 int i = 0;
			 View view;
 
			 for (int j = 0; j < views.length; j++) {
				 view = views[j];
				 if (view != null && !(view.getClass().getName()
						 .equals("com.android.internal.policy.impl.PhoneWindow$DecorView"))) {
					 decorViews[i] = view;
					 i++;
				 } 
			 } 
		 } 
		 return decorViews;
	 } 
 
 
 
	/** 
	 * Extracts all {@code View}s located in the currently active {@code Activity}, recursively. 
	 * 
	 * @param parent the {@code View} whose children should be returned, or {@code null} for all 
	 * @param onlySufficientlyVisible if only sufficiently visible views should be returned 
	 * @return all {@code View}s located in the currently active {@code Activity}, never {@code null} 
	 */ 
 
	public ArrayList<View> getViews(View parent, boolean onlySufficientlyVisible) {
		final ArrayList<View> views = new ArrayList<View>();
		final View parentToUse;
 
		if (parent == null){
			return getAllViews(onlySufficientlyVisible);
		}else{ 
			parentToUse = parent;
 
			views.add(parentToUse);
 
			if (parentToUse instanceof ViewGroup) {
				addChildren(views, (ViewGroup) parentToUse, onlySufficientlyVisible);
			} 
		} 
		return views;
	} 
 
	/** 
	 * Adds all children of {@code viewGroup} (recursively) into {@code views}. 
	 * 
	 * @param views an {@code ArrayList} of {@code View}s 
	 * @param viewGroup the {@code ViewGroup} to extract children from 
	 * @param onlySufficientlyVisible if only sufficiently visible views should be returned 
	 */ 
 
	private void addChildren(ArrayList<View> views, ViewGroup viewGroup, boolean onlySufficientlyVisible) {
		if(viewGroup != null){
			for (int i = 0; i < viewGroup.getChildCount(); i++) {
				final View child = viewGroup.getChildAt(i);
 
				if(onlySufficientlyVisible && isViewSufficientlyShown(child))
					views.add(child);
 
				else if(!onlySufficientlyVisible)
					views.add(child);
 
				if (child instanceof ViewGroup) {
					addChildren(views, (ViewGroup) child, onlySufficientlyVisible);
				} 
			} 
		} 
	} 
 
	/** 
	 * Returns true if the view is sufficiently shown 
	 * 
	 * @param view the view to check 
	 * @return true if the view is sufficiently shown 
	 */ 
 
	public final boolean isViewSufficientlyShown(View view){
		final int[] xyView = new int[2];
		final int[] xyParent = new int[2];
 
		if(view == null)
			return false; 
 
		final float viewHeight = view.getHeight();
		final View parent = getScrollOrListParent(view);
		view.getLocationOnScreen(xyView);
 
		if(parent == null){
			xyParent[1] = 0;
		} 
		else{ 
			parent.getLocationOnScreen(xyParent);
		} 
 
		if(xyView[1] + (viewHeight/2.0f) > getScrollListWindowHeight(view))
			return false; 
 
		else if(xyView[1] + (viewHeight/2.0f) < xyParent[1])
			return false; 
 
		return true; 
	} 
 
	/** 
	 * Returns the height of the scroll or list view parent 
	 * @param view the view who's parents height should be returned 
	 * @return the height of the scroll or list view parent 
	 */ 
 
	@SuppressWarnings("deprecation") 
	public float getScrollListWindowHeight(View view) {
		final int[] xyParent = new int[2];
		View parent = getScrollOrListParent(view);
		final float windowHeight;
		if(parent == null){
			windowHeight = activityUtils.getCurrentActivity(false).getWindowManager()
			.getDefaultDisplay().getHeight(); 
		} 
		else{ 
			parent.getLocationOnScreen(xyParent);
			windowHeight = xyParent[1] + parent.getHeight();
		} 
		parent = null;
		return windowHeight;
	}