Aditya
Aditya

Reputation: 425

What is meaning of "view.getWindowToken" in android?

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.

Upvotes: 4

Views: 2231

Answers (1)

Xid
Xid

Reputation: 4961

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.

Upvotes: 6

Related Questions