Android: adding new view with XML layout file

I am starting to develop applications for Android. I need to know how to link the xml layout file to the new class (and not the activity). For example, a class should have a table with an image and some texts.

thank!

+3
source share
3 answers

Got a response:

LayoutInflater inflater = LayoutInflater.from(context);
         View v = inflater.inflate(R.layout.yourxmllayout, null, false);
+3
source

You can also write this as:

LayoutInflater inflator = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflator.inflate(R.layout.yourxmllayout, null);

If you are in action, there is no need to use context.

0
source

Source: https://habr.com/ru/post/1765176/


All Articles