aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/ui/listeners/AvatarLogoPositioningListener.java
blob: e6846d9b2a05935fd98389b4af19af29cb2c2f31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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);
        }
    }
}