title: Porting Java class and methods to Android. (TextLayout, Font, Graphics2D, & more)
body: <p>I've been toying around in Android and attempting to port over a Java app. Below are some questions regarding to issues I've run into and would like some guidance on. </p>
  
  <p>It is a rather large question (multiple questions rather). However, I'm not asking them blindly as I have researched what I could about them and attempted to put my understanding to use. I've put time into asking the questions in hopes that they are half-ways clear on what I'm wanting to achieve.</p>
  
  <p>I'll be offering half of my rep as a bounty since I'm hoping for thorough answers and help, hopefully it will be enough to entice a few people to help.</p>
  
  <p>In advance, thank you for your time and help! (looking forward to reading responses).</p>
  
  <p><strong>TextLayout &amp;&amp; Font &amp;&amp; Graphics2D</strong></p>
  
  <p>In question are the following classes and methods:</p>
  
  <ul>
  <li><code>TextLayout</code></li>
  <li><code>TextLayout.getAdvance()</code></li>
  <li><code>TextLayout.getAscent()</code></li>
  <li><code>TextLayout.draw()</code></li>
  <li><code>Graphics2D.getFontRenderContext()</code></li>
  </ul>
  
  <p>I'm not quite sure what is equivalent of <code>TextLayout</code> in Android. I had read that some make a <code>TextView</code> and use that, but am unsure if that will </p>
  
  <p>work for the following. I'll provide some source of what I'm wanting to do and perhaps one can help me.</p>
  
  <p>Java Source: </p>
  
  <pre><code>private Font myStringFont = new Font("Helvetica", Font.BOLD, 12);
  private String myString = "My Test String";
  private int midX = getWidth() / 2;
  private int midY = getHeight() / 2;
  Graphics2D g2 = new Graphics2d();
  
  TextLayout layout = new TextLayout(myString, font, g2.getFontRenderContext());
  g2.drawString(myString, midX - ((int)layout.getAdvance() /2), midY);
  </code></pre>
  
  <p>Android Replication Attempt:</p>
  
  <pre><code>Canvas canvas;
  Paint paint;
  private String myString = "My Test String";
  private float midX = getWidth() / 2;
  private float midY = getHeight() / 2;
  //Unsure what to do about TextLayout &lt;- this is where I need an alternative
  canvas.drawText(myString, midX - /* whatever my alternative to layout.getAdvance() is */ /2), midY);
  </code></pre>
  
  <p>Im stuck at determining how to create a TextLayout and what to do for the method <code>getAdvance()</code>. I noticed that in <code>Paint.FontMetrics()</code> there are </p>
  
  <p>some possible alternatives, but I don't know if any compare.</p>
  
  <p>I'm also unsure how to deal with the following Java code:</p>
  
  <pre><code>Graphics2D g2 = new Graphics2d();
  private int midX = getWidth() / 2;
  private int midY = getHeight() / 2;
  
  TextLayout layout = new TextLayout(myString, g2.getFont(), g2.getFontRenderContext());
  layout.draw(g2, midX, MidY);
  </code></pre>
  
  <p>Review/Summary of Questions Above:</p>
  
  <ul>
  <li>What is an Android alternative for <code>TextLayout</code>? </li>
  <li>What is equivalent to <code>TextLayout.getAdvance()</code>? (Am I able to use <code>fontMetrics</code> to achieve it?)</li>
  <li>Are there Android equivalents to <code>Graphics2D.getFontRenderContext()</code>?</li>
  <li>Can you provide example source for Android? </li>
  </ul>
  
  <p>This is currently one of my biggest issues with porting Java over to Android. I would be greatful for any help, advice, examples, etc.</p>
  
  <p><strong>Font</strong></p>
  
  <p>Below are the methods I am wanting to replicate that deal with <code>font</code>, <code>textlayout</code>, and <code>graphics2d</code>. The first source is the Java methods and </p>
  
  <p>below it is my attempt to replicate it.</p>
  
  <p>In question are the following classes and methods:</p>
  
  <ul>
  <li><code>Font.deriveFont(float size)</code> Creates a new font objects by replicating the current font object and applying a new style to it</li>
  <li><code>TextLayout.getAdvance()</code> The advance is the distance from the origin to the advance of the rightmost (bottommost) character measuring in the line direction</li>
  <li><code>Graphics2D.setRenderingHint(RenderingHints, RenderingHints)</code> </li>
  <li><code>Graphics2D.getFontRenderContext()</code> Encapsulates application hints such as anti-aliasing and fractional metrics</li>
  </ul>
  
  <p>Java Source:</p>
  
  <pre><code>private String myString = "Print this test statement";
  private int myStringFontSize = 15;
  private Color myStringFontColor = Color.red;
  private Font myStringFont = new Font("Helvetica", Font.BOLD, myStringFontSize);
  private int midX = getWidth() / 2;
  private int midY = getHeight() / 2;
  
  public drawString(Graphics2D g2) {
      g2.setFont(myStringFont.deriveFont(determineFontSize(g2, myString)));
      g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALISING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
      TextLayout layout = new TextLayout(myString, g2.getFont(), g2.getFontRenderContext());
  
      g2.setPaint(myStringFontColor);
      g2.drawString(myString, midX - ((int) layout.getAdvance() / 2), midY);
  }
  
  protected float determinFontSize(Graphics2D g2, String myString) {
      int space = getWidth();
      float fontSize = 1.0f;
      float finalFontSize = fontSize;
  
      while(fontSize &lt; 25) {
          Font font myString.deriveFont(fontSize);
          Textlayout layout = new TextLayout(waitingMessage, font, g2.getFontRenderContext());
  
          if(layout.getAdvance() &gt; space) {
              finalFontSize = fontSize - 2;
              break;
          }
          fontSize++;
      }
      finalFontSize = fontSize - 4;
      return finalFontSize;
  }
  </code></pre>
  
  <p>Android Attempt:</p>
  
  <pre><code>private String myString = "Print this test statement";
  private int myStringFontSize = 15;
  private int myStringFontColor = Color.RED;  //Android uses int rather than Color
  Typeface tf = new Typeface();               //Android uses Typeface rather than Font  
  private float midX = getWidth() / 2;        //Changed to float because drawText requires float
  private float midY = getHeight() / 2;       //changed to float because drawText requires float
  
  
  public drawString(Canvas canvas, Paint paint) {
      tf.create("Helvetica", BOLD);
      paint.setTypeface(tf);
      paint.setTextSize((float) myStringFontSize);
  
      paint.setTextSize(determineFontSize(canvas, myString, paint);
      paint.setAntiAlias(true);
      //NOT SURE WHAT TO DO WITH TextLayout YET
  
      paint.setColor(myStringFontColor);
      canvas.drawText(myString, midX - ((int)layout.getAdvance() / 2), midY, paint);  //Not sure how to deal with layout.getAdvance() just yet    
  
  }
  
  protected float determineFontSize(Canvas canvas, String myString, Paint paint) {
      float fontSize = 1.0f;
      float finalFontSize = fontSize;
      int space = getWidth();
  
      while(fontSize &lt; 25) {
          paint.setTextSize(fontSize);
          //NOT SURE WHAT TO DO ABOUT TextLayout.getAdvance() YET or g2.getFontRenderContext()
  
          if(layout.getAdvance() &gt; space) {
              finalFontSize = fontSize - 2;
              break;
          }
          fontSize++;
      }
      finalFontSize = fontSize - 4;
      return finalFontSize;            
  }
  </code></pre>
  
  <p><strong>Final Questions About The Above Methods:</strong></p>
  
  <ul>
  <li>What alternative do I have for <code>TextLayout.getAdvance()</code>? (Ignore if it has been answered due to the question regarding TextLayout)</li>
  <li>What alternative do I have for <code>Graphics2D.getFontRenderContext()</code>?</li>
  <li>Does my Android source replicate the Java source? If not, what needs to be changed?</li>
  <li>Are there better ways of doing this? If so, how?</li>
  </ul>
  
  <p><strong>Elipse2D.Double(double x, double y, double w, double h)</strong></p>
  
  <p>Is there a way to make a subclass of oval to create something equal to Java Ellipse2D.Double? If so, how would one go about it?</p>
  
  <p><strong>ComponentAdapter &amp;&amp; ComponentEvent</strong></p>
  
  <p>I have these in java because my component is able to be resized, in Android what are the equivalents of these for views? (if any)</p>