Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

null

view

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.

like image 486
Joishi Bodio Avatar asked Nov 13 '22 22:11

Joishi Bodio


1 Answers

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

like image 91
keyser Avatar answered Jan 06 '23 18:01

keyser