In this video it shows how one can create multiple layouts in their Android App and switch between them easily by using setContentView command.
Further, it shows how one can create menu items for their Android App and switch between different layouts using the menu options selection. Exit option in the menu is shown which can be used to exit the App using the finish() API.
I hope you liked this video. For any questions, suggestions or appreciations, please reach out to us at:
[ Ссылка ] or write to us at: programmerworld1990@gmail.com
For complete source code of this project please refer to the below link:
[ Ссылка ]
The Java code of this App is available below:
package com.programmerworld.mymultiplelayoutsandmenu;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void homelayoutButton(View view){
setContentView(R.layout.secondlayout);
}
public void secondlayoutButton(View view){
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.mymenu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.home:
setContentView(R.layout.activity_main);
break;
case R.id.secondlayout:
setContentView(R.layout.secondlayout);
break;
case R.id.exit:
finish();
}
return super.onOptionsItemSelected(item);
}
}
/************/
Ещё видео!