title: Call a method in android - practical examples
body: <p>I'm new to Android / Java. I want to modify a simple open source Android game. It's much easier for me to learn programming with real examples.</p>
  
  <p><strong>Goal</strong>: I want to generate random backgrounds when the game is going to the next level or when the player is starting a new game.</p>
  
  <p><strong>Already:</strong> I found a way to generate random backgrounds when the user is starting the application, replacing the following code:</p>
  
  <pre><code>  mBackgroundOrig =
              BitmapFactory.decodeResource(res, R.drawable.background, options);
  </code></pre>
  
  <p>with:</p>
  
  <pre><code>      TypedArray imgs = getResources().obtainTypedArray(R.array.random_background);
        Random rand = new Random();
        int rndInt = rand.nextInt(imgs.length());
        int resID = imgs.getResourceId(rndInt, 0);
        mBackgroundOrig = BitmapFactory.decodeResource(res, randBackground.resID, options);
  </code></pre>
  
  <p>in: <a href="https://code.google.com/p/bubble-shoot/source/browse/trunk/bubble-shooter-pro/src/com/likeapp/game/bubbleshooter/GameView.java" rel="nofollow">https://code.google.com/p/bubble-shoot/source/browse/trunk/bubble-shooter-pro/src/com/likeapp/game/bubbleshooter/GameView.java</a></p>
  
  <p>And create a XML file with a string array in <strong>values/rand_bkgnd.xml</strong>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;string-array name="random_background"&gt;
          &lt;item name="background_01"&gt;@drawable/background01&lt;/item&gt;
          &lt;item name="background_02"&gt;@drawable/background02&lt;/item&gt;
          &lt;item name="background_03"&gt;@drawable/background03&lt;/item&gt;
          &lt;item name="background_04"&gt;@drawable/background04&lt;/item&gt;
          &lt;item name="background_05"&gt;@drawable/background05&lt;/item&gt;
          &lt;item name="background_06"&gt;@drawable/background06&lt;/item&gt;
          &lt;item name="background_07"&gt;@drawable/background07&lt;/item&gt;
          &lt;item name="background_08"&gt;@drawable/background08&lt;/item&gt;
          &lt;item name="background_09"&gt;@drawable/background09&lt;/item&gt;
          &lt;item name="background_10"&gt;@drawable/background10&lt;/item&gt;
      &lt;/string-array&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p><strong>Request:</strong> Please help me to create a method with the above randomize background code. I'd like to have this code in a separate java file and to be able to call it from <strong>goToNextLevel()</strong> method when the player is finishing one level and is going to the next level:</p>
  
  <pre><code>public void goToNextLevel() {
      SharedPreferences sp =this.mContext.getSharedPreferences(
                 BubbleShooterActivity.PREFS_NAME, Context.MODE_PRIVATE);
      currentLevel = sp.getInt(BubbleShooterActivity.PREFS_LEVEL_KEY_NAME, 0);
      int maxLevel = sp.getInt(BubbleShooterActivity.PREFS_UNLOCK_LEVEL_KEY_NAME, 0);     
      currentLevel++;
      if(maxLevel&lt;=currentLevel){
          maxLevel=currentLevel;
      }
      sp.edit().putInt(BubbleShooterActivity.PREFS_LEVEL_KEY_NAME, currentLevel).putInt(BubbleShooterActivity.PREFS_UNLOCK_LEVEL_KEY_NAME, maxLevel).commit();
      if (currentLevel &gt;= MAX_LEVEL_NUM) {
          currentLevel = 0;
      }
  }
  </code></pre>
  
  <p>in: <a href="https://code.google.com/p/bubble-shoot/source/browse/trunk/bubble-shooter-pro/src/com/likeapp/game/bubbleshooter/LevelManager.java" rel="nofollow">https://code.google.com/p/bubble-shoot/source/browse/trunk/bubble-shooter-pro/src/com/likeapp/game/bubbleshooter/LevelManager.java</a></p>
  
  <p>I'm assuming this must be very easy for somebody with at least average Java skills. Please provide me examples based on my code / game and step by step instructions or explanation.</p>