Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use AttributeSet in constructor

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the API we see constructors like



Which looks like it ought to provide a way to create a TextView with a set of attributes taken from some XML resource.

However, I can't figure out exactly how to do that. The API just seems to lead me in circles and the book I am using is not helping.

Does anybody know of a good tutorial that could help?

Bill
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create a custom View(by extending the View class) or a container(by extending some Layout class), and use your custom class directly in the layout XMLs, your implementation will be required to have that constructor because, by default, MyView( Context c, AttributeSet a ); would be constructor that would be invoked by the system. And any attributes that you set in the XML for your custom View will be passed in the AttributeSet instance.

You can test this by extending a simple View and having your custom widget in the layout XML.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Monu, I can see that I had the wrong idea about how AttributeSet is used and that article looks helpful.

Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic