0

I noticed, that a TextView with the same textSize can have a different height depending on the String it is displaying.

I disscovered it when paiting a simple TableLayout which has some TableRow items with some cells, each cell is a TextView.

Those cells doesn't have top margin, and they are being painted perfectly in Android 8.0 and upper devices, but in Android 7.0 and lower versions, some of the cells are adding a top margin between them, and this is because the height of the TextView is different. I can't understand how is that happening, it's very frustrating.

In this case, it's happening in the TextView called player and only when the name is too much long and it's using ellipsize to truncate it.

The code is very simple:

public void populateGoalscorersTable(ArrayList<Player> players){
    goalscorersTableLayout.removeAllViews();

    for (int i=0; i<players.size() && i<30; i++){
        TableRow row = new TableRow(this);
        row.setLayoutParams(goalscorersRowParams);

        TextView position = new TextView(this);
        position.setGravity(Gravity.CENTER);
        position.setLayoutParams(goalscorersPositionParams);
        position.setText(""+(i+1));
        position.setBackgroundResource(R.drawable.table_cell_background);
        position.setMinHeight((int) getResources().getDimension(R.dimen.cell_height));
        row.addView(position);

        TextView player = new TextView(this);
        player.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
        player.setLayoutParams(goalscorersPlayerParams);
        player.setText(players.get(i).getName());
        player.setBackgroundResource(R.drawable.table_cell_background);
        player.setMaxLines(1);
        player.setEllipsize(TextUtils.TruncateAt.END);
        player.setPadding((int) getResources().getDimension(R.dimen.spacing_small),0,(int) getResources().getDimension(R.dimen.spacing_small),0);
        player.setMinHeight((int) getResources().getDimension(R.dimen.cell_height));
        row.addView(player);

        TextView team = new TextView(this);
        team.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
        team.setLayoutParams(goalscorersTeamParams);
        team.setText(players.get(i).getTeam().getName());
        team.setBackgroundResource(R.drawable.table_cell_background);
        team.setMaxLines(1);
        team.setEllipsize(TextUtils.TruncateAt.END);
        team.setPadding((int) getResources().getDimension(R.dimen.spacing_small),0, (int) getResources().getDimension(R.dimen.spacing_small),0);
        team.setMinHeight((int) getResources().getDimension(R.dimen.cell_height));
        row.addView(team);

        TextView goals = new TextView(this);
        goals.setGravity(Gravity.CENTER);
        goals.setLayoutParams(goalscorersPositionParams);
        goals.setText(""+players.get(i).getLeagueGoals());
        goals.setBackgroundResource(R.drawable.table_cell_background);
        goals.setMinHeight((int) getResources().getDimension(R.dimen.cell_height));
        row.addView(goals);

        if (players.get(i).getTeam() == Manager.getInstance().getTeam()){
            player.setTextColor(getResources().getColor(R.color.player_team));
            player.setTypeface(null, Typeface.BOLD);

            team.setTextColor(getResources().getColor(R.color.player_team));
            team.setTypeface(null, Typeface.BOLD);
        }

        goalscorersTableLayout.addView(row);
    }
}

It's not the only way to make this errorneal top margin appearing (caused by a incorrect text height), I can see it for example when i have a row whith TextView cells with only one character and others with more than one character. The TextViews with more characters get's the margin. Also i can see this error happening when some TextViews are aligned to left and others centered. Very frustrating.

How can this be avoided?

NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • have you tried https://stackoverflow.com/questions/4768738/android-textview-remove-spacing-and-padding-on-top-and-bottom – Pawel Sep 06 '20 at 12:04
  • @Pawel checking that post I disscovered that if I put android:includeFontPadding to false and maxLines to 1, the problem is solved, but then, the text is not center vertically, is too much aligned to up, so android:includeFontPadding seems to be necessary to get a centered text... – NullPointerException Sep 06 '20 at 15:59

1 Answers1

0

If the font is not monospaced, different text can have different sizes and needs of space for letters that venture down the line such as j or p or q.