aspose file tools*
The moose likes Android and the fly likes custom view Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "custom view" Watch "custom view" New topic
Author

custom view

chen young
Ranch Hand

Joined: Sep 09, 2005
Posts: 187
Hi,

Hi All,

I created a custom view and initialized it in the activity onCreate method.

Is there any difference between those two CustView init declarations ?

Thanks
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369

When you say CustView custView = (CustView)findViewById(R.id.cust); you are trying to get a handle on the CustView object that was inflated(created) by parsing the layout XML file, when the View hierarchy was created. The constructor of the CustView will be invoked when you set contentView for the activity using the line: setContentView(R.layout.main);

When you say: CustView custView = new CustView (this); you are creating the a "new" view object in code and getting its reference in traditional Java-way.

This way you have more control on the object creation: you will be invoking a single arguement constructor of the view class. However, when the view is inflated from the layout XML a different constructor of the View will be called.(Probably the two arguement or the three arguement constructor). You can check this by writing a simple code.

Also, note that both the references will not be same. That is to say, if you have something like :


you will see "false" printed in Logcat.

For more details read Official Documentaion on User Interface

Hope that helps..


[List of FAQs] | [Android FAQ] | [Samuh Varta]
chen young
Ranch Hand

Joined: Sep 09, 2005
Posts: 187
Thanks,

This is very helpful

One more question,
I notice that if I will initialize any variable (for example Paint) in the CustView constructor he will be null when calling the onDraw function.
Why is that ?
Would it be better to initialize those variables only once in the constructor instead of using the onDraw ?

Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369

Few things to note first:
1. Though onDraw(Canvas) method is a callback which is called whenever you view is invalidated, it is nevertheless an instance method.
2. Instance methods can be called by the JVM or by the programmer only on some instance of class. This means that if onDraw() was called, the instance of your CustView WAS created.

If an instance of CustView was created then the constructor must have run. If the constructor has run your Paint object should have been initialized.

As already pointed out in my first post, different constructors of the view are called depending on how the view has been created. Please check if your constructor is being invoked. Usually, I put all my initialization code in one function and call this function from every constructor of the view.

If this is not the problem or doesnt help solve it, please post your code.
chen young
Ranch Hand

Joined: Sep 09, 2005
Posts: 187
Hi,

I didn’t initialize the variables via all constructors that’s way it was null

Thanks for the help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: custom view
 
Similar Threads
Cannot find the problem in the code
[Resolved] Application crashes on starting second Activity
ActivityNotFoundException while using implicit intent
Screen changing
Get byte[] of image