title: padding a hex string in java
body: <p>i am trying to build a hex string to be used as a hex code for a color background. i have the conversion done, but there is an issue when the "converted" string is less than 16--because the the number is only one digit long. you will see in my code that i tried making a method that check id the string is to short, then pads a 0 in front if it is...but i can't get it to ever do the padding. so i end up still with a string of length 1.</p>
  
  <pre><code>package com.example.android.test;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.NumberPicker;
  import android.widget.NumberPicker.OnValueChangeListener;
  
  public class MainActivity extends Activity {
  
  static final int MIN_VAL = 0;
  static final int MAX_VAL = 255;
  NumberPicker alpha, red, green, blue;
  View view;
  String redVal = "00", blueVal = "00", greenVal = "00", alphaVal = "FF";
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      alpha = (NumberPicker) findViewById(R.id.alphaPicker);
      red = (NumberPicker) findViewById(R.id.redPicker);
      green = (NumberPicker) findViewById(R.id.greenPicker);
      blue = (NumberPicker) findViewById(R.id.bluePicker);
  
      alpha.setMinValue(MIN_VAL);
      alpha.setMaxValue(MAX_VAL);
      alpha.setWrapSelectorWheel(false);
  
      red.setMinValue(MIN_VAL);
      red.setMaxValue(MAX_VAL);
      red.setWrapSelectorWheel(false);
  
      green.setMinValue(MIN_VAL);
      green.setMaxValue(MAX_VAL);
      green.setWrapSelectorWheel(false);
  
      blue.setMinValue(MIN_VAL);
      blue.setMaxValue(MAX_VAL);
      blue.setWrapSelectorWheel(false);
  
      view = findViewById(R.id.color_box);
      view.setBackgroundColor(0xFF808000);
  
      alpha.setOnValueChangedListener(new OnValueChangeListener() {
  
          @Override
          public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
              // TODO Auto-generated method stub
  
              alphaVal = Integer.toHexString(newVal);
              pad(alphaVal);
              String color = generateColor(alphaVal, redVal, greenVal, blueVal);
  //                view.setBackgroundColor(color);
              Log.v(color, "was selected");
          }
      });
  
      red.setOnValueChangedListener(new OnValueChangeListener() {
  
          @Override
          public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
  
  
              redVal = Integer.toHexString(newVal);
              pad(redVal);
  
  
              String color = generateColor(alphaVal, redVal, greenVal, blueVal);
  //                view.setBackgroundColor(color);
              Log.v(redVal, "was selected");
          }
      });
  
  }
  //  if the integer is less than 16, you need to convert the int to a hex string and pad a 0 in
  front
  private static final String pad(String s) {
  
      if(s.length() &lt; 2){
          StringBuilder theColor = new StringBuilder("0")
                  .append(s);
          return theColor.toString();
      }
      return s;
  }
  
  public String generateColor(String alphaVal, String redVal, String greenVal, String blueVal) {
  
  
      StringBuilder theColor = new StringBuilder("0x")
              .append(alphaVal)
              .append(redVal)
              .append(greenVal)
              .append(blueVal);
      return theColor.toString();
  }
  }
  </code></pre>