Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is a Context Class in Android Programming?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 44
    Comment on it

    Context Class,  android.context.Context is an most important class in android SDK which has ancestors of classes like Activity,Service,BackupAgent and Application.

     

    https://i.stack.imgur.com/mn21A.png

    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:-

    • getAssets()
    • getResources()
    • getPackageManager()
    • getString()
    • getSharedPrefsFile()

    Here is some method listed below by which you can get context:-

    • getApplicationContext()
    • getContext()
    • getBaseContext()
    • this (when in the activity class).

     

    Example:-

    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.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: