Joishi Bodio
Joishi Bodio

Reputation: 438

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

So I have a custom view set up in code (no XML layout defined for it) and am wondering how to get the child Views ID's defined correctly. I DO have an xml file defining id's similar to this.. But how I understand it, at least, since I don't have an xml layout there's no AttribSet to pass .. so my constructors are all just (Context context) types.

<resources>
    <item type="id" name="textview1"/>
    <item type="id" name="textview2"/>
</resources>

And my view looks something along these lines:

public class MyView extends View {

    protected RelativeLayout baseLayout;
    protected TextView textView1;
    protected TextView textView2;

    public MyView(Context context) {
        super(context);
        LayoutParams fill = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        setLayoutParams(fill);

        Resources res = getResources();

        baseLayout = new RelativeLayout(context);
        baseLayout.setLayoutParams(fill);

        textView1 = new TextView(context);
        textView1.setId(R.id.textview1);
        // other init stuff here

        textView2 = new TextView(context);
        textView2.setId(R.id.textview2);
        // other init stuff here

        baseLayout.addView(textView1);
        baseLayout.addView(textView2);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // *snip lots of stuff*

        baseLayout.draw(canvas);
    }

    // This is only here because findViewById() returns NULL when I search
    public TextView getTextView1() {
        return textView1;
    }

    // This is only here because findViewById() returns NULL when I search
    public TextView getTextView2() {
        return textView2;
    }

}

Here's the Activity that uses the view..

public class MyActivity extends Activity {

    // This is only here because findViewById() returns NULL when I search
    private MyView myView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Resources res = getResources();

        myView = new myView(this);
        setContentView(myView);

        // This returns NULL.  :/       
        // final TextView textView1 = (TextView) findViewById(R.id.textView1);

        // This is only here because findViewById() returns NULL when I search..
        final TextView textView1 = myView.getTextView1();
        final Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_animation);
        textView1.setAnimation(anim);
        anim.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation _anim) {
                Log.v("InsideAnimation", "Animation ended");
                textView1.setVisibility(View.INVISIBLE);
            }
            @Override
            public void onAnimationRepeat(Animation _anim) {
                Log.v("InsideAnimation", "Animation repeated");
            }
            @Override
            public void onAnimationStart(Animation _anim) {
                Log.v("InsideAnimation", "Animation started");
            }
        });
        anim.reset();
        textView1.startAnimation(anim);
    }
}

The main problem is that the animation I load (which is defined in xml) and start does absolutely nothing. I get a Log statement of "animation started" but that's it. It never ends and it never does anything when I emulate it. It's like it starts, but doesn't actually RUN. I think the issue is that maybe the animation is expecting to use xml id layout definitions to do it's work on, but since findViewById() returns NULL, I'm assuming that part is broken (and thus the animation doesn't work). So .. at least for right now, I want to find out what I'm doing wrong with setting the ID so that I can get findViewById() to return the correct view and not NULL... And then possibly the animation will work. (or if it still doesn't work after that point, I'll have to look at maybe defining the animation in code also instead of xml)

Any help or suggestions are appreciated.

Upvotes: 5

Views: 7580

Answers (2)

stk
stk

Reputation: 6461

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...

Upvotes: 0

keyser
keyser

Reputation: 19189

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

Upvotes: 1

Related Questions