medo medo - 4 months ago 11
Android Question

Use Activity View in another class

This is my activity

public class Conversation extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversation);
}
}


as we see it use this layout
activity_conversation


How can i send this layout or
View
to class for programming it like this

myclass m = new myclass(View);


Why i want to do this because i have another class whose layout is same as
activity_conversation
. So i want to re-use it like as:
myclass m = new mycalss(inflate layout);


How can i get view and pass to another class to inflate it.
and is it a right approach to re-use the code?

Answer

how can i send this layout or View to class for programming it like this

Do it using getDecorView() after calling setContentView :

View content_layout = (View)findViewById(android.R.id.content);
myclass m  = new myclass(content_layout);

OR

ViewGroup view=(ViewGroup)getWindow().getDecorView();
LinearLayout content_layout = (LinearLayout)view.getChildAt(0);
myclass m  = new myclass(content_layout);