9

I'm very new to Android development and I'm trying to create a view, which can be easily done by alloc then initWithFrame ... in Obj-C with Cocoa Touch, but in Java it uses the new ..() method and I'm stuck with defining the variable context, the parameter for LinearLayout().

I see some people use this as argument, namely new LinearLayout(this), but I don't understand what this argument actually does and I would appreciate if someone can give me a little bit guidance regarding what to put into as the argument.

LinearLayout layout = new LinearLayout(context);

What should context be? How should I define it? What does it do? What value should I assign it to?

  • http://stackoverflow.com/a/9227958/726863 – Lalit Poptani May 07 '13 at 07:36
  • 1
    http://developer.android.com/reference/android/content/Context.html The Context is basically your window into the environment. A little bit like `System` or `Runtime` but not static. Note that `Activity` is an instance of `Context` and any `View` has a `.getContext()` method meaning you should rarely be in a situation where you can't access a context. – Philip Couling May 07 '13 at 07:42
  • @couling yeah and I just go through [this](http://stackoverflow.com/questions/3572463/what-is-context-in-android) and [this](http://stackoverflow.com/questions/3918083/what-exactly-is-a-context-in-java) and I think I know what it is now. Thanks! – Help - I need somebody's help May 07 '13 at 07:45
  • 1
    [this question might be useful in your question][1] [1]: http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context/7298955#7298955 – AMD May 07 '13 at 07:46
  • @AMD Yeah thanks! (just gained a deeper insight into Android from that link. ) – Help - I need somebody's help May 07 '13 at 07:47
  • Read [the documentation](http://developer.android.com/intl/ru/reference/android/content/Context.html). And read [this answer][1]. [1]: http://stackoverflow.com/questions/3572463/what-is-context-in-android – Vsevolod May 07 '13 at 07:47

2 Answers2

8

Simple way is

just declare variable as below,

private Context context;

and onCreate() method, assign its value as below,

public void onCreate(Bundle savedInstanceState) 
{
     super.onCreate(savedInstanceState);
     context = this;
     ...
}

You can also assign in another way as follows,

context = getApplicationContext();

You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in the activity class).

Lucifer
  • 29,392
  • 25
  • 90
  • 143
0

Context is an abstract class whose implementation is given by Android system. It helps in using the application resources, launching activities, broadcast and many more.

It tells the compiler to which context activity or application your current belongs which you want to show. Mostly we give the context of activity while initializing the view.

LinearLayout layout = new LinearLayout(ActivityName.this);

or you can initialize a variable in that start of the activity like this

private Context context;

//in activity class 
context=ActivityName.this;
mmBs
  • 8,421
  • 6
  • 38
  • 46