Custom View - How do I set their ID's so that they can be found via findViewById()?

My guess is, that your TextView is not instantiated at this point. Try to trigger your animation outside of the oncreate-method Additionally, your MyView is extended from View and you´re creating a RelativeLayout inside. If you do this, you could also directly extend your class from RelativeLayout and then do something like mRelativeLayout = new MyRelativeLayout(context) mRelativeLayout.

FindViewById(R.id. Textview1) could also be the problem, because you haven´t got the reference to your RelativeLayout in your Activity. Although it´s more likely that my first guess is the solution Oh, and you could simply design your RelativeLayout in xml.

I see no real point in using a programmatically Layout here.

My guess is, that your TextView is not instantiated at this point. Try to trigger your animation outside of the oncreate-method. Additionally, your MyView is extended from View and you´re creating a RelativeLayout inside.

If you do this, you could also directly extend your class from RelativeLayout and then do something like mRelativeLayout = new MyRelativeLayout(context) mRelativeLayout. FindViewById(R.id. Textview1) could also be the problem, because you haven´t got the reference to your RelativeLayout in your Activity.

Although it´s more likely that my first guess is the solution... Oh, and you could simply design your RelativeLayout in xml. I see no real point in using a programmatically Layout here...

I can guarantee that the TextView IS instantiated.. I ran Log. V statements (before I ever posted this) to test that out. The output of instantiation was present before the output of trying to find it via it's ID.As for extending View - I already have already tried extending RelativeLayout instead of View .. but when I do that the other stuff that I have inside the onDraw method no longer draws (which I'm sure I could potentially fix, but I'm not sure why it wouldn't draw ... but none-the-less, it doesn't).

(cont) – Joishi Bodio Jun 4 at 20:29 However, I could try running myView.findViewById() instead of simply just findViewById() .. that might work. I'll let you know after I eat.In terms of just designing in inside an xml layout .. I know I can do that (and in fact already have). But the problem is that I want it to calculate values based off the dimensions of the display ... using XML won't do that, so I'm working within code instead of xml.

I want my program to look the same on multiple displays without having to have an xml layout for each type of display I might encounter... I will try what you mentioned, though. – Joishi Bodio Jun 4 at 20:31 So I added a method to my class that gave me the RelativeLayout, and then I ran myRelativeLayout. FindViewById(R.id.

Textview1) and it actually DID return the correct object, so what you suggested worked. :) Unfortunately, the animation still appears to be doing nothing.. Sooooo.. I might have to program out the animation in code also. I don't know.

I'm not a new programmer - just new to Android, and it does some things that seem strange to me. – Joishi Bodio Jun 4 at 20:50 edit: ah, you fixed it yourself. Don´t worry, I came from iOS-developing and there are maaaaaany things in Android that seem a bit odd to me.

:) so good luck with your animation! – stk Jun 4 at 20:57.

There's a setID-method for java coding a view.

– Joishi Bodio Jun 4 at 20:32 You are indeed. But it seems weird that you're using setId(R.id...). I might have misunderstood the issue now, but aren't you initiating an id by referring to its already existing id (which....doesn't exist).

If that's correct, then you just need to switch the argument from R.id.. to an actual nr. Sry if I'm saying stuff you think is obvious and stupid :p – keyser5053 Jun 4 at 23:02 The id only exists in xml. When I create the object, it has no id associated with it (since I haven't defined the object in xml with an @id+ tag).

So I define an id in xml (my first code block) .. and then I link that ID to the view so I can get the view back later. It's np, Martin - I just didn't know if you were referring so something else. – Joishi Bodio Jun 4 at 23:47 Oh ok.

Now I have question :p Does that xml file actually generate ID's? I had no idea you could do it like that. Good luck!

– keyser5053 Jun 5 at 10:35 Yes, it does. If you load up and look at the R file (it's under the gen folder under your application's package name), you'll see the additional id's under the "id" section of it. – Joishi Bodio 3 Jun1 at 16:45.

I just wrote an answer for someone confused by findViewById and I realised that I have a gap in my understanding. This question is for knowledge and curiosity only. FindViewById returns an instance of View, which is then cast to the target class.

All good so far. To setup the view, findViewById constructs an AttributeSet from the parameters in the associated XML declaration which it passes to the constructor of View. We then cast the View instance to Button.

How does the AttributeSet get passed in turn to the Button constructor? So I was the confused one :). The whole point is that when the layout is inflated, the view hierarchy already contains an instance of the view descendant class.

FindViewById simply returns a reference to it. Obvious when you think about it - doh..

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions