Note that there are some explanatory texts on larger screens.

plurals
  1. POOff-center alignment of text inside a TextView
    text
    copied!<p>I have a layout/alignment problem with a TextView which I haven't yet been able to find a solution for, that is, I want to align text off-center inside a TextView horizontally.</p> <p>To give a little more context, I have an ImageView and a TextView side by side, the ImageView touching the left edge of the screen and the TextView filling the rest of the screen horizontally, like this: </p> <pre><code>[-img-|-----text-----] </code></pre> <p>The TextView is configured as singleLine="true" and maxLines="1", so that it will be truncated if its too long for its horizontal space. My aim is to align the text in the center of the screen, not the center of the TextView, because there are other elements on the screen that are aligned to the center and I need the text aligment to match that.</p> <p>So, if I use gravity="center" on the TextView, I get the image above, but what I actually want is</p> <pre><code>[-img-|--text--------] </code></pre> <p>I tried putting image and text in a RelativeLayout, so that the TextView actually touches both edges of the screen, which does what I want with respect to the alignment, except that if the text is long enough, the first characters will be hidden by the image, since the TextView lies behind the image view.</p> <p>I also experimented with margins, padding and a compound drawable to the left, but the text is always centered relative to the available space (which I would consider the expected behavior).</p> <p>Does anybody have any clues on how to achieve this alignment, i.e. relative to the center of a different component than the TextView, maybe programmatically at runtime? Thanks in advance for any helpful advice.</p> <p><strong>Edit:</strong> user Budius suggested using padding to the right to achieve a centered alignment which works but leads to long texts being truncated before the right edge of the TextView is reached and I'm looking for a solution that avoids that, i.e. that uses the entire avilable space, if possible.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload