Can you inflate a class?

Asked

Viewed 167 times

2

There are many methods that you inflate a layout.xml but I need to inflate a class within a tab, anyone know what I can do? I’m suffocating in here. To inflate layout.xml use this code:

if(this.getTag() == "formulario3"){
   return inflater.inflate(R.layout.formulario3, container, false);
}

But I created a dynamic form only for Java code and I’m not able to inflate.

1 answer

0

daniel12345smith, assuming the class stores objects/properties of the type View. So instead of "inflate" (which is an exclusive option to work with the xml), you can call methods of this class that you insert into Context desired these views related to this class.

Example

PrincipalActivity.java

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

    Contatos.carregaContatos(PrincipalActivity.this);
}

Contatos.java

public class Contatos
{

    public static void carregaContatos(Activity activity)
    {
        // declara TextView
        // note que "activity" agora pode procurar uma view da activity PrincipalActivity.java 
        TextView textView = new TextView(); // ou "this.minhaTextView", whatever

        // codigo que insere uma View guardada e gerida por esta classe "Contatos"
        // note que "activity" agora pode procurar uma view da activity PrincipalActivity.java 
        LinearLayout linearLayout = activity.findViewById(R.id.info); 
        linearLayout.addView(textView);

    }

}

And so on and so forth. The idea is to keep the "logica" (Domain Logic and business Logic) separate (unfortunately Android follows a View-Controller standard) and we have to manage as we can.

  • vlw but I have nothing xml in my project, that’s the problem.

  • Oops, so, all right! This is the idea. Create Views as OBJECTS (by code) in your classes and then add in the Activity you want, all by code. Note that TextView textView = new TextView(); of my example above does not exist in XML any...and it was added to the container Relativelayout by code, in the line below. But for this, Activityprincipal had to call the method Contatos.carregaContatos(PrincipalActivity.this); and pass your Activity as a parameter. It was just to illustrate that your Idea in the question can follow this flow. By the way, I do everything like this hehe.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.