summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/base/utils/ViewHelper.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/base/utils/ViewHelper.java')
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/base/utils/ViewHelper.java71
1 files changed, 68 insertions, 3 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/base/utils/ViewHelper.java b/app/src/main/java/se/leap/bitmaskclient/base/utils/ViewHelper.java
index 51bcb2b1..ebf26048 100644
--- a/app/src/main/java/se/leap/bitmaskclient/base/utils/ViewHelper.java
+++ b/app/src/main/java/se/leap/bitmaskclient/base/utils/ViewHelper.java
@@ -1,11 +1,14 @@
package se.leap.bitmaskclient.base.utils;
+import static android.view.View.GONE;
+import static android.view.View.VISIBLE;
+
+import android.animation.Animator;
+import android.animation.ValueAnimator;
import android.app.Activity;
-import android.app.Notification;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
-import android.os.Build;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
@@ -13,13 +16,14 @@ import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
+import android.view.inputmethod.InputMethodManager;
import androidx.annotation.ColorRes;
import androidx.annotation.DimenRes;
+import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
-import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
@@ -146,4 +150,65 @@ public class ViewHelper {
bar.setTitle(spannableTitle);
}
+ public interface AnimationInterface {
+ void onAnimationEnd();
+ }
+
+ public static void animateContainerVisibility(View container, boolean isExpanded) {
+ animateContainerVisibility(container, isExpanded, null);
+ }
+
+ public static void animateContainerVisibility(View container, boolean isExpanded, AnimationInterface animationInterface) {
+
+ int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
+ int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
+
+ container.measure(widthMeasureSpec, heightMeasureSpec);
+ int measuredHeight = container.getMeasuredHeight();
+
+ int targetHeight = isExpanded ? 0 : measuredHeight; // Get the actual content height of the view
+ int initialHeight = isExpanded ? measuredHeight : 0;
+
+ ValueAnimator animator = ValueAnimator.ofInt(initialHeight, targetHeight);
+ animator.setDuration(250); // Set the duration of the animation in milliseconds
+
+ animator.addUpdateListener(animation -> {
+ container.getLayoutParams().height = (int) animation.getAnimatedValue();
+ container.requestLayout();
+ });
+ animator.addListener(new Animator.AnimatorListener() {
+ @Override
+ public void onAnimationStart(@NonNull Animator animation) {
+ if (initialHeight == 0 && container.getVisibility() == GONE) {
+ container.setVisibility(VISIBLE);
+ }
+ }
+
+ @Override
+ public void onAnimationEnd(@NonNull Animator animation) {
+ if (targetHeight == 0) {
+ container.setVisibility(GONE);
+ }
+ if (animationInterface != null) {
+ animationInterface.onAnimationEnd();
+ }
+ }
+
+ @Override
+ public void onAnimationCancel(@NonNull Animator animation) {
+ container.setVisibility(targetHeight == 0 ? GONE : VISIBLE);
+ }
+
+ @Override
+ public void onAnimationRepeat(@NonNull Animator animation) {}
+ });
+
+ animator.start();
+ }
+
+ public static void hideKeyboardFrom(Context context, View view) {
+ InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
+ imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
+ }
+
}