1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.latin.utils; 18 19 import android.view.View; 20 import android.view.ViewGroup; 21 import android.view.ViewGroup.MarginLayoutParams; 22 import android.view.Window; 23 import android.view.WindowManager; 24 import android.widget.FrameLayout; 25 import android.widget.LinearLayout; 26 import android.widget.RelativeLayout; 27 28 public final class ViewLayoutUtils { 29 private ViewLayoutUtils() { 30 // This utility class is not publicly instantiable. 31 } 32 33 public static MarginLayoutParams newLayoutParam(final ViewGroup placer, final int width, 34 final int height) { 35 if (placer instanceof FrameLayout) { 36 return new FrameLayout.LayoutParams(width, height); 37 } else if (placer instanceof RelativeLayout) { 38 return new RelativeLayout.LayoutParams(width, height); 39 } else if (placer == null) { 40 throw new NullPointerException("placer is null"); 41 } else { 42 throw new IllegalArgumentException("placer is neither FrameLayout nor RelativeLayout: " 43 + placer.getClass().getName()); 44 } 45 } 46 47 public static void placeViewAt(final View view, final int x, final int y, final int w, 48 final int h) { 49 final ViewGroup.LayoutParams lp = view.getLayoutParams(); 50 if (lp instanceof MarginLayoutParams) { 51 final MarginLayoutParams marginLayoutParams = (MarginLayoutParams)lp; 52 marginLayoutParams.width = w; 53 marginLayoutParams.height = h; 54 marginLayoutParams.setMargins(x, y, 0, 0); 55 } 56 } 57 58 public static void updateLayoutHeightOf(final Window window, final int layoutHeight) { 59 final WindowManager.LayoutParams params = window.getAttributes(); 60 if (params.height != layoutHeight) { 61 params.height = layoutHeight; 62 window.setAttributes(params); 63 } 64 } 65 66 public static void updateLayoutHeightOf(final View view, final int layoutHeight) { 67 final ViewGroup.LayoutParams params = view.getLayoutParams(); 68 if (params.height != layoutHeight) { 69 params.height = layoutHeight; 70 view.setLayoutParams(params); 71 } 72 } 73 74 public static void updateLayoutGravityOf(final View view, final int layoutGravity) { 75 final ViewGroup.LayoutParams lp = view.getLayoutParams(); 76 if (lp instanceof LinearLayout.LayoutParams) { 77 final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)lp; 78 if (params.gravity != layoutGravity) { 79 params.gravity = layoutGravity; 80 view.setLayoutParams(params); 81 } 82 } else if (lp instanceof FrameLayout.LayoutParams) { 83 final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)lp; 84 if (params.gravity != layoutGravity) { 85 params.gravity = layoutGravity; 86 view.setLayoutParams(params); 87 } 88 } else { 89 throw new IllegalArgumentException("Layout parameter doesn't have gravity: " 90 + lp.getClass().getName()); 91 } 92 } 93 } 94