Centralized text in TextView programmatically
I created a TextView programmatically with text and background. I want to center text in a TextView, but the text has been centered at the top.
here's my code in the center of the text in the TextView:
protected class TileView extends TextView{
private int tileType;
private int col;
private int row;
protected TileView(Context context, int row, int col, int tileType) {
super(context);
this.col = col;
this.row = row;
this.tileType = tileType;
String result = Integer.toString(RandomNumber(1,9));
Drawable image = getResources().getDrawable(tile_id[tileType]);
setBackgroundDrawable(image);
setClickable(true);
setText(result);
setTextAppearance(context, R.style.boldText);
setOnClickListener(GameView.this);
}
}
here's my code in onLayout ()
int left = oneFifth * tileView.getCol();
int top = oneFifth * tileView.getRow();
int right = oneFifth * tileView.getCol() + oneFifth;
int bottom = oneFifth * tileView.getRow() + oneFifth;
tileView.layout(left, top, right, bottom);
tileView.setGravity(Gravity.CENTER);
How can I center the text in the TextView?
+3
source to share
4 answers