Sunday, 9 February 2014

Post 14: Global Variables in Android

Today I want to show you how to implement global varaibles in Android. While in common JAVA programs (or any other OOP language for that matter) you can use MVC without any problems, Android doesn't support MVC for some reason or another.

I've seen some implementation of MVC in Android, though they are way too complicated and are kinda overkill if you just want to save a few variables. That's why this post is exactly focused on that.

As usual I'll implement a small application. First I'll explain to you what this app is about, then how to create it with Eclipse and then I'll show you the most important code snippets. Last but not least I'll post the GitHub link to the full code of this app.

App


The feature of this app is a simple counter. It has two slides and it should demonstrate that you can access values that the other slide has changed.

One slide is the MainActiviy, the other is the NextActivty. On the MainActivity you have two buttons: One is the "Add" button and the other is "Next" button. Further more, on the top left hand side you can see a TextView with a number (see below).


With the "Add" button you increase the number. By pressing the "Next" button a new Slide appears and shows the number of times you pressed the "Add" button in another TextView (again on the top left hand side). This slide has only one button: A "Delete" button. With this button you can reset the counter to zero again (see below).

If you press the return button and press the "Add" button in the MainActivity, you'd see that it counts from 1 again.

In order to achieve this, we use basically three classes: the MainActivity-, the NextActivity- and the GlobalVariable-class. These classes are interconnected as follows:

The GlobalVariable class contains a private attribute and some methods

Create

The GlobalVariable class extends Application and contains besides the private Attribute counter some basic methods:

import android.app.Application;

public class GlobalVariable extends Application{

 private int counter;
 
 public int getCounter(){
  return this.counter;
 }
 
 public void setCounter(int newCounter){
  this.counter=newCounter;
 }
 
 public void addCounter(){
  this.counter+=1;
 }
 
 public void resetCounter(){
  this.counter=0;
 }
 
}



The MainActivity class:

 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  textViewMain=(TextView)findViewById(R.id.tvMain);
  buttonAdd=(Button)findViewById(R.id.bAdd);
  buttonNext=(Button)findViewById(R.id.bNext);
  
  addListenerOnButton();
 }

 private void addListenerOnButton() {
  buttonAdd.setOnClickListener(new OnClickListener(){
   
   @Override
   public void onClick(View arg0) {
    final GlobalVariable myVariable=(GlobalVariable)getApplicationContext();
    myVariable.addCounter();

    String newCounter=String.valueOf(myVariable.getCounter());
    textViewMain.setText(newCounter);
   }   
  });
 }


The NextActivity class:

 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_next);
  
  buttonDelete=(Button)findViewById(R.id.bDelete);
  textViewNext=(TextView)findViewById(R.id.tvNext);

  final GlobalVariable myVariable=(GlobalVariable)getApplicationContext();
  String newCounter=String.valueOf(myVariable.getCounter());
  textViewNext.setText(newCounter);
  
  addListenerOnButton();
 }

 private void addListenerOnButton() {
  buttonDelete.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    final GlobalVariable myVariable=(GlobalVariable)getApplicationContext();

    myVariable.resetCounter();
    String newCounter=String.valueOf(myVariable.getCounter());
    textViewNext.setText(newCounter);
   }
   
  });
 }


Very important you have to add 'android:name="GlobalVariable" ' in the Manifest.xml, like so:
 
You can find the whole code here:
GitHub link to the full code.

Tweet