4

I was trying to hide keyboard on click of a button.

Then I came across this code:

public static void hideKeyboardFrom(Context context, View view) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

My question is what is view.getWindowToken() and what is the significance of it and what does it return.

Aditya
  • 325
  • 6
  • 19

1 Answers1

6

So basically all Graphical User Interface is rendered on a Window. One example is that of an Activity, from the official documentation:

Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View)

Another example would be that of an AlertDialog.

A screen in Android may have multiple windows. For example, you can use two activities in the multi-window Android feature. Sometimes a window can contain other windows.

Here is a question with some good answers regarding what a Window is.

To answer your question, each Window has a unique identifier. This identifier is called a Window Token. So, the significance of a "WindowToken" is to uniquely identify a Window. There is another method, namely getApplicationWindowToken(), which can be used to get the identifier for the top-most Window.

Xid
  • 4,578
  • 2
  • 13
  • 35
  • Then what is the difference between window and fragment? – Aditya Oct 20 '20 at 03:49
  • I recommend you through the [official guide](https://developer.android.com/guide) – Xid Oct 20 '20 at 03:56
  • Ok, then what is the difference between window and activity – Aditya Oct 20 '20 at 04:01
  • No that doesn't tell the difference. Could you please tell here? – Aditya Oct 20 '20 at 15:28
  • 1
    The system uses a window to draw the status bar on, to show dialogs, to provide soft navigation keys, etc. In your application, an Activity provides this window on which UI components are shown. – Xid Oct 20 '20 at 15:55