Can a Layout be used in 2 activities?

4

Good afternoon.

I have an app that consumes webservices (that part is already finished), but now I require that this app works without internet access, I have created my bd and I already have saved data.

My question is if I can use a Layout for 2 different activities: one for when I'm connected to wifi and the other activity when I'm not using wifi and work locally.

    
asked by Hugo Rodriguez 13.07.2016 в 00:51
source

2 answers

5

Of course you can Hugo, the way to load the layout in your Activity as you know is through the method setContentView () to inflate the layout:

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);

    }   

You can reuse the same layout in several Activities, even validate in the same Activity to load one or the other layout.

    protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                if(existeWIFI){
                    setContentView(R.layout.my_layout);
                }else{
                  setContentView(R.layout.my_layout_nowifi);
                }

     }  
    
answered by 13.07.2016 / 10:43
source
2

You can use the SetConentView() method, I'll give you an example:

public class PrincipalActivity extends Activity {

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

The line setContentView(R.layout.main); indicates that there is a static class called R.layout , and that within that class there is an integer constant called main , which points to a view defined by a resource file layout XML

    
answered by 26.10.2016 в 13:56