Issue
I was trying to add a text view to a LinearLayout
programmatically using the following:
Resources r = getResources ();
TextView text = new TextView (this);
text.setText (R.string.no_passwords);
Log.d ("My App", r.getString (R.string.no_passwords));
text.setTextSize (r.getDimension (R.dimen.prompt_size));
Log.d ("My App", "Text size: " + r.getDimension (R.dimen.prompt_size));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
parent.setGravity (Gravity.CENTER);
text.setLayoutParams (params);
parent.addView (text);
Explanation:
parent
is the LinearLayout
with the id parent_layout
(You will see this in the code below). I am logging with the tag "My App" because I am using a custom filter to log stuff.
But the view is not added. I looked at the log message and see something relly weird:
10-17 09:33:26.764 11877-11877/com.passwordgen D/My App﹕ [ 10-17 09:33:26.764 11877:11877 D/My App ]
Text size: 43.132
R.string.no_password
should be "You have not saved any passwords yet" and R.dimen.prompt_size
should be 9pt.
Quuestions:
Why the first log message isn't showing the right text but [ 10-17 09:33:26.764 11877:11877 D/My App ]
? And why the second message is not prefixed by the tag "My App"?
Solution
You will need to call invalidate()
to make the view be redrawn. Also it is recommended you do this via an xml file vs. in code.
Answered By - Colin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.