I'm trying to create a custom TextView
. I look here for my guidance as well and I made it working, though I'm still confused by why the other component is not being drawn, this is just more simple than the reference guide of android but it's not working in mine.
From the custom class I made (CustomTextView
w/c extends TextView
),
I try to add a OnTouchListener()
and invoke the invalidate()
method for both this
and other component w/c is a TextView
but it still not being drawn.
CustomTextView.java
public class CustomTextView extends TextView {
TextView extra_detail;
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
extra_detail = new TextView(context);
extra_detail.setText("Hey!");
extra_detail.setPadding(20, 0, 0, 0);
extra_detail.setTextSize(2);
a.recycle();
//I also tried this in case the space isn't enough
setMinHeight(80);
extra_detail.invalidate();
this.invalidate();
}
final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Here's my problem
//both are not being drawn
//even if I remove the other one and vice-versa
canvas.drawText("Hey", 10, 0, paint);
extra_detail.draw(canvas); //is this the right way to do this?
}
@Override
public boolean onTouchEvent(MotionEvent event) {
invalidate();
extra_detail.invalidate();
//still not being drawn
//after the invalidation
Toast.makeText(getContext(), "Hey!", Toast.LENGTH_SHORT).show();
return super.onTouchEvent(event);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
extra_detail.measure(widthMeasureSpec, heightMeasureSpec);
}
}
It seems I'm missing something here but cannot figure it out.
Update
I happen to extend
my Activity
with ActionBarActivity
. Now with this kind of pre defined Activity
, there is a title bar
implemented and its hiding all the canvas.draw*()
I made on the top-left-most of the screen. I figure it out after I invoke the setY(10)
then the canvas.drawText()
appears.
But I still have the problem of drawing another View
in onDraw()
event.