I made an Android Code Template to Accelerate Your Android Development
Have you ever thought of making a coding template for yourself to accelerate your android development? As a Computer Science student studying in Universiti Malaya. I have an exam of a course called “WIA2007: Mobile Application Development” (MAD). This course requires us to code an android app within 3 hours. I find that most of the code used in Android development is actually redundant and reusable. Hence I came up with an idea of doing a template myself.
According to my course, I have learnt the concept of navigation, fragment, activity, database, storage and so on. So I am going to break down my template into parts and explain what they does. Before I begin I just want to appreciate if you are able to support me on https://ko-fi.com/szeyusim where this is booster to my motivation to create more wonderful product in the future!
This blog has been divided into several section:
Java Template
- Activity
- Data (DAO, Database, Repository)
- Model
- UI (Adapter, Components, Fragment, Navigation, ViewHolder, ViewModel)
- Util (Storage, Navigation)
XML Template
- Login Layout
- Bottom Navigation Layout
- Side Navigation Layout
- Card Layout
- Button Layout
- Toolbar Layout
- Navigator Layout
- Edit Model Layout
All the code can be found from my GitHub as shown below, and all the setup step can be found from my README.md in my GitHub
Activity
Starting from activity, I have create a code template file called CustomNavigatorMainActivity.java. It is a custom main activity java template code for navigation-based android applications. In the code, I have set up navigation component including side navigation drawer, bottom navigation, toolbar as well as overflow menu. Hence saving time for me to code a navigation based app.
Below are the code template for CustomNavigatorMainActivity.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* Custom main activity template for navigation-based Android applications.
* Features:
* - Navigation component setup
* - Drawer and Bottom Navigation integration
* - Toolbar with navigation handling
* - Overflow menu for additional options
*
* Usage:
* 1. Replace `${PACKAGE_NAME}` with your actual package name
* 2. Ensure `nav_graph.xml` and required menu files exist
* 3. Add dependencies for Navigation Component and Material Design
*/
package ${PACKAGE_NAME};
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.appcompat.widget.Toolbar;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;
import com.google.android.material.navigation.NavigationView;
public class CustomNavigatorMainActivity extends AppCompatActivity {
private NavController navController;
private ActionBarDrawerToggle drawerToggle;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.custom_navigator_main_activity);
// Setup custom toolbar
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Initialize DrawerLayout
drawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.side_nav_view);
// Configure drawer toggle
drawerToggle = new ActionBarDrawerToggle(
this, drawerLayout, toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
);
drawerLayout.addDrawerListener(drawerToggle);
drawerToggle.syncState();
// Setup Navigation Component
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
.findFragmentById(R.id.nav_host_fragment);
if (navHostFragment != null) {
navController = navHostFragment.getNavController();
} else {
throw new IllegalStateException("NavHostFragment not found");
}
// Configure Navigation UI
NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_nav_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
// Handle settings navigation
// Option 1: Navigate using NavController
// NavigationUtil.navigateTo(activity, R.id.settingsFragment);
// Option 2: Navigate using Intent
// NavigationUtil.navigateTo(activity, SettingsActivity.class);
return true;
} else if (id == R.id.action_about) {
// Handle the "About" action (e.g., show a dialog)
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onSupportNavigateUp() {
return NavigationUI.navigateUp(navController, drawerLayout) || super.onSupportNavigateUp();
}
}Data
Although the abstraction of room database implementation really hide a lot of details to implement the database with SQLite, room database is actually one of the most redundant code that I will ever come across with. I think since that the code is so redundant to code every time, so making it into a code template is a good choice!
DAO
Data Access Object (DAO) interface is used to code CRUD operations and include SQL Queries. So I have implement a template for it called CustomDAO.java
Code template for CustomDAO.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* Room Database DAO (Data Access Object) interface template.
* Features:
* - Basic CRUD operations
* - LiveData support for reactive programming
* - Customizable query methods
* - Asynchronous database operations
*
* Usage:
* 1. Create your entity class
* 2. Extend this DAO interface
* 3. Add custom queries as needed
*/
package ${PACKAGE_NAME};
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;
import java.util.List;
// Replace ${PACKAGE_NAME} with your package name
// Replace ${DAO_NAME} with your DAO interface name
// Replace ${MODEL_NAME} with your data model class name
// Replace ${TABLE_NAME} with your database table name
@Dao
public interface ${DAO_NAME} {
@Insert
void insert(${MODEL_NAME} item);
@Update
void update(${MODEL_NAME} item);
@Delete
void delete(${MODEL_NAME} item);
@Query("SELECT * FROM ${TABLE_NAME}")
LiveData<List<${MODEL_NAME}>> getAllItems();
@Query("SELECT * FROM ${TABLE_NAME} WHERE id = :itemId LIMIT 1")
LiveData<${MODEL_NAME}> getItemById(int itemId);
}Database
This database code is mostly fixed and use the same code every time. And I call it CustomDatabase.java
Code template for CustomDatabase.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* Room Database singleton template for Android applications.
* Features:
* - Thread-safe singleton implementation
* - Database instance management
* - Migration handling
* - Fallback to destructive migration
*
* Usage:
* 1. Define your database entities
* 2. Add your DAOs
* 3. Access using getInstance() method
*/
package ${PACKAGE_NAME};
import android.content.Context;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
// Replace ${PACKAGE_NAME} with your package name
// Replace ${DATABASE_NAME} with your database class name
// Replace ${MODEL_NAME} with your data model class name
// Replace ${DAO_NAME} with your DAO interface name
@Database(entities = {${MODEL_NAME}.class}, version = 1, exportSchema = false)
public abstract class ${DATABASE_NAME} extends RoomDatabase {
private static volatile ${DATABASE_NAME} INSTANCE;
public abstract ${DAO_NAME} ${DAO_INSTANCE_NAME}();
public static ${DATABASE_NAME} getDatabase(final Context context) {
if (INSTANCE == null) {
synchronized (${DATABASE_NAME}.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
${DATABASE_NAME}.class, "${DATABASE_FILE_NAME}")
.fallbackToDestructiveMigration()
.build();
}
}
}
return INSTANCE;
}
}Repository
This repository code are the code that use DAO to interact with RoomDatabase and I call it CustomDatabase.java
Code template for CustomRepository.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* Repository pattern implementation for data operations.
* Features:
* - Single source of truth principle
* - Async operation handling
* - LiveData integration
* - Clean architecture compliance
*
* Usage:
* 1. Initialize with your DAO
* 2. Implement data operations
* 3. Use with ViewModel
*/
package ${PACKAGE_NAME};
import android.app.Application;
import androidx.lifecycle.LiveData;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// Replace ${PACKAGE_NAME} with your package name
// Replace ${REPOSITORY_NAME} with your Repository class name
// Replace ${DAO_NAME} with your DAO interface name
// Replace ${MODEL_NAME} with your data model class name
// Replace ${DATABASE_NAME} with your Room database class name
public class ${REPOSITORY_NAME} {
private final ${DAO_NAME} dao;
private final LiveData<List<${MODEL_NAME}>> allItems;
private final ExecutorService executorService;
public ${REPOSITORY_NAME}(Application application) {
${DATABASE_NAME} db = ${DATABASE_NAME}.getDatabase(application);
dao = db.${DAO_INSTANCE_NAME}();
allItems = dao.getAllItems();
executorService = Executors.newFixedThreadPool(2);
}
public LiveData<List<${MODEL_NAME}>> getAllItems() {
return allItems;
}
public LiveData<List<${MODEL_NAME}>> getItemById(int itemId) {
return dao.getItemById(itemId);
}
public void insert(${MODEL_NAME} item) {
executorService.execute(() -> dao.insert(item));
}
public void update(${MODEL_NAME} item) {
executorService.execute(() -> dao.update(item));
}
public void delete(${MODEL_NAME} item) {
executorService.execute(() -> dao.delete(item));
}
}Model
This model represent a Room Database Entity Model which already have basic data structure and primary key configuration set up. User can edit the field that needed according to the need.
Code template for CustomModel.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* Room Entity model template for data structure.
* Features:
* - Room Entity annotations
* - Primary key configuration
* - Basic data structure setup
* - Common field implementations
*
* Usage:
* 1. Define your entity fields
* 2. Add necessary Room annotations
* 3. Implement getters and setters
*/
package ${PACKAGE_NAME};
import androidx.room.Entity;
import androidx.room.PrimaryKey;
// Replace ${PACKAGE_NAME} with your package name
// Replace ${MODEL_NAME} with your data model class name
@Entity(tableName = "${TABLE_NAME}")
public class ${MODEL_NAME} {
@PrimaryKey(autoGenerate = true)
private int id;
// Add your fields here
private String title;
private String content;
private String imagePath;
private String audioPath;
private long timestamp;
// Empty constructor
public ${MODEL_NAME}() {
}
// Getters and Setters
// For each field, create public getter and setter methods
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
// Repeat for other fields
}UI
These consist of all the code that user can use to interact with the components of the XML layout. I will help you to speed up your backend setup to link with your frontend development.
Adapter
This is a RecyclerView Adapter template which implement the ViewHolder pattern, click listener interface, data binding and list management methods.
Code template for CustomAdapter.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* CustomAdapter template.
*/
package ${PACKAGE_NAME};
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
// Replace ${PACKAGE_NAME} with your package name
// Replace ${ADAPTER_NAME} with your Adapter class name
// Replace ${VIEW_HOLDER_NAME} with your ViewHolder class name
public class ${ADAPTER_NAME} extends RecyclerView.Adapter<${ADAPTER_NAME}.${VIEW_HOLDER_NAME}> {
private List<${MODEL_NAME}> itemList;
private OnItemClickListener listener;
public interface OnItemClickListener {
void onItemClick(${MODEL_NAME} item);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
public ${ADAPTER_NAME}(List<${MODEL_NAME}> itemList) {
this.itemList = itemList;
}
@NonNull
@Override
public ${VIEW_HOLDER_NAME} onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.${ITEM_LAYOUT_NAME}, parent, false);
return new ${VIEW_HOLDER_NAME}(view);
}
@Override
public void onBindViewHolder(@NonNull ${VIEW_HOLDER_NAME} holder, int position) {
${MODEL_NAME} item = itemList.get(position);
holder.bind(item, listener);
}
@Override
public int getItemCount() {
return itemList != null ? itemList.size() : 0;
}
public void setItemList(List<${MODEL_NAME}> newList) {
this.itemList.clear();
this.itemList.addAll(newList);
notifyDataSetChanged();
}
public static class ${VIEW_HOLDER_NAME} extends RecyclerView.ViewHolder {
// Define your view components here
private final TextView textViewTitle;
private final TextView textViewSubtitle;
public ${VIEW_HOLDER_NAME}(View itemView) {
super(itemView);
// Initialize your view components
textViewTitle = itemView.findViewById(R.id.tv_card_title);
textViewSubtitle = itemView.findViewById(R.id.tv_card_subtitle);
}
public void bind(final ${MODEL_NAME} item, final OnItemClickListener listener) {
// Bind data to your views
// Example: textView.setText(item.getTitle());
textViewTitle.setText(item.getTitle());
textViewSubtitle.setText(item.getSubtitle());
itemView.setOnClickListener(v -> {
if (listener != null) {
listener.onItemClick(item);
}
});
}
}
}Components
These components files are just created for defining the logic of the component quicker. Feel free to contribute more components to my GitHub.
CustomInfoButton.java implement custom image button with Material Design integration and click handling. CustomFormControls.java consist of a collection of form control component logic including radio, checkbox, switch, autocomplete text view, spinner dropdown and button
Code template for CustomFormControls.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* Custom form controls activity implementing various interactive UI components.
* Features:
* - RadioGroup with multiple radio buttons
* - Checkbox group with multiple selections
* - Switch component for toggling options
* - AutoCompleteTextView with sample data
* - Spinner dropdown with predefined options
* - Button to submit selections
*
* Usage:
* 1. Inflate the layout `activity_custom_form_controls.xml`
* 2. Call `setupRadio()`, `setupCheckbox()`, etc., in `onCreate()`
* 3. Handle user interactions using event listeners
*/
package ${PACKAGE_NAME};
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import ${PACKAGE_NAME}.R;
import java.util.ArrayList;
import java.util.List;
public class CustomFormControls extends AppCompatActivity {
private RadioGroup radioGroup;
private CheckBox checkBox1, checkBox2, checkBox3;
private Switch toggleSwitch;
private AutoCompleteTextView autoCompleteTextView;
private Spinner spinner;
private Button submitButton;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_form_controls);
// Initialize form controls
setupRadio();
setupCheckbox();
setupSwitch();
setupAutoComplete();
setupSpinner();
setupButton();
}
private void setupRadio() {
radioGroup = findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
RadioButton selected = findViewById(checkedId);
Toast.makeText(this, "Selected: " + selected.getText(), Toast.LENGTH_SHORT).show();
});
}
private void setupCheckbox() {
checkBox1 = findViewById(R.id.checkbox1);
checkBox2 = findViewById(R.id.checkbox2);
checkBox3 = findViewById(R.id.checkbox3);
View.OnClickListener listener = v -> {
StringBuilder selected = new StringBuilder("Selected: ");
if (checkBox1.isChecked()) selected.append(checkBox1.getText()).append(" ");
if (checkBox2.isChecked()) selected.append(checkBox2.getText()).append(" ");
if (checkBox3.isChecked()) selected.append(checkBox3.getText()).append(" ");
Toast.makeText(this, selected.toString(), Toast.LENGTH_SHORT).show();
};
checkBox1.setOnClickListener(listener);
checkBox2.setOnClickListener(listener);
checkBox3.setOnClickListener(listener);
}
private void setupSwitch() {
toggleSwitch = findViewById(R.id.toggleSwitch);
toggleSwitch.setOnCheckedChangeListener((buttonView, isChecked) ->
Toast.makeText(this, "Switch is " + (isChecked ? "ON" : "OFF"), Toast.LENGTH_SHORT).show());
}
private void setupAutoComplete() {
autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
String[] suggestions = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, suggestions);
autoCompleteTextView.setAdapter(adapter);
}
private void setupSpinner() {
spinner = findViewById(R.id.spinner);
List<String> options = new ArrayList<>();
options.add("Option 1");
options.add("Option 2");
options.add("Option 3");
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, options);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(CustomFormControls.this, "Selected: " + options.get(position), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
}
private void setupButton() {
submitButton = findViewById(R.id.submitButton);
submitButton.setOnClickListener(v -> Toast.makeText(this, "Form Submitted!", Toast.LENGTH_SHORT).show());
}
}Code template for CustomInfoButton.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* Custom ImageButton implementation for information display.
* Features:
* - Material Design integration
* - Built-in click handling
* - Customizable appearance
* - Easy integration with activities and fragments
*
* Usage:
* 1. Add to your layout XML
* 2. Customize appearance through attributes
* 3. Set click listener for actions
*/
package ${PACKAGE_NAME};
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import androidx.appcompat.widget.AppCompatImageButton;
// Replace ${PACKAGE_NAME} with your package name
public class InfoButton extends AppCompatImageButton implements View.OnClickListener {
public InfoButton(Context context) {
super(context);
init();
}
public InfoButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public InfoButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
// Set up the button (e.g., set image resource)
setImageResource(R.drawable.baseline_info_24); // Replace with your actual drawable
setOnClickListener(this);
}
@Override
public void onClick(View v) {
// Handle the click event, e.g., show a dialog or toast
// You can also define an interface to communicate back to the activity
}
}Fragment
I have made 2 important and commonly used Fragment which are CustomListFragment.java and CustomEditModelFragment.java.
CustomListFragment is a fragment template that displays a list of items using RecycleView, ViewModel and NavigationUtil. It support different layout managers and item click navigation.
CustomEditFragment is a fragment template that allow for editing a specific model entity by ID. It integrates ViewModel for data binding and form validation, using NavigationUtil for navigation
Code template for CustomListFragment.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* CustomListFragment Template.
* Features:
* - Displays a list of ${MODEL_NAME} items using RecyclerView, a ViewModel, and NavigationUtil.
* - Offers options for different LayoutManagers.
* - Handles item clicks to navigate to another fragment.
*
* Usage:
* 1. Replace placeholders with your specific names:
* - ${PACKAGE_NAME}: Your package name (e.g., com.multimediajournalapp.peoplemory.ui.fragments)
* - ${FRAGMENT_NAME}: The name of this fragment (e.g., PeopleListFragment)
* - ${MODEL_NAME}: Your model class name (e.g., Person)
* - ${ADAPTER_NAME}: Your adapter class name (e.g., PersonAdapter)
* - ${VIEW_MODEL_CLASS}: Your ViewModel class name (e.g., PersonViewModel)
* - ${LAYOUT_RESOURCE}: The layout resource (e.g., R.layout.fragment_people_list)
* - ${RECYCLER_VIEW_ID}: The RecyclerView id (e.g., R.id.recycler_view_people)
* - ${NAVIGATION_ACTION_ID}: Navigation action id (e.g., R.id.action_peopleListFragment_to_editPersonFragment)
* - ${ITEM_ID_KEY}: The key for the item's id in the bundle (e.g., "personId")
* 2. Uncomment the LayoutManager option that you want.
*/
package ${PACKAGE_NAME};
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import ${PACKAGE_NAME}.R;
import ${PACKAGE_NAME}.model.${MODEL_NAME};
import ${PACKAGE_NAME}.ui.adapter.${ADAPTER_NAME};
import ${PACKAGE_NAME}.ui.navigation.NavigationUtil;
import ${PACKAGE_NAME}.ui.viewModel.${VIEW_MODEL_CLASS};
import java.util.List;
public class ${FRAGMENT_NAME} extends Fragment {
private ${VIEW_MODEL_CLASS} viewModel;
private ${ADAPTER_NAME} adapter;
public ${FRAGMENT_NAME}() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(${LAYOUT_RESOURCE}, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
if (view == null) {
throw new NullPointerException("Fragment view is null, check " + ${LAYOUT_RESOURCE} + " layout.");
}
RecyclerView recyclerView = view.findViewById(${RECYCLER_VIEW_ID});
if (recyclerView == null) {
throw new NullPointerException("RecyclerView with ID " + ${RECYCLER_VIEW_ID} + " not found in the inflated layout");
}
// Set LayoutManager (Required)
// Uncomment one of the options below:
// For a default vertical list:
// recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
// For a grid layout with 2 columns:
// recyclerView.setLayoutManager(new GridLayoutManager(requireContext(), 2));
// For a staggered grid layout with 2 columns:
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
// Initialize adapter, set item click listener, and assign adapter to RecyclerView
adapter = new ${ADAPTER_NAME}();
adapter.setOnItemClickListener(item -> {
// Create a bundle to pass the selected ${MODEL_NAME}'s ID
Bundle args = new Bundle();
args.putInt("${ITEM_ID_KEY}", item.getId());
NavigationUtil.navigateTo(${FRAGMENT_NAME}.this, ${NAVIGATION_ACTION_ID}, args);
});
recyclerView.setAdapter(adapter);
// Initialize ViewModel
viewModel = new ViewModelProvider(this).get(${VIEW_MODEL_CLASS}.class);
// Observe the LiveData and update RecyclerView when data changes
viewModel.getAllItems().observe(getViewLifecycleOwner(), (List<${MODEL_NAME}> ${MODEL_NAME_LOWER}) -> {
adapter.setItemList(${MODEL_NAME_LOWER});
});
}
}Code template for CustomEditModelFragment.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* A fully customizable fragment for editing a model.
*
* Features:
* - Supports dynamic model (`${MODEL_NAME}`)
* - Allows updating and deleting entities
* - Uses ViewModel for data binding
* - Supports NavigationUtil for smooth transitions
*
* Usage:
* 1. Replace `${MODEL_NAME}` with your model class (e.g., `Person`, `Task`)
* 2. Define necessary attributes (name, description, etc.)
* 3. Ensure the corresponding ViewModel (`${MODEL_NAME}ViewModel`) exists
*/
package ${PACKAGE_NAME}.ui.fragments;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import ${PACKAGE_NAME}.R;
import ${PACKAGE_NAME}.model.${MODEL_NAME};
import ${PACKAGE_NAME}.ui.navigation.NavigationUtil;
import ${PACKAGE_NAME}.ui.viewModel.${MODEL_NAME}ViewModel;
public class CustomEditModelFragment extends Fragment {
private ${MODEL_NAME}ViewModel viewModel;
private int modelId; // ID passed as an argument
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate your layout for editing a model
return inflater.inflate(R.layout.fragment_edit_${MODEL_NAME}.toLowerCase(), container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Retrieve modelId from arguments (use Safe Args if available)
if (getArguments() != null) {
modelId = getArguments().getInt("${MODEL_NAME}Id", -1);
}
// Initialize ViewModel
viewModel = new ViewModelProvider(this).get(${MODEL_NAME}ViewModel.class);
// Setup UI components
EditText etName = view.findViewById(R.id.et_edit_${MODEL_NAME.toLowerCase()}_name);
EditText etDescription = view.findViewById(R.id.et_edit_${MODEL_NAME.toLowerCase()}_description);
Button btnSave = view.findViewById(R.id.btn_save_changes);
Button btnDelete = view.findViewById(R.id.btn_delete_${MODEL_NAME.toLowerCase()});
// Fetch and display existing ${MODEL_NAME} data
viewModel.getItemById(modelId).observe(getViewLifecycleOwner(), model -> {
if (model != null) {
etName.setText(model.getName());
etDescription.setText(model.getDescription());
}
});
// Handle Save button click
btnSave.setOnClickListener(v -> {
String name = etName.getText().toString().trim();
String description = etDescription.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
Toast.makeText(getContext(), "Name cannot be empty", Toast.LENGTH_SHORT).show();
return;
}
// Update model object
${MODEL_NAME} updatedModel = new ${MODEL_NAME}(name, description);
updatedModel.setId(modelId);
viewModel.update(updatedModel);
Toast.makeText(getContext(), "${MODEL_NAME} updated", Toast.LENGTH_SHORT).show();
// Navigate back
NavigationUtil.navigateBack(this);
});
// Handle Delete button click
btnDelete.setOnClickListener(v -> {
// Delete model
${MODEL_NAME} modelToDelete = new ${MODEL_NAME}();
modelToDelete.setId(modelId);
viewModel.delete(modelToDelete);
Toast.makeText(getContext(), "${MODEL_NAME} deleted", Toast.LENGTH_SHORT).show();
// Navigate back
NavigationUtil.navigateBack(this);
});
}
}Navigation
I have implemented 2 main navigation part which will exist in mobile app which are the bottom navigation and side menu. Those template are designed with Mateiral Design and using NavigationUtil for seamless navigation.
Code template for BottomNavigation.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* Material Design bottom navigation implementation.
* Features:
* - Pre-configured navigation listener
* - Easy integration with fragments using NavigationUtil
* - Customizable menu items
* - Supports both fragment-based and intent-based navigation
*
* Usage:
* 1. Include bottom_nav_menu.xml in your resources
* 2. Initialize in your activity
* 3. Uncomment either fragment-based or intent-based navigation
*/
package ${PACKAGE_NAME}.ui.navigation;
import android.app.Activity;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import ${PACKAGE_NAME}.R;
public class BottomNavigation {
private final BottomNavigationView bottomNavigationView;
private final Activity activity;
public BottomNavigation(Activity activity) {
this.activity = activity;
this.bottomNavigationView = activity.findViewById(R.id.bottom_navigation);
setupBottomNav();
}
private void setupBottomNav() {
bottomNavigationView.setOnItemSelectedListener(item -> {
int id = item.getItemId();
if (id == R.id.nav_home) {
// Option 1: Navigate using NavController
// NavigationUtil.navigateTo(activity, R.id.homeFragment);
// Option 2: Navigate using Intent
// NavigationUtil.navigateTo(activity, HomeActivity.class);
return true;
} else if (id == R.id.nav_add) {
// Option 1: Navigate using NavController
// NavigationUtil.navigateTo(activity, R.id.addFragment);
// Option 2: Navigate using Intent
// NavigationUtil.navigateTo(activity, AddActivity.class);
return true;
}
return false;
});
}
}Code template for SideMenu.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* Navigation drawer implementation using Material Design.
* Features:
* - Material Design NavigationView
* - Drawer layout configuration
* - Menu item handling with NavigationUtil
* - Supports both fragment-based and intent-based navigation
*
* Usage:
* 1. Include the side menu in your activity layout
* 2. Setup menu items in res/menu
* 3. Uncomment either fragment-based or intent-based navigation
*/
package ${PACKAGE_NAME}.ui.navigation;
import android.app.Activity;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
import ${PACKAGE_NAME}.R;
public class SideMenu {
private final DrawerLayout drawerLayout;
private final NavigationView navigationView;
private final Activity activity;
public SideMenu(Activity activity) {
this.activity = activity;
this.drawerLayout = activity.findViewById(R.id.drawer_layout);
this.navigationView = activity.findViewById(R.id.side_nav_view);
setupDrawerContent();
}
private void setupDrawerContent() {
navigationView.setNavigationItemSelectedListener(menuItem -> {
int id = menuItem.getItemId();
if (id == R.id.nav_home) {
// Option 1: Navigate using NavController (uncomment this)
// NavigationUtil.navigateTo(activity, R.id.peopleListFragment);
// Option 2: Navigate using Intent (uncomment this)
// NavigationUtil.navigateTo(activity, HomeActivity.class);
} else if (id == R.id.nav_settings) {
// Option 1: Navigate using NavController
// NavigationUtil.navigateTo(activity, R.id.settingsFragment);
// Option 2: Navigate using Intent
// NavigationUtil.navigateTo(activity, SettingsActivity.class);
}
drawerLayout.closeDrawers();
return true;
});
}
}ViewHolder
This is a RecyclerView ViewHolder template with view binding and data binding method for efficient view recycling
Code template for CustomViewHolder.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* RecyclerView ViewHolder template for item views.
* Features:
* - View binding implementation
* - Click handling
* - Data binding methods
* - Efficient view recycling
*
* Usage:
* 1. Define item layout
* 2. Implement bind method
* 3. Use with RecyclerView adapter
*/
package ${PACKAGE_NAME};
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
// Replace ${PACKAGE_NAME} with your actual package name
// Replace ${VIEW_HOLDER_NAME} with the name of your ViewHolder class
// Replace ${ITEM_LAYOUT_NAME} with the layout resource ID for your item view
public class ${VIEW_HOLDER_NAME} extends RecyclerView.ViewHolder {
// Define your view components here
private final TextView textViewTitle;
private final TextView textViewSubtitle;
public ${VIEW_HOLDER_NAME}(@NonNull View itemView) {
super(itemView);
// Initialize your view components
textViewTitle = itemView.findViewById(R.id.tv_card_title);
textViewSubtitle = itemView.findViewById(R.id.tv_card_subtitle);
}
/**
* Bind data to the views.
*
* @param item The data item to bind
*/
public void bind(${MODEL_NAME} item) {
// Set data to your views
textViewTitle.setText(item.getTitle());
textViewSubtitle.setText(item.getSubtitle());
}
}ViewModel
I have implement a view model for repository integration for Entity View Model so far. It have CRUD operations for lifecyle-aware data handling too.
Code template for CustomEntityViewModel.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* AndroidViewModel implementation for MVVM architecture.
* Features:
* - LiveData management
* - Repository integration
* - Configuration change handling
* - Clean architecture compliance
*
* Usage:
* 1. Initialize with repository
* 2. Implement data operations
* 3. Observe LiveData in UI
*/
package ${PACKAGE_NAME};
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import java.util.List;
// Replace ${PACKAGE_NAME} with your package name
// Replace ${ENTITY_VIEW_MODEL_NAME} with your EntityViewModel class name
// Replace ${MODEL_NAME} with your data model class name
// Replace ${REPOSITORY_NAME} with your repository class name
public class ${ENTITY_VIEW_MODEL_NAME} extends AndroidViewModel {
private final ${REPOSITORY_NAME} repository;
private final LiveData<List<${MODEL_NAME}>> allItems;
public ${ENTITY_VIEW_MODEL_NAME}(@NonNull Application application) {
super(application);
repository = new ${REPOSITORY_NAME}(application);
allItems = repository.getAllItems();
}
public LiveData<List<${MODEL_NAME}>> getAllItems() {
return allItems;
}
public LiveData<${MODEL_NAME}> getItemById(int itemId) {
return repository.getItemById(itemId);
}
public void insert(${MODEL_NAME} item) {
repository.insert(item);
}
public void update(${MODEL_NAME} item) {
repository.update(item);
}
public void delete(${MODEL_NAME} item) {
repository.delete(item);
}
}Util
These utilities are so important that I have created so much utilities to help me to speed up the backend development. There are indeed too many redundant code and packaging them up into a Class is definitely the best idea.
There are Navigation Util and Storage Util. Navigation Util will handle the navigation between fragment or activity either using intent or navigation graph. Then the storage utils are so many and I will then let user choose which one to use according to their suitability in their project.
To avoid confusion, I have added the sample usage of these util classes in the comment section of the code.
Code template for AppStorageUtil.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* A modular utility class for app-specific storage using internal storage.
* Features:
* - Singleton pattern for easy access.
* - Methods to save and read text data to/from a file.
* - File deletion support.
*
* Usage:
* 1. Retrieve an instance via AppStorageUtil.getInstance(context);
* 2. Use saveToFile() to write data and readFromFile() to retrieve data.
*
* Sample Usage
* // requireContext() gets Fragment’s context
* // 'this' refers to the Activity context
*
* // Save data to internal storage
* AppStorageUtil.saveToFile(context, "config.json", "{ 'theme': 'dark' }");
*
* // Read data from internal storage
* String configData = AppStorageUtil.readFromFile(context, "config.json");
*
* // Delete a file
* AppStorageUtil.deleteFile(context, "config.json");
*
* // Check if file exists
* boolean exists = AppStorageUtil.fileExists(context, "config.json");
*/
public class AppStorageUtil {
private static AppStorageUtil instance;
private Context context;
private AppStorageUtil(Context context) {
this.context = context.getApplicationContext();
}
public static synchronized AppStorageUtil getInstance(Context context) {
if (instance == null) {
instance = new AppStorageUtil(context);
}
return instance;
}
// Save text data to a file.
public boolean saveToFile(String fileName, String data) {
try (FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE)) {
fos.write(data.getBytes());
fos.flush();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
// Read text data from a file.
public String readFromFile(String fileName) {
try (FileInputStream fis = context.openFileInput(fileName);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr)) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString().trim();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
// Delete a file.
public boolean deleteFile(String fileName) {
return context.deleteFile(fileName);
}
}Code template for DiskCacheUtil.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* A persistent disk cache using DiskLruCache.
* Features:
* - Stores data on disk for long-term caching.
* - Automatic eviction when full.
*
* Usage:
* 1. Initialize using DiskCacheUtil.getInstance(context);
* 2. Use put() and get() for cache operations.
*
* Sample Usage
* // requireContext() gets Fragment’s context
* // 'this' refers to the Activity context
*
* // Save data to disk cache
* DiskCacheUtil.put(context, "user_profile", "{ 'name': 'John', 'age': 25 }");
*
* // Retrieve data from disk cache
* String userProfile = DiskCacheUtil.get(context, "user_profile");
*
* // Check if cache entry exists
* boolean exists = DiskCacheUtil.contains(context, "user_profile");
*
* // Remove specific cache entry
* DiskCacheUtil.remove(context, "user_profile");
*
* // Clear entire disk cache
* DiskCacheUtil.clearAll(context);
*/
public class DiskCacheUtil {
private static final int APP_VERSION = 1;
private static final int VALUE_COUNT = 1;
private static final long MAX_SIZE = 10 * 1024 * 1024; // 10MB cache size
private static DiskCacheUtil instance;
private DiskLruCache diskCache;
private DiskCacheUtil(Context context) throws IOException {
File cacheDir = new File(context.getCacheDir(), "disk_cache");
diskCache = DiskLruCache.open(cacheDir, APP_VERSION, VALUE_COUNT, MAX_SIZE);
}
public static synchronized DiskCacheUtil getInstance(Context context) throws IOException {
if (instance == null) {
instance = new DiskCacheUtil(context);
}
return instance;
}
public void put(String key, String data) throws IOException {
String hashedKey = String.valueOf(key.hashCode());
DiskLruCache.Editor editor = diskCache.edit(hashedKey);
if (editor != null) {
try (OutputStream output = editor.newOutputStream(0)) {
output.write(data.getBytes());
output.flush();
editor.commit();
} catch (IOException e) {
editor.abort();
throw e;
}
}
}
public String get(String key) throws IOException {
String hashedKey = String.valueOf(key.hashCode());
DiskLruCache.Snapshot snapshot = diskCache.get(hashedKey);
if (snapshot != null) {
try (InputStream input = snapshot.getInputStream(0);
BufferedReader reader = new BufferedReader(new InputStreamReader(input))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return sb.toString();
}
}
return null;
}
public void remove(String key) throws IOException {
String hashedKey = String.valueOf(key.hashCode());
diskCache.remove(hashedKey);
}
public void clear() throws IOException {
diskCache.delete();
}
}Code template for HybridCacheUtil.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* A hybrid caching system using LruCache (memory) and DiskLruCache (disk).
* Features:
* - Uses memory cache for fast access.
* - Falls back to disk cache if memory cache misses.
* - Evicts old data automatically when full.
*
* Usage:
* 1. Initialize using HybridCacheUtil.getInstance(context, maxSize);
* 2. Use put(), get(), and remove() to manage cache.
*
* Sample Usage
* // requireContext() gets Fragment’s context
* // 'this' refers to the Activity context
*
* // Store data in hybrid cache (tries memory first, then disk)
* HybridCacheUtil.put(context, "session_token", "abcdef123456");
*
* // Retrieve data (first tries memory, then falls back to disk)
* String token = HybridCacheUtil.get(context, "session_token");
*
* // Remove an entry from hybrid cache
* HybridCacheUtil.remove(context, "session_token");
*
* // Clear entire hybrid cache
* HybridCacheUtil.clearAll(context);
*/
public class HybridCacheUtil {
private static HybridCacheUtil instance;
private MemoryCacheUtil memoryCache;
private DiskCacheUtil diskCache;
private HybridCacheUtil(Context context, int memorySize) throws IOException {
memoryCache = new MemoryCacheUtil(memorySize);
diskCache = DiskCacheUtil.getInstance(context);
}
public static synchronized HybridCacheUtil getInstance(Context context, int memorySize) throws IOException {
if (instance == null) {
instance = new HybridCacheUtil(context, memorySize);
}
return instance;
}
public void put(String key, String data) {
memoryCache.put(key, data);
try {
diskCache.put(key, data);
} catch (IOException e) {
e.printStackTrace();
}
}
public String get(String key) {
// Try memory cache first
String data = (String) memoryCache.get(key);
if (data != null) {
return data;
}
// Fallback to disk cache
try {
data = diskCache.get(key);
if (data != null) {
memoryCache.put(key, data);
}
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
public void remove(String key) {
memoryCache.remove(key);
try {
diskCache.remove(key);
} catch (IOException e) {
e.printStackTrace();
}
}
public void clear() {
memoryCache.clear();
try {
diskCache.clear();
} catch (IOException e) {
e.printStackTrace();
}
}
}Code template for MemoryCacheUtil.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* A lightweight memory cache using LruCache.
* Features:
* - Stores objects in memory for fast access.
* - Removes least recently used items when full.
*
* Usage:
* 1. Initialize using MemoryCacheUtil.init(maxSize);
* 2. Use put(), get(), and remove() for caching.
*
* Sample Usage
* // requireContext() gets Fragment’s context
* // 'this' refers to the Activity context
*
* // Add data to memory cache
* MemoryCacheUtil.put("api_response", "{ 'status': 'ok', 'data': [] }");
*
* // Retrieve data from memory cache
* String response = MemoryCacheUtil.get("api_response");
*
* // Check if cache contains key
* boolean exists = MemoryCacheUtil.contains("api_response");
*
* // Remove a specific entry
* MemoryCacheUtil.remove("api_response");
*
* // Clear entire memory cache
* MemoryCacheUtil.clearAll();
*/
public class MemoryCacheUtil {
private static MemoryCacheUtil instance;
private LruCache<String, Object> memoryCache;
private MemoryCacheUtil(int maxSize) {
memoryCache = new LruCache<>(maxSize);
}
public static synchronized void init(int maxSize) {
if (instance == null) {
instance = new MemoryCacheUtil(maxSize);
}
}
public static MemoryCacheUtil getInstance() {
if (instance == null) {
throw new IllegalStateException("MemoryCacheUtil not initialized. Call init(maxSize) first.");
}
return instance;
}
public void put(String key, Object value) {
memoryCache.put(key, value);
}
public Object get(String key) {
return memoryCache.get(key);
}
public void remove(String key) {
memoryCache.remove(key);
}
public void clear() {
memoryCache.evictAll();
}
}Code template for NavigationUtil.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* Utility class for handling navigation throughout the app.
* Features:
* - Static navigation methods
* - Intent management
* - Activity transition handling
* - Common navigation patterns
*
* Usage:
* 1. Import this utility class
* 2. Call static methods for navigation
* 3. Uncomment the navigation option (fragment-based or intent-based)
*/
package ${PACKAGE_NAME}.ui.navigation;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import ${PACKAGE_NAME}.R;
public class NavigationUtil {
private NavigationUtil() {
// Private constructor to prevent instantiation
}
// Navigate using NavController (for fragments) without arguments
public static void navigateTo(Fragment fragment, int actionId) {
try {
NavController navController = Navigation.findNavController(fragment.requireView());
navController.navigate(actionId);
} catch (Exception e) {
Log.e("NavigationUtil", "NavController not found, check setup.");
}
}
// Navigate using NavController (for activities)
public static void navigateTo(Activity activity, int actionId) {
try {
NavController navController = Navigation.findNavController(activity, R.id.nav_host_fragment);
navController.navigate(actionId);
} catch (Exception e) {
Log.e("NavigationUtil", "NavController not found in Activity, check setup.");
}
}
// Navigate using Intent (for activities)
public static void navigateTo(Activity activity, Class<?> destination) {
Intent intent = new Intent(activity, destination);
activity.startActivity(intent);
}
// Navigate back (pop the back stack) using the NavController.
public static void navigateBack(Fragment fragment) {
try {
NavController navController = Navigation.findNavController(fragment.requireView());
navController.popBackStack();
} catch (Exception e) {
Log.e("NavigationUtil", "Failed to navigate back", e);
}
}
}Code template for PreferenceManagerUtil.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awesome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* Sample Usage
* // requireContext() gets Fragment’s context
* // 'this' refers to the Activity context
*
* // Save user preferences
* PreferenceManagerUtil.setBoolean(context, "notifications_enabled", true);
* PreferenceManagerUtil.setString(context, "theme", "dark");
*
* // Retrieve user preferences
* boolean notifications = PreferenceManagerUtil.getBoolean(context, "notifications_enabled");
* String theme = PreferenceManagerUtil.getString(context, "theme", "light");
*
* // Remove a specific preference
* PreferenceManagerUtil.remove(context, "theme");
*
* // Clear all preferences
* PreferenceManagerUtil.clearAll(context);
*/
package com.example.utils;
import android.content.Context;
import android.content.SharedPreferences;
public class PreferenceManagerUtil {
private static final String PREF_NAME = "app_preferences";
private static SharedPreferences getPreferences(Context context) {
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
public static void setString(Context context, String key, String value) {
SharedPreferences.Editor editor = getPreferences(context).edit();
editor.putString(key, value);
editor.apply();
}
public static String getString(Context context, String key, String defaultValue) {
return getPreferences(context).getString(key, defaultValue);
}
public static void setBoolean(Context context, String key, boolean value) {
SharedPreferences.Editor editor = getPreferences(context).edit();
editor.putBoolean(key, value);
editor.apply();
}
public static boolean getBoolean(Context context, String key) {
return getPreferences(context).getBoolean(key, false);
}
public static void setInt(Context context, String key, int value) {
SharedPreferences.Editor editor = getPreferences(context).edit();
editor.putInt(key, value);
editor.apply();
}
public static int getInt(Context context, String key, int defaultValue) {
return getPreferences(context).getInt(key, defaultValue);
}
public static void remove(Context context, String key) {
SharedPreferences.Editor editor = getPreferences(context).edit();
editor.remove(key);
editor.apply();
}
public static void clearAll(Context context) {
SharedPreferences.Editor editor = getPreferences(context).edit();
editor.clear();
editor.apply();
}
}Code template for SharedPrefsUtil.java
/**
* Created by Sim Sze Yu
* GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
*
* This template is part of the WIA2007 Mobile Application Development Course
* Bachelor of Computer Science (Artificial Intelligence)
* Universiti Malaya
*
* Licensed under MIT License
* Copyright (c) 2025 Sim Sze Yu
*
* A modular utility class for handling SharedPreferences in Android.
* Features:
* - Singleton pattern for global access.
* - Easy read/write operations for various data types.
* - Supports storing and retrieving lists using Gson.
* - Methods to clear or remove preferences.
*
* Usage:
* 1. Initialize in your Application class using SharedPrefsUtil.init(context);
* 2. Retrieve the instance via SharedPrefsUtil.getInstance() for subsequent operations.
*
* Sample Usage
* // requireContext() gets Fragment’s context
* // 'this' refers to the Activity context
*
* // Save user preferences
* SharedPrefsUtil.getInstance().putBoolean("first_launch", false);
* SharedPrefsUtil.getInstance().putString("username", "SimSzeYu");
*
* // Retrieve user preferences
* boolean isFirstLaunch = SharedPrefsUtil.getInstance().getBoolean("first_launch", true);
* String username = SharedPrefsUtil.getInstance().getString("username", "Guest");
*
* // Save and retrieve lists
* List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
* SharedPrefsUtil.getInstance().putStringList("name_list", names);
* List<String> retrievedNames = SharedPrefsUtil.getInstance().getStringList("name_list");
*
* List<Integer> scores = Arrays.asList(10, 20, 30);
* SharedPrefsUtil.getInstance().putIntList("score_list", scores);
* List<Integer> retrievedScores = SharedPrefsUtil.getInstance().getIntList("score_list");
*
* // Remove a specific preference
* SharedPrefsUtil.getInstance().remove("username");
*
* // Clear all preferences
* SharedPrefsUtil.getInstance().clear();
*/
import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class SharedPrefsUtil {
private static final String PREFS_NAME = "SharedPrefsUtil"; // Replace with your preference file name.
private static SharedPrefsUtil instance;
private SharedPreferences sharedPreferences;
private Gson gson;
private SharedPrefsUtil(Context context) {
sharedPreferences = context.getApplicationContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
gson = new Gson();
}
public static synchronized void init(Context context) {
if (instance == null) {
instance = new SharedPrefsUtil(context);
}
}
public static SharedPrefsUtil getInstance() {
if (instance == null) {
throw new IllegalStateException("SharedPrefsUtil is not initialized. Call init(context) in your Application class.");
}
return instance;
}
// Save a String value.
public void putString(String key, String value) {
sharedPreferences.edit().putString(key, value).apply();
}
// Retrieve a String value.
public String getString(String key, String defaultValue) {
return sharedPreferences.getString(key, defaultValue);
}
// Save an integer value.
public void putInt(String key, int value) {
sharedPreferences.edit().putInt(key, value).apply();
}
// Retrieve an integer value.
public int getInt(String key, int defaultValue) {
return sharedPreferences.getInt(key, defaultValue);
}
// Save a boolean value.
public void putBoolean(String key, boolean value) {
sharedPreferences.edit().putBoolean(key, value).apply();
}
// Retrieve a boolean value.
public boolean getBoolean(String key, boolean defaultValue) {
return sharedPreferences.getBoolean(key, defaultValue);
}
// Save a long value.
public void putLong(String key, long value) {
sharedPreferences.edit().putLong(key, value).apply();
}
// Retrieve a long value.
public long getLong(String key, long defaultValue) {
return sharedPreferences.getLong(key, defaultValue);
}
// Save a float value.
public void putFloat(String key, float value) {
sharedPreferences.edit().putFloat(key, value).apply();
}
// Retrieve a float value.
public float getFloat(String key, float defaultValue) {
return sharedPreferences.getFloat(key, defaultValue);
}
// Save a list of strings.
public void putStringList(String key, List<String> list) {
String json = gson.toJson(list);
sharedPreferences.edit().putString(key, json).apply();
}
// Retrieve a list of strings.
public List<String> getStringList(String key) {
String json = sharedPreferences.getString(key, null);
if (json == null) {
return new ArrayList<>();
}
Type type = new TypeToken<List<String>>() {}.getType();
return gson.fromJson(json, type);
}
// Save a list of integers.
public void putIntList(String key, List<Integer> list) {
String json = gson.toJson(list);
sharedPreferences.edit().putString(key, json).apply();
}
// Retrieve a list of integers.
public List<Integer> getIntList(String key) {
String json = sharedPreferences.getString(key, null);
if (json == null) {
return new ArrayList<>();
}
Type type = new TypeToken<List<Integer>>() {}.getType();
return gson.fromJson(json, type);
}
// Remove a specific key.
public void remove(String key) {
sharedPreferences.edit().remove(key).apply();
}
// Clear all preferences.
public void clear() {
sharedPreferences.edit().clear().apply();
}
}Layouts
Now these are the xml templates for layout which is the frontend development. Here come the song APT: “All you gotta do is just..” ok just copy and paste those xml file into the android studio XD😆.
- custom_login_layout.xml — Mateiral Design login screen with email and password field.
- custom_bottom_nav_layout.xml — Layout for bottom navigation integrated with a fragment container.
- custom_side_nav_layout.xml — Layout for a navigation drawer with header and menu items.
- custom_card_layout.xml — Material card layout with image, title, and subtitle.
- custom_button_layout.xml — Layout showcasing various Material button styles.
- custom_toolbar_layout.xml —Custom toolbar layout with title and menu support.
- custom_navigator_main_activity.xml — Layout for CustomNavigatorMainActivity, including AppBarLayout with Toolbar, FragmentContainerView, BottomNavigationView, and NavigationView for side menu.
- custom_form_controls_layout.xml — Layout for which consists of variety of component for form control.
- custom_list_recyler_view_layout.xml — Custom RecyclerView layout with standard spacing and consistent design.
Code template for custom_login_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Created by Sim Sze Yu
~ GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
~
~ This template is part of the WIA2007 Mobile Application Development Course
~ Bachelor of Computer Science (Artificial Intelligence)
~ Universiti Malaya
~
~ Licensed under MIT License
~ Copyright (c) 2025 Sim Sze Yu
~
~ Material Design login layout template.
~ Features:
~ - Material TextInputLayout for fields
~ - Responsive design
~ - Error handling support
~ - Remember me option
~
~ Usage:
~ 1. Include in your login activity
~ 2. Customize colors and text
~ 3. Add validation logic
-->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="@color/background">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/spacing_large">
<ImageView
android:id="@+id/iv_logo"
android:layout_width="@dimen/icon_size_large"
android:layout_height="@dimen/icon_size_large"
android:layout_marginTop="@dimen/spacing_xlarge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/til_email"
style="@style/Widget.Custom.TextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_xlarge"
android:hint="Email"
app:layout_constraintTop_toBottomOf="@id/iv_logo">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/til_password"
style="@style/Widget.Custom.TextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
app:layout_constraintTop_toBottomOf="@id/til_email"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_login"
style="@style/Widget.Custom.Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_large"
android:text="Login"
app:layout_constraintTop_toBottomOf="@id/til_password" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>Code template for custom_bottom_nav_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Created by Sim Sze Yu
~ GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
~
~ This template is part of the WIA2007 Mobile Application Development Course
~ Bachelor of Computer Science (Artificial Intelligence)
~ Universiti Malaya
~
~ Licensed under MIT License
~ Copyright (c) 2025 Sim Sze Yu
~
~ Material Design bottom navigation layout template.
~ Features:
~ - Fixed bottom navigation
~ - Fragment container
~ - Material Design styling
~ - Customizable menu items
~
~ Usage:
~ 1. Include in your main activity
~ 2. Set up menu resource
~ 3. Configure navigation behavior
-->
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/surface"
app:itemIconTint="@color/primary"
app:itemTextColor="@color/primary"
app:itemIconSize="@dimen/icon_size_small"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/custom_bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>Code template for custom_side_nav_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Created by Sim Sze Yu
~ GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
~
~ This template is part of the WIA2007 Mobile Application Development Course
~ Bachelor of Computer Science (Artificial Intelligence)
~ Universiti Malaya
~
~ Licensed under MIT License
~ Copyright (c) 2025 Sim Sze Yu
~
~ Navigation drawer layout template.
~ Features:
~ - DrawerLayout implementation
~ - Header view support
~ - Menu grouping
~ - Material Design styling
~
~ Usage:
~ 1. Include in your main activity
~ 2. Set up navigation menu
~ 3. Add header layout if needed
-->
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Main Content -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/custom_toolbar_layout" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- Navigation Drawer -->
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="@color/surface"
app:itemIconTint="@color/primary"
app:itemTextColor="@color/text_primary"
app:itemIconSize="@dimen/icon_size_small"
app:itemHorizontalPadding="@dimen/spacing_medium"
app:menu="@menu/custom_side_nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>Code template for custom_card_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Created by Sim Sze Yu
~ GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
~
~ This template is part of the WIA2007 Mobile Application Development Course
~ Bachelor of Computer Science (Artificial Intelligence)
~ Universiti Malaya
~
~ Licensed under MIT License
~ Copyright (c) 2025 Sim Sze Yu
~
~ Material Design card layout template.
~ Features:
~ - MaterialCardView implementation
~ - Image support
~ - Title and subtitle areas
~ - Customizable elevation
~
~ Usage:
~ 1. Use in RecyclerView items
~ 2. Customize card appearance
~ 3. Add click behaviors
-->
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
style="@style/Widget.Custom.CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_card_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
app:layout_constraintTop_toTopOf="parent"
tools:src="@tools:sample/backgrounds/scenic" />
<TextView
android:id="@+id/tv_card_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/spacing_medium"
android:layout_marginTop="@dimen/spacing_medium"
android:textAppearance="@style/TextAppearance.Custom.Title"
app:layout_constraintTop_toBottomOf="@id/iv_card_image"
tools:text="Card Title" />
<TextView
android:id="@+id/tv_card_subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/spacing_medium"
android:layout_marginTop="@dimen/spacing_small"
android:layout_marginBottom="@dimen/spacing_medium"
android:textAppearance="@style/TextAppearance.Custom.Body"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_card_title"
tools:text="Card Subtitle" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>Code template for custom_button_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Created by Sim Sze Yu
~ GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
~
~ This template is part of the WIA2007 Mobile Application Development Course
~ Bachelor of Computer Science (Artificial Intelligence)
~ Universiti Malaya
~
~ Licensed under MIT License
~ Copyright (c) 2025 Sim Sze Yu
~
~ Material Design button collection template.
~ Features:
~ - Different button styles (contained, outlined, text)
~ - Icon support
~ - Custom corner radius
~ - Consistent padding
~
~ Usage:
~ 1. Include buttons as needed
~ 2. Customize styles
~ 3. Add click handlers
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/spacing_medium">
<!-- Primary Button -->
<com.google.android.material.button.MaterialButton
style="@style/Widget.Custom.Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Primary Button" />
<!-- Outlined Button -->
<com.google.android.material.button.MaterialButton
style="@style/Widget.Custom.Button.Outlined"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_medium"
android:text="Outlined Button" />
<!-- Text Button -->
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_medium"
android:text="Text Button"
android:textColor="@color/primary" />
<!-- Icon Button -->
<com.google.android.material.button.MaterialButton
style="@style/Widget.Custom.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_medium"
android:text="Icon Button"
app:icon="@drawable/baseline_add_24"
app:iconGravity="textStart"
app:iconTint="@color/text_light"
app:iconPadding="@dimen/spacing_small" />
</LinearLayout>Code template for custom_toolbar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Created by Sim Sze Yu
~ GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
~
~ This template is part of the WIA2007 Mobile Application Development Course
~ Bachelor of Computer Science (Artificial Intelligence)
~ Universiti Malaya
~
~ Licensed under MIT License
~ Copyright (c) 2025 Sim Sze Yu
~
~ Material Design toolbar template.
~ Features:
~ - AppBarLayout integration
~ - Title customization
~ - Menu support
~ - Elevation control
~
~ Usage:
~ 1. Include in activity layout
~ 2. Set up menu items
~ 3. Configure navigation icon
-->
<com.google.android.material.appbar.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.MaterialComponents.Light">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/app_name"
android:textAppearance="@style/TextAppearance.Custom.Title"
android:textColor="@color/text_light" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>Code template for custom_navigator_main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
Created by Sim Sze Yu
GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
This template is part of the WIA2007 Mobile Application Development Course
Bachelor of Computer Science (Artificial Intelligence)
Universiti Malaya
Licensed under MIT License
Copyright (c) 2025 Sim Sze Yu
XML layout for CustomNavigatorMainActivity.
Features:
- AppBarLayout with Toolbar
- Fragment container for Navigation Component
- Bottom navigation menu
- Side navigation drawer
-->
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CustomNavigatorMainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="@string/app_name" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/surface"
app:itemIconTint="@color/primary"
app:itemTextColor="@color/primary"
app:menu="@menu/bottom_nav_menu" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/side_nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/surface"
app:menu="@menu/side_nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>Code template for custom_form_controls_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Created by Sim Sze Yu
~ GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
~
~ This template is part of the WIA2007 Mobile Application Development Course
~ Bachelor of Computer Science (Artificial Intelligence)
~ Universiti Malaya
~
~ Licensed under MIT License
~ Copyright (c) 2025 Sim Sze Yu
~
~ Custom form controls layout with Material Design styling.
~ Features:
~ - Styled RadioGroup, CheckBox, Switch, AutoCompleteTextView, Spinner
~ - Uses Material Theme colors and dimensions
~ - Consistent spacing and typography
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/spacing_medium"
android:background="@color/background">
<!-- Radio Group -->
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/spacing_small">
<RadioButton
android:id="@+id/radio1"
style="@style/Widget.MaterialComponents.CompoundButton.RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:id="@+id/radio2"
style="@style/Widget.MaterialComponents.CompoundButton.RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
</RadioGroup>
<!-- Checkboxes -->
<CheckBox
android:id="@+id/checkbox1"
style="@style/Widget.MaterialComponents.CompoundButton.CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check 1" />
<CheckBox
android:id="@+id/checkbox2"
style="@style/Widget.MaterialComponents.CompoundButton.CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check 2" />
<CheckBox
android:id="@+id/checkbox3"
style="@style/Widget.MaterialComponents.CompoundButton.CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check 3" />
<!-- Switch -->
<Switch
android:id="@+id/toggleSwitch"
style="@style/Widget.MaterialComponents.CompoundButton.Switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enable feature" />
<!-- AutoCompleteTextView -->
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
style="@style/Widget.Custom.TextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Choose an item" />
<!-- Spinner -->
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_small"
android:popupBackground="@color/surface" />
<!-- Submit Button -->
<Button
android:id="@+id/submitButton"
style="@style/Widget.Custom.Button"
android:layout_width="match_parent"
android:layout_height="@dimen/button_height"
android:text="Submit" />
</LinearLayout>Code template for custom_list_recycler_view_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Created by Sim Sze Yu
~ GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
~
~ This template is part of the WIA2007 Mobile Application Development Course
~ Bachelor of Computer Science (Artificial Intelligence)
~ Universiti Malaya
~
~ Licensed under MIT License
~ Copyright (c) 2025 Sim Sze Yu
~
~ Custom RecyclerView layout with standard spacing and consistent design.
~ Features:
~ - RecyclerView with ConstraintLayout
~ - Custom list item using person_card_layout
~ - Standard margin and padding
~
~ Usage:
~ 1. Place RecyclerView inside layouts
~ 2. Use with custom adapter and `custom_card_layout` for list items
~ 3. Maintain consistent spacing and design across RecyclerView
-->
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- RecyclerView for displaying list of people -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_people"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/custom_card_layout" />
</androidx.constraintlayout.widget.ConstraintLayout>Menus
Now these are the xml templates for menu.
- custom_bottom_nav_menu.xml — Menu resource for bottom navigation items.
- custom_main_nav_menu.xml — Menu resource for the overflow menu in CustomNavigatorMainActivity.
- custom_side_nav_menu.xml — Menu resource for navigation drawer items.
Code template for custom_bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Created by Sim Sze Yu
~ GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
~
~ This template is part of the WIA2007 Mobile Application Development Course
~ Bachelor of Computer Science (Artificial Intelligence)
~ Universiti Malaya
~
~ Licensed under MIT License
~ Copyright (c) 2025 Sim Sze Yu
~
~ Bottom navigation menu resource template.
~ Features:
~ - Standard navigation items
~ - Icon and label support
~ - Material Design icons
~ - Item ID configuration
~
~ Usage:
~ 1. Customize menu items
~ 2. Add icons
~ 3. Set up navigation IDs
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_home"
android:icon="@drawable/baseline_home_24"
android:title="Home" />
<item
android:id="@+id/nav_dashboard"
android:icon="@drawable/baseline_space_dashboard_24"
android:title="Dashboard" />
<item
android:id="@+id/nav_notifications"
android:icon="@drawable/baseline_notifications_24"
android:title="Notifications" />
<item
android:id="@+id/nav_profile"
android:icon="@drawable/baseline_person_24"
android:title="Profile" />
</menu>Code template for custom_main_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?><!--
~ Created by Sim Sze Yu
~ GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
~
~ This template is part of the WIA2007 Mobile Application Development Course
~ Bachelor of Computer Science (Artificial Intelligence)
~ Universiti Malaya
~
~ Licensed under MIT License
~ Copyright (c) 2025 Sim Sze Yu
~
~ Overflow menu resource template.
~ Features:
~ - Supports action items and overflow menu
~ - Configurable icons and titles
~ - Easy extension for additional actions
~
~ Usage:
~ 1. Add menu items with appropriate IDs and icons
~ 2. Configure "showAsAction" based on UI needs
~ 3. Implement actions in onOptionsItemSelected()
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Settings option (always visible if space allows) -->
<item
android:id="@+id/action_settings"
android:icon="@drawable/baseline_settings_24"
android:title="Settings"
app:showAsAction="ifRoom" />
<!-- Help option (always in overflow menu) -->
<item
android:id="@+id/action_help"
android:icon="@drawable/baseline_help_outline_24"
android:title="Help"
app:showAsAction="never" />
<!-- About option (always in overflow menu) -->
<item
android:id="@+id/action_about"
android:icon="@drawable/baseline_info_24"
android:title="About"
app:showAsAction="never" />
</menu>Code template for custom_side_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Created by Sim Sze Yu
~ GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
~
~ This template is part of the WIA2007 Mobile Application Development Course
~ Bachelor of Computer Science (Artificial Intelligence)
~ Universiti Malaya
~
~ Licensed under MIT License
~ Copyright (c) 2025 Sim Sze Yu
~
~ Navigation drawer menu resource template.
~ Features:
~ - Grouped menu items
~ - Submenus support
~ - Icon integration
~ - Checkable items
~
~ Usage:
~ 1. Define menu groups
~ 2. Add menu items
~ 3. Configure icons
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/baseline_home_24"
android:title="Home" />
<item
android:id="@+id/nav_dashboard"
android:icon="@drawable/baseline_space_dashboard_24"
android:title="Dashboard" />
<item
android:id="@+id/nav_notifications"
android:icon="@drawable/baseline_notifications_24"
android:title="Notifications" />
</group>
<item android:title="Other">
<menu>
<item
android:id="@+id/nav_settings"
android:icon="@drawable/baseline_settings_24"
android:title="Settings" />
<item
android:id="@+id/nav_about"
android:icon="@drawable/baseline_info_24"
android:title="About" />
<item
android:id="@+id/nav_logout"
android:icon="@drawable/baseline_logout_24"
android:title="Logout" />
</menu>
</item>
</menu>Values
Now these are the xml templates for values.
- custom_colors.xml — Material Design color system resources.
- custom_dimens.xml — Material Design color system resources.
- custom_styles.xml — Material component styles and themes.
Code template for custom_colors.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Created by Sim Sze Yu
~ GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
~
~ This template is part of the WIA2007 Mobile Application Development Course
~ Bachelor of Computer Science (Artificial Intelligence)
~ Universiti Malaya
~
~ Licensed under MIT License
~ Copyright (c) 2025 Sim Sze Yu
~
~ Material Design color resource template.
~ Features:
~ - Material Design color system
~ - Primary and secondary colors
~ - Light and dark theme support
~ - Semantic color naming
~
~ Usage:
~ 1. Use in themes and styles
~ 2. Reference in layouts
~ 3. Maintain consistent branding
-->
<resources>
<!-- Primary Colors -->
<color name="primary">#1976D2</color>
<color name="primary_dark">#1565C0</color>
<color name="primary_light">#42A5F5</color>
<!-- Secondary Colors -->
<color name="secondary">#FF4081</color>
<color name="secondary_dark">#F50057</color>
<color name="secondary_light">#FF80AB</color>
<!-- Neutral Colors -->
<color name="background">#FFFFFF</color>
<color name="surface">#FFFFFF</color>
<color name="error">#B00020</color>
<!-- Text Colors -->
<color name="text_primary">#DE000000</color>
<color name="text_secondary">#99000000</color>
<color name="text_disabled">#61000000</color>
<color name="text_light">#FFFFFF</color>
<!-- Component Colors -->
<color name="card_background">#FFFFFF</color>
<color name="card_stroke">#1F000000</color>
<color name="button_disabled">#1F000000</color>
<color name="ripple">#33000000</color>
<!-- Status Colors -->
<color name="success">#4CAF50</color>
<color name="warning">#FFC107</color>
<color name="info">#2196F3</color>
</resources>Code template for custom_dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Created by Sim Sze Yu
~ GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
~
~ This template is part of the WIA2007 Mobile Application Development Course
~ Bachelor of Computer Science (Artificial Intelligence)
~ Universiti Malaya
~
~ Licensed under MIT License
~ Copyright (c) 2025 Sim Sze Yu
~
~ Dimension resource template for consistent spacing and sizing.
~ Features:
~ - Standard spacing system
~ - Component dimensions
~ - Text sizes
~ - Common measurements
~
~ Usage:
~ 1. Reference in layouts
~ 2. Use in styles
~ 3. Maintain consistent spacing
-->
<resources>
<!-- Margins and Padding -->
<dimen name="spacing_tiny">4dp</dimen>
<dimen name="spacing_small">8dp</dimen>
<dimen name="spacing_medium">16dp</dimen>
<dimen name="spacing_large">24dp</dimen>
<dimen name="spacing_xlarge">32dp</dimen>
<dimen name="spacing_xxlarge">48dp</dimen>
<!-- Text Sizes -->
<dimen name="text_micro">12sp</dimen>
<dimen name="text_small">14sp</dimen>
<dimen name="text_medium">16sp</dimen>
<dimen name="text_large">20sp</dimen>
<dimen name="text_xlarge">24sp</dimen>
<dimen name="text_xxlarge">34sp</dimen>
<!-- Additional Text Sizes for Headline, Title, and Body -->
<dimen name="text_headline">24sp</dimen>
<dimen name="text_title">20sp</dimen>
<dimen name="text_body">16sp</dimen>
<!-- Component Sizes -->
<dimen name="button_height">48dp</dimen>
<dimen name="button_min_width">88dp</dimen>
<dimen name="button_corner_radius">8dp</dimen>
<dimen name="button_padding">12dp</dimen>
<!-- Card Dimensions -->
<dimen name="card_corner_radius">8dp</dimen>
<dimen name="card_elevation">4dp</dimen>
<dimen name="card_margin">8dp</dimen>
<dimen name="card_content_padding">16dp</dimen>
<!-- Icon Sizes -->
<dimen name="icon_size_small">24dp</dimen>
<dimen name="icon_size_medium">32dp</dimen>
<dimen name="icon_size_large">48dp</dimen>
<dimen name="icon_size_xlarge">64dp</dimen>
<!-- Input Fields -->
<dimen name="input_height">56dp</dimen>
<dimen name="input_spacing">8dp</dimen>
<dimen name="input_corner_radius">4dp</dimen>
<!-- Toolbar and Navigation -->
<dimen name="toolbar_height">56dp</dimen>
<dimen name="nav_header_height">176dp</dimen>
<dimen name="bottom_nav_height">56dp</dimen>
<!-- Dividers -->
<dimen name="divider_height">1dp</dimen>
<dimen name="divider_margin">8dp</dimen>
</resources>Code template for custom_styles.xml
<?xml version="1.0" encoding="utf-8"?><!--
~ Created by Sim Sze Yu
~ GitHub: https://github.com/szeyu/Awsome-Android-Code-Template
~
~ This template is part of the WIA2007 Mobile Application Development Course
~ Bachelor of Computer Science (Artificial Intelligence)
~ Universiti Malaya
~
~ Licensed under MIT License
~ Copyright (c) 2025 Sim Sze Yu
~
~ Material Design styles resource template.
~ Features:
~ - Custom theme attributes
~ - Component styles
~ - Text appearances
~ - Common widget styling
~
~ Usage:
~ 1. Apply styles to themes
~ 2. Use in layouts
~ 3. Customize as needed
-->
<resources>
<!-- Base Text Appearance -->
<style name="TextAppearance.Custom" parent="TextAppearance.AppCompat.Body1">
<item name="android:textSize">@dimen/text_body</item>
<item name="android:textColor">@color/text_primary</item>
<item name="android:fontFamily">sans-serif</item>
</style>
<!-- Headline Text Appearance -->
<style name="TextAppearance.Custom.Headline">
<item name="android:textSize">@dimen/text_headline</item>
<item name="android:textColor">@color/text_primary</item>
<item name="android:fontFamily">sans-serif-medium</item>
</style>
<!-- Title Text Appearance -->
<style name="TextAppearance.Custom.Title">
<item name="android:textSize">@dimen/text_title</item>
<item name="android:textColor">@color/text_primary</item>
<item name="android:fontFamily">sans-serif-medium</item>
</style>
<!-- Body Text Appearance -->
<style name="TextAppearance.Custom.Body">
<item name="android:textSize">@dimen/text_body</item>
<item name="android:textColor">@color/text_secondary</item>
<item name="android:fontFamily">sans-serif</item>
</style>
<!-- Button Styles -->
<style name="Widget.Custom.Button" parent="Widget.MaterialComponents.Button">
<item name="android:minHeight">@dimen/button_height</item>
<item name="android:minWidth">@dimen/button_min_width</item>
<item name="android:padding">@dimen/spacing_medium</item>
<item name="cornerRadius">@dimen/button_corner_radius</item>
</style>
<style name="Widget.Custom.Button.Outlined" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="android:minHeight">@dimen/button_height</item>
<item name="android:minWidth">@dimen/button_min_width</item>
<item name="android:padding">@dimen/spacing_medium</item>
<item name="cornerRadius">@dimen/button_corner_radius</item>
<item name="strokeWidth">1dp</item>
</style>
<!-- Card Styles -->
<style name="Widget.Custom.CardView" parent="Widget.MaterialComponents.CardView">
<item name="cardCornerRadius">@dimen/card_corner_radius</item>
<item name="cardElevation">@dimen/card_elevation</item>
<item name="android:layout_margin">@dimen/card_margin</item>
<item name="cardBackgroundColor">@color/card_background</item>
</style>
<!-- Input Field Styles -->
<style name="Widget.Custom.TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="android:layout_height">@dimen/input_height</item>
<item name="android:layout_marginTop">@dimen/input_spacing</item>
<item name="boxCornerRadiusTopStart">@dimen/button_corner_radius</item>
<item name="boxCornerRadiusTopEnd">@dimen/button_corner_radius</item>
<item name="boxCornerRadiusBottomStart">@dimen/button_corner_radius</item>
<item name="boxCornerRadiusBottomEnd">@dimen/button_corner_radius</item>
</style>
</resources>Alright! Now here comes the end of the templates code for my Android Development. I do really hope that these templates are able to help you to accelerate your mobile application development. If you like my project, come and support me on https://ko-fi.com/szeyusim!😍
Doing these templates took me days to edit the template over and over again until it works perfectly and speed up my development time. Although there are things called GPT and DeepSeek alrready but writing your own is cool!
Follow me for more update on Medium, I will write more blogs regarding my personal development on Coding Journey and share the notes with you all!
My GitHub: https://github.com/szeyu
My LinkedIn: https://www.linkedin.com/in/szeyusim/
