Context Class, android.context.Context is an most important class in android SDK which has ancestors of classes like Activity,Service,BackupAgent and Application.
Image Source: Stackoverflow.com
Context class is an abstract type class which is used to get the global information about the application. Its implementation is provided by the Android OS. It is used to get the application_specific resource and classes, for example, when we launch an activity, Broadcast and receive Intent Etc.
Context is used to get the current state of the application. Its Use is common in android development, for example, whenever we are creating a Dynamic view, Open Another Activity, Toasting a Message and broadcasting then we uses the context.
It allows to access the following resources:-
Here is some method listed below by which you can get context:-
- this (when in the activity class).
Creating New Object:-
Whenever we are creating the View at run time in android then it needs the context.
In this example, We are creating a textview at runtime, TextView constructor need the context, so we are passing the context to it.
TextView tv=new TextView(getContext());// getContext() is used to pass the context.
tv.setText("Hello Word");// here we are setting the text to TextView.