android-studio – Can a Layout be used in 2 activities?

Question:

Good afternoon.

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

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

Answer:

Of course you can Hugo, the way to load the layout in your Activity as you know is through the setContentView () method 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 another layout.

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

     }  
Scroll to Top