title: Android set TextView text size to look the same on all screen sizes
body: <p>I have a square LinearLayout containing a few other <code>LinearLayouts</code> with <code>ImageViews</code> and <code>TextViews</code> inside. I am saving the layout content as png file after the user changes it. All the Images and Texts are being arranged using the general gravity and layout_gravity values. Here is a small sample:</p>
  <p><img src="https://i.stack.imgur.com/QTPJF.png" alt="enter image description here" /></p>
  <p>Here is my problem: I want the SAMPLE TEXT to look exactly the same size on all screen sizes, being a phone of 480p or a tablet of 1200p. Initially I thought that setting the text size in <code>SP</code> will be enought but it is not, because as screen resolution gets bigger, the text gets smaller.</p>
  <p>So my question is, how can I programatically set the text size so it will look exactly of the same size, independent of screen width. As you can see the parent layout will always be a square.</p>
  <p>I tried programatically setting SP or using a scale factor.</p>
  <pre><code>float scale = 0;
  
  if (screenWidth == 720) {
          scale = 1;
  } else {
          scale = (720 - screenWidth) * 100.2f / screenWidth;
  }
  
  float defaultSizeFor720 = 35f;
  
  textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, defaultSizeFor720 * scale);
  </code></pre>
  <p>It does not work. as screen gets bigger, text gets smaller, not as small as when only using SP alone, but still. I also tried most of the answers from other questions but still did not work.
  Any ideas?</p>