1

I'm new to android programming and I'm working on an financial management app. Currently I got into a problem that the setText() method destroys the string format that I pass in.

I have an Account class which has a customary toString method which returns a formatted string of the account information:

public String toString(int colWidth1, int colWidth2, int colWidth3) {
    return String.format("%-" + colWidth1 + "s", myDisplayName) +
            String.format("%-" + colWidth2 + "s", myBalance) + 
            String.format("%-" + colWidth3 + "s", myInterestRate);
}

And in the AccountInfo activity, I have:

// create a table that contains all account information of the current user
    TableLayout accountTable = (TableLayout) findViewById(R.id.tableLayout_account_details);
    List<Account> accountList = accountManager.getAllAccounts(CurrentUser.getCurrentUser().getUserName());

    // for each account, display name, balance and interest rate
    for (int i = 0; i < accountList.size(); i++) {
        Account account = accountList.get(i);
        TableRow accEntry = new TableRow(this);
        Button accButton = new Button(this);
        accButton.setText(account.toString(10, 10, 10));
        Log.i("account info", account.toString(10, 10, 10));

        // button format
        TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(0, -7, 0, -10);
        accButton.setLayoutParams(params);
        accButton.setGravity(Gravity.LEFT);

        // add button to the row
        accEntry.addView(accButton);

        // add row to the table
        accountTable.addView(accEntry);

        // ...more code here for button click listener
    }

Although my logcat prints nicely formatted string as I wanted, the text on button does not line up nicely

(just realized I don't have enough reputations to post a screenshot of my app...)

I've tried to debug for long but still don't have a clue. Any help is deeply appreciated!!!

EDIT: screenshot

enter image description here

AD7six
  • 63,116
  • 12
  • 91
  • 123
TheInvisibleFist
  • 445
  • 1
  • 6
  • 12

2 Answers2

1

My roommate points out the problem! He's awesome!!!

The alignment is off because the font is not monospaced. Simply do

android:typeface="monospace"

in the layout xml and problem solved!!!

TheInvisibleFist
  • 445
  • 1
  • 6
  • 12
0

I want to answer from Android Studio:

Do not concatenate text displayed with setText. Use resource string with placeholders.

When calling TextView#setText:

Never call Number#toString() to format numbers; it will not handle fraction separators and locale-specific digits properly. Consider using String#format with proper format specifications (%d or %f) instead.

Do not pass a string literal (e.g. "Hello") to display text. Hardcoded text can not be properly translated to other languages. Consider using Android resource strings instead.

Do not build messages by concatenating text chunks. Such messages can not be properly translated.