I created a new class in android that creates LinearLayouts when instantiated. However I can’t figure out the context to put in the brackets of: new LinearLayout(context). Can someone shed some light? (I’ve already tried reading everything i can on contexts)
I’m assuming I don’t need to extend Activity in my class
public class NewLayouts {
...
newParentLayout = new LinearLayout(getApplicationContext()); //<--eclipse warns of error here saying not a valid context
newParentLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
newParentLayout.setOrientation(LinearLayout.VERTICAL);
TextView monthDisplay = new TextView(getApplicationContext()); //<--eclipse warns of error here saying not a valid context
...
}
My main activity:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NewLayouts Sample = new NewLayouts(1,2); //variables required in my constructor for new Layouts
setContentView(Sample.newParentLayout);
}
Change the constructor of
NewLayoutsto be something like……then use
ctxas theContextinNewLayoutsfor creating theViews.In the
Activitythen do the following…That will pass the
Activity'sownContextintoNewLayoutsconstructor.