title: how to put text on image bitmap at bottom?
body: <p><strong>What I Want:</strong> I want to add text at bottom of a image which I have chose from either gallery or camera.</p>
  
  <p><strong>Original Image</strong></p>
  
  <p><a href="https://i.stack.imgur.com/ojfwX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ojfwX.jpg" alt="Original Image"></a></p>
  
  <p><strong>I added blue color strip to image at bottom</strong></p>
  
  <p><a href="https://i.stack.imgur.com/b7qbb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b7qbb.png" alt="I added little strip to image at bottom"></a></p>
  
  <p>In that strip, I want to add some text exactly in middle.</p>
  
  <p><strong>What's the Problem:</strong>  </p>
  
  <ol>
  <li>I'm unable to position text exactly in middle of blue color strip.</li>
  <li>For different images, text size changes. Some time it is very small, some time it is very big.</li>
  </ol>
  
  <p><strong>What I Tried:</strong> My code is like below.</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>    public class MainActivity extends AppCompatActivity {
  
      private ImageView mImageView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          mImageView = findViewById(R.id.imageView);
      }
  
      public void openGallery(View view) {
          Intent intent = new Intent();
          intent.setType("image/*");
          intent.setAction(Intent.ACTION_GET_CONTENT);
          startActivityForResult(Intent.createChooser(intent, "Select Picture"), 100);
      }
  
      public void openCamera(View view) {
          Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          startActivityForResult(intent, 101);
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          if (data != null &amp;&amp; resultCode == Activity.RESULT_OK) {
  
              if (requestCode == 100) {
  
                  Bitmap bitmap = null;
                  try {
                      bitmap = MediaStore.Images.Media
                              .getBitmap(getApplicationContext().getContentResolver(), data.getData());
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
  
                  addStampToImage(bitmap);
  
              } else if (requestCode == 101) {
  
                  Bitmap bitmap = (Bitmap) data.getExtras().get("data");
                  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                  bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
                  File destination = new File(Environment.getExternalStorageDirectory(),
                          System.currentTimeMillis() + ".jpg");
                  FileOutputStream fo;
                  try {
                      destination.createNewFile();
                      fo = new FileOutputStream(destination);
                      fo.write(bytes.toByteArray());
                      fo.close();
                  } catch (FileNotFoundException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
  
                  addStampToImage(bitmap);
              }
          }
      }
  
      private void addStampToImage(Bitmap originalBitmap) {
  
          int extraHeight = (int) (originalBitmap.getHeight() * 0.15);
  
          Bitmap newBitmap = Bitmap.createBitmap(originalBitmap.getWidth(),
                  originalBitmap.getHeight() + extraHeight, Bitmap.Config.ARGB_8888);
  
          Canvas canvas = new Canvas(newBitmap);
          canvas.drawColor(Color.BLUE);
          canvas.drawBitmap(originalBitmap, 0, 0, null);
  
          Resources resources = getResources();
          float scale = resources.getDisplayMetrics().density;
  
          Paint pText = new Paint();
          pText.setColor(Color.WHITE);
          pText.setTextSize((int) (20 * scale));
  
          String text = "Maulik";
  
          /*Rect r = new Rect();
          canvas.getClipBounds(r);
          int cHeight = r.height();
          int cWidth = r.width();
          pText.setTextAlign(Paint.Align.LEFT);
          pText.getTextBounds(text, 0, text.length(), r);
          float x = -r.left;
          float y = cHeight / 2f + r.height() / 2f - r.bottom;
  
          int minusSpace = (int) (canvas.getClipBounds().bottom * 0.07);
  
          canvas.drawText(text, 0, canvas.getClipBounds().bottom - minusSpace, pText);*/
  
          Rect bounds = new Rect();
          pText.getTextBounds(text, 0, text.length(), bounds);
          int x = (newBitmap.getWidth() - bounds.width())/6;
          int y = (newBitmap.getHeight() + bounds.height())/5;
  
          canvas.drawText(text, x * scale, y * scale, pText);
  
          mImageView.setImageBitmap(newBitmap);
      }
  }
  </code></pre>
  
  <p>Any help will be appreciated!</p>
  
  <p><strong>Updated: 1st Aug 2018</strong></p>
  
  <p>Changes in <strong>addStampToImage</strong> method.</p>
  
  <pre><code>int extraHeight = (int) (originalBitmap.getHeight() * 0.15);
  
  Rect textHeightWidth = new Rect();
  pText.getTextBounds(fromWhichMode, 0, fromWhichMode.length(), textHeightWidth);
  
  canvas.drawText(textToStamp, (canvas.getWidth() / 2) - (textHeightWidth.width() / 2),
                  originalBitmap.getHeight() + (extraHeight / 2) + (textHeightWidth.height() / 2),
                  pText);
  </code></pre>
  
  <p>Above changes giving me text in middle of blue strip. But core <strong>ISSUE</strong> remains same. i.e Text size changes with respect to different image sizes.</p>