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);
textView2 = new TextView(context);
textView2.setId(R.id.textview2);
baseLayout.addView(textView1);
baseLayout.addView(textView2);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
baseLayout.draw(canvas);
}
public TextView getTextView1() {
return textView1;
}
public TextView getTextView2() {
return textView2;
}
}
This is where the action using the view is performed.
public class MyActivity extends Activity {
private MyView myView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources res = getResources();
myView = new myView(this);
setContentView(myView);
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)
.