Modules are piece of code that is independently created and maintained and can be used in a different system. In Android apps modules can be a simple feature like Search or it may be purchased on demand. For eg. In gaming apps like PUBG, skins of the guns and the character are also a module. There are several modules namely they are,
- Application module
- Core module
- Abstraction module
- Feature module
- Dynamic support module
- Instant app module
Example
In this example, we will create an application GFG Modular Architecture, that has a login module, which we access from our application.
Step by Step Implementation
Step 1: Create a new project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Create a login module
To create a new module, Go to app > Right-Click > New > Module. After that select module type as Android Library and give a name to this module as login. After that, you will find a new login module is created separately in the root path of the application. You can also verify it by going to Root-Path > Gradle Script > settings.gradle In settings.gradle you will find the login module is included in our GFG Modular Architecture application.
include ':login' include ':app' rootProject.name = "GFG Modular Architecture"
Before moving further let’s add some color attributes in order to enhance the app view. Go to the module login > res > values > right-click > New > Value Resource and create a new value resource file and name it as colors.xml and add the following color attributes.
XML
< resources > < color name = "colorPrimary" >#0F9D58</ color > < color name = "colorPrimaryDark" >#16E37F</ color > < color name = "colorAccent" >#03DAC5</ color > </ resources > |
Step 3: Adding dependency
We need to add a dependency for the login module to our GFG Modular Architecture app. Go to Gradle Scripts > build.gradle(Module: app) and add the following dependency. After adding the dependency you need to click on Sync Now.
dependencies { implementation project (':login') }
Step 4: Create a new activity in the login module
In this step we will create a new activity in our login module, for this, we will go to our module, login > Java > package > right-click > New > Activity > Empty Activity and create a new activity and name it as LoginActivity.
Step 5: Create a layout for the login module
In this step, we will create a layout for our login module. It has two EditText for username and password and a login button. Go to login> res > layout > activity_login.xml and add the following code snippet.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" android:gravity = "center" tools:context = ".LoginActivity" > < EditText android:id = "@+id/username" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:hint = "Enter your username" android:layout_marginTop = "8dp" /> < EditText android:id = "@+id/password" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:hint = "Enter your password" android:layout_marginTop = "8dp" /> < Button android:id = "@+id/loginBtn" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:background = "@color/colorPrimary" android:textColor = "#ffffff" android:layout_marginTop = "8dp" android:text = "Login" /> </ LinearLayout > |
Step 6: Working with LoginActivity.java file
In this step, we will initialize our EditText and Button. On entering the correct username and password a Toast message will be displayed saying “Modular Architecture Works Fine”.
Java
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class LoginActivity extends AppCompatActivity { private EditText userName; private EditText passWord; private Button loginBtn; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_login); // initializing EditText and Button userName = findViewById(R.id.username); passWord = findViewById(R.id.password); loginBtn = findViewById(R.id.loginBtn); loginBtn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // for testing purpose we are using username as gfg // and password as neveropen. // On successful login it will display a toast message if (userName.getText().toString().equals( "gfg" ) && passWord.getText().toString().equals( "neveropen" )){ Toast.makeText(LoginActivity. this , "Modular Architecture Works Fine" , Toast.LENGTH_SHORT).show(); } } }); } } |
Step 7: Working with activity_main.xml file
This is our main application(GFG Modular Architecture) layout file. Here, we just add a button.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" android:gravity = "center" tools:context = ".MainActivity" > < Button android:id = "@+id/modularTextView" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Modular Architecture" app:layout_constraintBottom_toBottomOf = "parent" app:layout_constraintLeft_toLeftOf = "parent" app:layout_constraintRight_toRightOf = "parent" app:layout_constraintTop_toTopOf = "parent" /> </ LinearLayout > |
Step 8: Working with MainActivity.java file
In this step, we will initialize the button of our main application. On click of which it will redirect us to the login module. First, we initialize the button and then add a listener to it.
Java
package com.paulsofts.gfgmodulararchitecture; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; import com.paulsofts.login.LoginActivity; public class MainActivity extends AppCompatActivity { private TextView modularText; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // initializing the button modularText = findViewById(R.id.modularTextView); // adding listener to the button modularText.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { startActivity( new Intent(MainActivity. this , LoginActivity. class )); } }); } } |
Output: