How to use setContentView (int) from a class that does not extend Activity

I need to call setContentView (int) from my main Activity from another class that does not extend the Activity.

In my custom class, I have private Context context;var, which is passed from Activity in the constructor, but I cannot figure out how to access the Activity methods using a variable context.

+5
source share
3 answers

If your context is an instance of the Activity class, a simple class should execute:

Activity a = (Activity) context;
a.setContentView(R.layout.your_layout);
+10
source

( , ) , .

+1

You will need to pass a link to the action you are using.

Something like that

class ActivityA extends Activity{
   @Override
   public void onCreate(Bundle state){
      super.onCreate(state);
      ClassA myclass = new ClassA(this);
   }
}

And then class A will have:

class ClassA {
   public ClassA(Activity yourActivity){
      ... Get your view here ....
      yourActivity.setContentView(view);
      ... do more things...
   }
}
0
source

All Articles