|
3 | 3 | import android.app.Activity; |
4 | 4 | import android.content.Context; |
5 | 5 | import android.content.res.Configuration; |
| 6 | +import android.content.res.Resources; |
6 | 7 | import android.graphics.Point; |
| 8 | +import android.os.Build; |
7 | 9 | import android.util.TypedValue; |
8 | 10 | import android.view.Display; |
9 | 11 | import android.view.View; |
| 12 | +import android.view.WindowInsets; |
10 | 13 | import android.view.WindowManager; |
11 | 14 | import android.view.inputmethod.InputMethodManager; |
| 15 | +import android.widget.RelativeLayout; |
| 16 | +import com.google.android.material.floatingactionbutton.FloatingActionButton; |
12 | 17 |
|
13 | 18 | import androidx.annotation.NonNull; |
14 | 19 | import androidx.annotation.Nullable; |
@@ -149,4 +154,39 @@ public static void showKeyboard(@Nullable final View view) { |
149 | 154 | inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); |
150 | 155 | } |
151 | 156 | } |
| 157 | + |
| 158 | + /** |
| 159 | + * Apply window insets for a {@link FloatingActionButton} in a {@link RelativeLayout} to the given {@link View}. |
| 160 | + * |
| 161 | + * @param insets {@link WindowInsets} to apply to {@link View}. |
| 162 | + * @param resources {@link Resources} to get dimension value from. |
| 163 | + * @param view {@link View} to apply {@link WindowInsets} to. |
| 164 | + * |
| 165 | + * @return {@link WindowInsets} supplied from a listener. |
| 166 | + */ |
| 167 | + public static WindowInsets applyWindowInsetsForFloatingActionButton(WindowInsets insets, Resources resources, View view) { |
| 168 | + int bottom; |
| 169 | + |
| 170 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { |
| 171 | + bottom = insets.getInsets(WindowInsets.Type.systemBars()).bottom; |
| 172 | + } else { |
| 173 | + bottom = insets.getSystemWindowInsetBottom(); |
| 174 | + } |
| 175 | + |
| 176 | + int button = (int) resources.getDimension(R.dimen.button_floating); |
| 177 | + int margin = (int) resources.getDimension(R.dimen.margin_default); |
| 178 | + |
| 179 | + RelativeLayout.LayoutParams buttonLayoutParams = new RelativeLayout.LayoutParams( |
| 180 | + RelativeLayout.LayoutParams.WRAP_CONTENT, |
| 181 | + RelativeLayout.LayoutParams.WRAP_CONTENT |
| 182 | + ); |
| 183 | + buttonLayoutParams.setMargins(margin, 0, margin, bottom + margin); |
| 184 | + buttonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); |
| 185 | + buttonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_END); |
| 186 | + buttonLayoutParams.height = button; |
| 187 | + buttonLayoutParams.width = button; |
| 188 | + view.setLayoutParams(buttonLayoutParams); |
| 189 | + |
| 190 | + return insets; |
| 191 | + } |
152 | 192 | } |
0 commit comments