title: center text in TextView programmatically
body: <p>I created a TextView programmatically with a text and background drawable. I want to center the text in the TextView but the text was centered on the top.</p>
  
  <p><img src="https://i.stack.imgur.com/a4yuL.png" alt="enter image description here"></p>
  
  <p>here's my code in centering the text in the TextView:</p>
  
  <pre><code>protected class TileView extends TextView{
      private int tileType;
      private int col;
      private int row;
  
  protected TileView(Context context, int row, int col, int tileType) {
  
      super(context);
      this.col = col;
      this.row = row;
      this.tileType = tileType;
  
      String result = Integer.toString(RandomNumber(1,9));            
      Drawable image = getResources().getDrawable(tile_id[tileType]);
      setBackgroundDrawable(image);
      setClickable(true);
      setText(result);        
      setTextAppearance(context, R.style.boldText);           
      setOnClickListener(GameView.this);
  }
  }
  </code></pre>
  
  <p>here's my code in onLayout()</p>
  
  <pre><code>int left = oneFifth * tileView.getCol();
  int top = oneFifth * tileView.getRow();
  int right = oneFifth * tileView.getCol() + oneFifth;
  int bottom = oneFifth * tileView.getRow() + oneFifth;
  tileView.layout(left, top, right, bottom);
  tileView.setGravity(Gravity.CENTER);
  </code></pre>
  
  <p>How can I center the text in the TextView?</p>