package com.gojira.util; import android.content.Context; import android.os.Build; import android.support.annotation.NonNull; import android.view.View; import android.view.ViewTreeObserver; import android.view.inputmethod.InputMethodManager; /** * @author Stratos Theodorou * @version 1.0 * @since 15/05/2015 */ public class ViewUtils { public static void addOnGlobalLayoutListener(final View view, final Runnable runnable) { ViewTreeObserver vto = view.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @SuppressWarnings("deprecation") @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { view.getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { view.getViewTreeObserver().removeOnGlobalLayoutListener(this); } runnable.run(); } }); } public static void hideKeyboard(@NonNull View view) { InputMethodManager imm = (InputMethodManager) view.getContext() .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } }