package de.thedevstack.conversationsplus.ui.listeners; import android.os.Build; import android.view.View; import android.view.ViewTreeObserver; import de.thedevstack.conversationsplus.R; /** * This listener aims to position the avatar logo. */ public class AvatarLogoPositioningListener implements ViewTreeObserver.OnGlobalLayoutListener { private View actionBarView; private View avatarLogoView; public AvatarLogoPositioningListener(View actionBarView, View avatarLogoView) { this.actionBarView = actionBarView; this.avatarLogoView = avatarLogoView; } @Override public void onGlobalLayout() { // Measure views int[] location = new int[2]; this.actionBarView.getLocationOnScreen(location); int[] logoLocation = new int[2]; this.avatarLogoView.getLocationOnScreen(logoLocation); int[] titleViewLocation = new int[2]; View titleView = this.actionBarView.findViewById(R.id.conversationsTitle); int oldTitleViewLeftPadding = 0; if (null != titleView) { // Remove the listener only if all necessary view elements are there if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { this.actionBarView.getViewTreeObserver().removeOnGlobalLayoutListener(this); } else { this.actionBarView.getViewTreeObserver().removeGlobalOnLayoutListener(this); } titleView.getLocationOnScreen(titleViewLocation); int avatarWidth = (int) (58 * titleView.getResources().getDisplayMetrics().density); int newTitleViewLeftPadding = titleViewLocation[0] + avatarWidth; titleView.setPadding(newTitleViewLeftPadding, titleView.getPaddingTop(), 0, 0); oldTitleViewLeftPadding = titleViewLocation[0]; } // Add top padding if necessary if (location[1] > logoLocation[1]) { int actionBarViewHeight = this.actionBarView.getMeasuredHeight(); int newAvatarLogoTopPadding = location[1]; // Move to the top padding of the action bar (below the notification bar) newAvatarLogoTopPadding += actionBarViewHeight; newAvatarLogoTopPadding -= 58 * avatarLogoView.getResources().getDisplayMetrics().density * 0.6; this.avatarLogoView.setPadding(oldTitleViewLeftPadding, newAvatarLogoTopPadding, 0, 0); } } }