Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 588
sergi Avatar asked Jul 15 '11 16:07

sergi


2 Answers

If your context is an instance of Activity class, simple class cast should work:

Activity a = (Activity) context;
a.setContentView(R.layout.your_layout);
like image 188
Konstantin Burov Avatar answered Nov 09 '22 20:11

Konstantin Burov


One solution (may not be the most elegant) is to pass the calling activity to the other class, not just the context.

like image 1
Ashterothi Avatar answered Nov 09 '22 22:11

Ashterothi