Reputation: 979
I need to call the setContentView(int) from my main Activity from another class which does not extends Activity.
In my custom class I've got the private Context context;
var that is passed from the Activity in the Constructor but I can't figure out how to acces the Activity methods using the context
variable.
Upvotes: 6
Views: 16364
Reputation: 36035
You would have to pass in a reference to the Activity you're using.
Something like this
class ActivityA extends Activity{
@Override
public void onCreate(Bundle state){
super.onCreate(state);
ClassA myclass = new ClassA(this);
}
}
And then Class A would have:
class ClassA {
public ClassA(Activity yourActivity){
... Get your view here ....
yourActivity.setContentView(view);
... do more things...
}
}
Upvotes: 0
Reputation: 69228
If your context is an instance of Activity class, simple class cast should work:
Activity a = (Activity) context;
a.setContentView(R.layout.your_layout);
Upvotes: 13
Reputation: 3282
One solution (may not be the most elegant) is to pass the calling activity to the other class, not just the context.
Upvotes: 1