Skip to main content

MVC pattern in Android

This is an example of implementation MVC pattern in android development.

Calculator application

- MainActivity is the application execution, there is only CalculatorActivity, which is responsible for the GUI, event handling and display of visual objects.
- JModel: maintains state of the calculator.
- JView: display of model state, the state of the calculator.
- JController: receives user input via button onClick events, sends user input to Model, and sends "Model state" to myView.











Class JController:

package com.example.mvcpatternandroid;

import com.example.mvcpatternandroid.R;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;

/**
 * copyright (c) jbohn
 */
public class JController implements OnClickListener {

    JModel myModel = null;
    JView myView = null;

    public JController(final JModel myModel, final JView myView) 
    {
        this.myModel = myModel;
        this.myView = myView;
        this.myView.registerListener(this);
    }

    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.button0:
            myModel.setModel('0');
            break;
        case R.id.button1:
            myModel.setModel('1');
            break;
        case R.id.button2:
            myModel.setModel('2');
            break;
        case R.id.button3:
            myModel.setModel('3');
            break;
        case R.id.button4:
            myModel.setModel('4');
            break;
        case R.id.button5:
            myModel.setModel('5');
            break;
        case R.id.button6:
            myModel.setModel('6');
            break;
        case R.id.button7:
            myModel.setModel('7');
            break;
        case R.id.button8:
            myModel.setModel('8');
            break;
        case R.id.button9:
            myModel.setModel('9');
            break;
        case R.id.buttonPlus:
            myModel.setModel('+');
            break;
        case R.id.buttonDivide:
            myModel.setModel('/');
            break;
        case R.id.buttonMinus:
            myModel.setModel('-');
            break;
        case R.id.buttonTimes:
            myModel.setModel('*');
            break;
        case R.id.buttonC:
            myModel.setModel('C');
            break;
        case R.id.buttonEqual:
            myModel.setModel('=');
            break;
        }
        myView.setView(myModel.getModel());
    }
}
 
* Class JView 
 
package com.example.mvcpatternandroid;

import com.example.mvcpatternandroid.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;


/**
 * @author QuocTrinh
 * copyright (c) jbohn
 */
public class JView extends LinearLayout {
    private EditText txtDisplay;
    LayoutInflater layoutInflater;
    public JView(Activity activity) 
    {
        super(activity, null);
        
        this.setOrientation(VERTICAL);
        
        getInFlater(activity);
        
        getTxtView();
    }


    private void getInFlater(Activity activity) 
    {
        layoutInflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    private void getTxtView() {
        layoutInflater.inflate(R.layout.display, this);
        txtDisplay = (EditText) findViewById(R.id.editString);
    }

    public void setView(String s) {
        txtDisplay.setText(s);
    }
    
    public void registerListener(OnClickListener onClickListener) {
        
        View view = (View) layoutInflater.inflate(R.layout.keyboard, this);
        
        final Button button0 = (Button) view.findViewById(R.id.button0);
        final Button button1 = (Button) view.findViewById(R.id.button1);
        final Button button2 = (Button) view.findViewById(R.id.button2);
        final Button button3 = (Button) view.findViewById(R.id.button3);
        final Button button4 = (Button) view.findViewById(R.id.button4);
        final Button button5 = (Button) view.findViewById(R.id.button5);
        final Button button6 = (Button) view.findViewById(R.id.button6);
        final Button button7 = (Button) view.findViewById(R.id.button7);
        final Button button8 = (Button) view.findViewById(R.id.button8);
        final Button button9 = (Button) view.findViewById(R.id.button9);
        final Button buttonC = (Button) view.findViewById(R.id.buttonC);
        final Button buttonPlus = (Button) view.findViewById(R.id.buttonPlus);
        final Button buttonEqual = (Button) view.findViewById(R.id.buttonEqual);
        final Button buttonMinus = (Button) view.findViewById(R.id.buttonMinus);
        final Button buttonTimes = (Button) view.findViewById(R.id.buttonTimes);
        final Button buttonDivide = (Button) view
                .findViewById(R.id.buttonDivide);

        button0.setOnClickListener(onClickListener);
        button1.setOnClickListener(onClickListener);
        button2.setOnClickListener(onClickListener);
        button3.setOnClickListener(onClickListener);
        button4.setOnClickListener(onClickListener);
        button5.setOnClickListener(onClickListener);
        button6.setOnClickListener(onClickListener);
        button7.setOnClickListener(onClickListener);
        button8.setOnClickListener(onClickListener);
        button9.setOnClickListener(onClickListener);
        buttonC.setOnClickListener(onClickListener);
        buttonPlus.setOnClickListener(onClickListener);
        buttonEqual.setOnClickListener(onClickListener);
        buttonMinus.setOnClickListener(onClickListener);
        buttonTimes.setOnClickListener(onClickListener);
        buttonDivide.setOnClickListener(onClickListener);
    }

} 

* Class JModel
package com.example.mvcpatternandroid;

/**
 * @author QuocTrinh
 * copyright (c) jbohn
 */
public class JModel {
    private double accumulator, opr;
    private char op;

