Custom view. How to set an identifier to find them through findViewById ()?

So, I have a custom view configured in the code (there is no XML layout defined for it), and I am wondering how to correctly determine the identifier of the child. I have an xml file defining an identifier similar to this. But as I understand it, at least since I do not have an xml layout, there is no AttribSet to pass in ... so my constructors are all simple (context context).

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

And my view looks something like this:

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;
    }

}

This is where the action using the view is performed.

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);
    }
}

, ( xml) . " ", . , , . , . , , , , xml id, , findViewById() NULL, , (, , ). ... , , , findViewById(), , NULL... , , , ( - , , , xml)

.

+5
2

, TextView . oncreate.

, MyView View, RelativeLayout . , RelativeLayout, -

mRelativeLayout = new MyRelativeLayout(context)
mRelativeLayout.findViewById(R.id.textview1)

, RelativeLayout . , - ...

, RelativeLayout xml. ...

0

setID-method java- .

+1

All Articles