Declare View Elements In Another Class (context Not Clear)
I am creating a dynamic UI programatically and in main class I am creating UI element so that I can later add it to TableLayout view. Normally I declare it like this: TextView tw_1
Solution 1:
Basically you have to send context from your activity that you call new class. for this purpose you can use constructor to send context data to the new object of class. I have an example that show how to create a constrctor and use it. for example this is your ExampleClass:
publicclassExampleClass{
privatefinal Context context;
publicExampleClass(Context context){
this.context = context;
}
}
and in your Activity Class do this:
ExampleClassex1=newExampleClass(MainActivity.this);
and if you want use your class as static you must only define your class and context variable as static without constructor and set context equals your context. I hope this help you.
Post a Comment for "Declare View Elements In Another Class (context Not Clear)"