Skip to content

Commit 5c57939

Browse files
committed
Add layout util file to organize layout utilities
1 parent f65ca03 commit 5c57939

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package fr.free.nrw.commons.utils;
2+
3+
import android.util.Log;
4+
import android.view.View;
5+
import android.view.ViewGroup;
6+
import android.view.ViewTreeObserver;
7+
8+
public class LayoutUtils {
9+
10+
/**
11+
* Can be used for keeping aspect radios suggested by material guidelines. See:
12+
* https://material.io/design/layout/spacing-methods.html#containers-aspect-ratios
13+
* In some cases we don't know exact width, for such cases this method measures
14+
* width and sets height by multiplying the width with height.
15+
* @param rate Aspect ratios, ie 1 for 1:1. (width * rate = height)
16+
* @param view view to change height
17+
*/
18+
public static void setLayoutHeightAllignedToWidth(double rate, View view) {
19+
ViewTreeObserver vto = view.getViewTreeObserver();
20+
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
21+
@Override
22+
public void onGlobalLayout() {
23+
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
24+
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
25+
Log.d("deneme7","heig:"+layoutParams.height+"weig:"+view.getWidth() * rate);
26+
layoutParams.height = (int) (view.getWidth() * rate);
27+
view.setLayoutParams(layoutParams);
28+
}
29+
});
30+
}
31+
32+
/**
33+
* Can be used for keeping aspect radios suggested by material guidelines. See:
34+
* https://material.io/design/layout/spacing-methods.html#containers-aspect-ratios
35+
* In some cases we don't know exact height, for such cases this method measures
36+
* height and sets width by multiplying the width with height.
37+
* @param rate Aspect ratios, ie 1 for 1:1. (height * rate = width)
38+
* @param view view to change width
39+
*/
40+
public static void setLayoutWidthAllignedToHeight(double rate, View view) {
41+
ViewTreeObserver vto = view.getViewTreeObserver();
42+
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
43+
@Override
44+
public void onGlobalLayout() {
45+
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
46+
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
47+
layoutParams.width = (int) (view.getHeight() * rate);
48+
view.setLayoutParams(layoutParams);
49+
}
50+
});
51+
}
52+
}

0 commit comments

Comments
 (0)