Note that there are some explanatory texts on larger screens.

plurals
  1. POProgrammatically centering text in Android's Textview
    primarykey
    data
    text
    <p>I've created a class which extends Android's Text view. I'm trying to to center the text horizontally and vertically. The text appears to center horizontally but not vertically.</p> <p>I've tried both:</p> <pre><code>setGravity(Gravity.CENTER); </code></pre> <p>and</p> <pre><code>setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); </code></pre> <p>My class is below:</p> <pre><code>public class NumberView extends TextView { private Paint circlePaint; private int radius; public NumberView(Context context) { super(context); setText("0"); circlePaint = new Paint(); circlePaint.setColor(Color.BLUE); // setGravity(Gravity.CENTER); setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); setBackgroundColor(Color.rgb(0xf1, 0xf1, 0xf1)); setTypeface(null, Typeface.BOLD); setTextSize(TypedValue.COMPLEX_UNIT_SP, 15); } @Override protected void onDraw(Canvas canvas) { // draw circle at center of canvas super.onDraw(canvas); int width = getWidth(); int height = getHeight(); radius = Math.min(width, height); radius /= 2; canvas.drawCircle(width/2, height/2, radius, circlePaint); //super.onDraw(canvas); } </code></pre> <p>}</p> <p>I'm adding NumberView as part of a ExpandableList View. Down below is how I had the expandable list view to the layout:</p> <pre><code> listView = new ExpandableListView(this); listView.setAdapter(listAdapter); TextView emptyView = new TextView(this); listView.setEmptyView(emptyView); mainLayout.addView(listView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT , 2)); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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