Back to project page android_device.
The source code is released under:
[Apache License](http://www.apache.org/licenses/): Version 2.0, January 2004 =============== ## TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION ## ### 1. Definitions. ### "License" sha...
If you think the Android project android_device listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * ================================================================================================= * Copyright (C) 2014 Martin Albedinsky [Wolf-ITechnologies] * ================================================================================================= * Licensed under the Apache License, Version 2.0 or later (further "License" only). * ------------------------------------------------------------------------------------------------- * You may use this file only in compliance with the License. More details and copy of this License * you may obtain at/* w w w. ja va2 s .c o m*/ * * http://www.apache.org/licenses/LICENSE-2.0 * * You can redistribute, modify or publish any part of the code written within this file but as it * is described in the License, the software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND. * * See the License for the specific language governing permissions and limitations under the License. * ================================================================================================= */ package com.wit.android.device.util; import android.app.Activity; import android.content.Context; import android.support.annotation.NonNull; import android.view.View; import android.view.inputmethod.InputMethodManager; /** * <h3>Class Overview</h3> * todo: description * * @author Martin Albedinsky */ public class ScreenUtils { /** * Interface =================================================================================== */ /** * Constants =================================================================================== */ /** * Log TAG. */ // private static final String TAG = "ScreenUtils"; /** * Flag indicating whether the output trough log-cat is enabled or not. */ // private static final boolean LOG_ENABLED = true; /** * Flag indicating whether the debug output trough log-cat is enabled or not. */ // private static final boolean DEBUG_ENABLED = true; /** * Methods ===================================================================================== */ /** * Hides soft keyboard from the current window. * <p> * <b>Note</b>, that this will not work when you want to hide soft keyboard while a dialog is * visible, use {@link #hideSoftKeyboard(android.view.View)} instead. * * @param activity Current activity context to obtain the current focused view. * @return {@code True} if hiding was successful, {@code false} if a window of the given * activity does not have focused view at this time. */ public static boolean hideSoftKeyboard(@NonNull Activity activity) { final View focusedView = activity.getWindow().getCurrentFocus(); return focusedView != null && hideSoftKeyboard(focusedView); } /** * Hides soft keyboard from the current window using token of the given focused view. * * @param focusedView The view which has focus at this time. * @return {@code True} if hiding was successful, {@code false} if the view does not * have focus at this time. */ public static boolean hideSoftKeyboard(@NonNull View focusedView) { if (focusedView.getContext() == null || !focusedView.hasFocus()) { return false; } // Get input method manager. final InputMethodManager imm = (InputMethodManager) focusedView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); // Now hide keyboard from window. return imm.hideSoftInputFromWindow(focusedView.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN); } /** * Shows soft keyboard on the current window. * <p> * <b>Note</b>, that this will not work when you want to show soft keyboard while a dialog is * visible, use {@link #showSoftKeyboard(android.view.View)} instead. * * @param activity Current activity context to obtain the current focused view. * @return {@code True} if showing was successful, {@code false} otherwise. */ public static boolean showSoftKeyboard(@NonNull Activity activity) { final View view = activity.getWindow().getCurrentFocus(); return view != null && showSoftKeyboard(view); } /** * Shows soft keyboard on the current window using token of the given focused view. * * @param focusedView The view which has focus at this time. If the view does not have focus, * focus for that view will be requested by {@link android.view.View#requestFocus()}. * @return {@code True} if showing was successful, {@code false} otherwise. */ public static boolean showSoftKeyboard(@NonNull View focusedView) { if (focusedView.getContext() == null) { return false; } if (focusedView.hasFocus() || focusedView.requestFocus()) { // Get input method manager. final InputMethodManager imm = (InputMethodManager) focusedView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); // Now hide Keyboard from window. return imm.showSoftInput(focusedView, InputMethodManager.RESULT_UNCHANGED_SHOWN); } return false; } }