    public JModel() {
        accumulator = 0.0;
    }

    public void setModel(char c) {
        switch (c) {
        case 'C':
            opr = 0.0;
            accumulator = 0.0;
            break;
        case '+':
            ;
        case '-':
            ;
        case '*':
            ;
        case '/':
            op = c;
            opr = accumulator;
            accumulator = 0.0;
            break;
        case '=':
            switch (op) {
            case '+':
                accumulator = opr + accumulator;
                break;
            case '-':
                accumulator = opr - accumulator;
                break;
            case '*':
                accumulator = opr * accumulator;
                break;
            case '/':
                accumulator = opr / accumulator;
                break;
            default:
            }
            break;
        default: // Assume '0'..'9' digit
            accumulator = accumulator * 10.0 + (c - '0');
            break;
        }
    }

    public String getModel() {
        return accumulator + "";
    }
}
 
Download Source code (http://jbohn.blogspot.com/)
 

Comments

  1. any chance of looking at the complete source for this MVC example?
    regards

    ReplyDelete
  2. this is download link: https://learn-java-together.googlecode.com/files/MVCPatternAndroid.7z


    Actually I posted source at the end of this entry, u can use a reader QR app to get it.

    BR

    ReplyDelete

Post a Comment

Popular posts from this blog

How to Install SQL Server on MacOS with docker

 I'm writing a small tut for who need to install SQL Server on macOS using docker Step 1: Download the SQL Server Image sudo docker pull mcr.microsoft.com/mssql/server:2019-latest Step 2: Launch the SQL Server Image in Docker docker run -d --name example_sql_server -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Pass.word-123' -p 1433:1433 mcr.microsoft.com/mssql/server:2019-latest Step 3: Check the SQL Server Docker Container docker ps -a Step 4: Install SQL Server Command-Line Tool sudo npm install -g sql-cli Step 5: Connect to SQL Server  5.1 Using Command mssql -u sa -p Pass.word-123 5.2: Using VSCode to connect to sql server Using the extension SQL Server (mssql)

Create Grid using React native

After fist post about react native I feel I'm getting familiar with react native, this framework quite easy to use, today I continue with another tutorial about create a simple grid card in react native. Let see the our final result first:                             Let's start... Analytics Assump our data has below structure: We have an array of rows and each row has an array of columns, then I decided to use FlatList to display object in cloumns array in horizontal direction, you can imaging like below image. Gridcard design Card Item each card has a logo and a text. I'm using the Icon from lib react-native-vector-icons To have a flatlist in horizontal we use  horizontal = {true} property, and to make sure the width of flatlist is same as screen with we use  contentContainerStyle={{ flexGrow: 1, justifyContent: 'center' }} Grid Now the implementation quite easy now, just need to...