mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Add layout util file to organize layout utilities
This commit is contained in:
parent
f65ca0387a
commit
5c57939245
1 changed files with 52 additions and 0 deletions
52
app/src/main/java/fr/free/nrw/commons/utils/LayoutUtils.java
Normal file
52
app/src/main/java/fr/free/nrw/commons/utils/LayoutUtils.java
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package fr.free.nrw.commons.utils;
|
||||
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewTreeObserver;
|
||||
|
||||
public class LayoutUtils {
|
||||
|
||||
/**
|
||||
* Can be used for keeping aspect radios suggested by material guidelines. See:
|
||||
* https://material.io/design/layout/spacing-methods.html#containers-aspect-ratios
|
||||
* In some cases we don't know exact width, for such cases this method measures
|
||||
* width and sets height by multiplying the width with height.
|
||||
* @param rate Aspect ratios, ie 1 for 1:1. (width * rate = height)
|
||||
* @param view view to change height
|
||||
*/
|
||||
public static void setLayoutHeightAllignedToWidth(double rate, View view) {
|
||||
ViewTreeObserver vto = view.getViewTreeObserver();
|
||||
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
||||
@Override
|
||||
public void onGlobalLayout() {
|
||||
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
|
||||
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
|
||||
Log.d("deneme7","heig:"+layoutParams.height+"weig:"+view.getWidth() * rate);
|
||||
layoutParams.height = (int) (view.getWidth() * rate);
|
||||
view.setLayoutParams(layoutParams);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Can be used for keeping aspect radios suggested by material guidelines. See:
|
||||
* https://material.io/design/layout/spacing-methods.html#containers-aspect-ratios
|
||||
* In some cases we don't know exact height, for such cases this method measures
|
||||
* height and sets width by multiplying the width with height.
|
||||
* @param rate Aspect ratios, ie 1 for 1:1. (height * rate = width)
|
||||
* @param view view to change width
|
||||
*/
|
||||
public static void setLayoutWidthAllignedToHeight(double rate, View view) {
|
||||
ViewTreeObserver vto = view.getViewTreeObserver();
|
||||
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
||||
@Override
|
||||
public void onGlobalLayout() {
|
||||
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
|
||||
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
|
||||
layoutParams.width = (int) (view.getHeight() * rate);
|
||||
view.setLayoutParams(layoutParams);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue