In my case I have an activity
with a TableLayout
which contains a few views such as a Button
with id \'Number1\' and a TextView
After looking at the few similar occurrences on Stack Overflow like this, I tried a few different things including programmatically setting the text of the Button and also setting its gravity and padding again. None of this worked.
I arrived at this solution by trial and error - Hide and then show the button before and after the offending call to setText. Also, note that you have to set visibility to GONE rather than INVISIBLE. This is possibly forcing the layout engine to create the button again, with the correct alignment.
New event handler shown below.
public void buttonNumberClick(View view) {
TextView passwordText = (TextView) findViewById(R.id.password);
Button sourceButton = (Button)view;
String sourceText = sourceButton.getText().toString();
String text = passwordText.getText().toString();
if (text.length() < 4) {
int number = Integer.parseInt(sourceText);
String newText = text + Integer.toString(number);
// Fix the issue of the button text being mis-aligned after the below call to setText.
sourceButton.setVisibility(View.GONE);
passwordText.setText(newText);
// Fix the issue of the button text being mis-aligned after the above call to setText.
sourceButton.setVisibility(View.VISIBLE);
}
}