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