OnDraw() method of custom view only called once on button click - android

I'm trying to call onDraw method on Button click ,button the view is updated only once also onDraw method is called . But no change in the position of line.
I have this custom view
public LineSeekbar(Context context) {
super(context);
this.setWillNotDraw(false);
setNewX(130);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.e("GRAPH","draw");
paint = new Paint();
paint.setARGB(225, 215, 10, 20);
paint.setStrokeWidth(2);
paint.setStyle(Style.FILL);
canvas.drawLine(130,900,getNewX(),100, paint);
setNewX(getNewX()+15);
}
Calling this from activity class
final Bitmap mBackgroundImage = Bitmap.createBitmap(500,500, Bitmap.Config.RGB_565);
cv =new Canvas(mBackgroundImage);
LineView = new LineSeekbar(LineActivity.this);
LineView.setLayoutParams(new LayoutParams(500,500));
LineView.onMeasure(500,500);
LineView.invalidate();
LineView.draw(cv);
// LineView = null;
ImageView mImageView = new ImageView(this);
mImageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mImageView.setBackgroundColor(android.R.color.white);
mImageView.setImageBitmap( mBackgroundImage );
LinearLayout ll =(LinearLayout) findViewById(R.id.linearLayout1);
ll.addView(mImageView);
Button inc = (Button) findViewById(R.id.increase);
inc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LineView.invalidate();
LineView.draw(cv);
}
});

You should call an instance with small letter because it is not a class: lineView insted of LineView.
Since you are calling it from another thread (UI) you should say
LineView.postInvalidate();
When you create LineView instance for the first time, onDraw will be executed on its own and show the first line.
It is not clear what is LineView.draw(cv); You can draw the background image in onDraw just before drawing the line. You can use onSizeChange method in the custom view to find its real dimensions and resize your bitmap...
In onDraw insert a line
Log.e("LineView", Integer.toString(getNewX()));
and then in DDMS - LogCat watch the output when you press the button.

Try this:
You didn't made an object of the LineView class like you should. You should use small letters for instances.
final Bitmap mBackgroundImage = Bitmap.createBitmap(500,500, Bitmap.Config.RGB_565);
cv =new Canvas(mBackgroundImage);
LineView lineView = new LineSeekbar(LineActivity.this);
lineView.setLayoutParams(new LayoutParams(500,500));
lineView.onMeasure(500,500);
lineView.invalidate();
lineView.draw(cv);
// LineView = null;
ImageView mImageView = new ImageView(this);
mImageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mImageView.setBackgroundColor(android.R.color.white);
mImageView.setImageBitmap( mBackgroundImage );
LinearLayout ll =(LinearLayout) findViewById(R.id.linearLayout1);
ll.addView(mImageView);
Button inc = (Button) findViewById(R.id.increase);
inc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
lineView.invalidate();
lineView.draw(cv);
}
});

Solved it ,I placed custom view in xml layout ,its working fine.

Related

Android add ImageView programmatically

I currently add images through xml with the whole R.id.x method with the following function:
public void Image(int ID, int x, int y){
ImageView iv = (ImageView) findViewById(ID);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.setMargins(x, y, 0, 0);
iv.setLayoutParams(position);
}
I've written a new function to get these images on screen programmatically instead of parsing them in XML, with help from afore-mentioned topics/questions I searched and came up with this:
public void ImageRAW(int ID, int x, int y){
ImageView iv = new ImageView(c);
iv.setImageResource(ID);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.setMargins(x, y, 0, 0);
iv.setLayoutParams(position);
rl.addView(iv);
}
But it did not work. I also tried adding the following line, to no avail: iv.setVisibility(View.VISIBLE);
And in regards to the variables rl and c:
private Context c;
private RelativeLayout rl;
public void SetUtilContext(Context context){
c = context;
rl = new RelativeLayout(context);
}
The above function is called in every Activity's onCreate() function and sets the UtilLib's current Context and RelativeLayout for drawing accordingly.
The function ImageRAW() is something I would like to use to replace the old Image() function, to make things easier for me. How would/could I get this working?
Try add this before your SetUtilContext():
RelativeLayout menu = findViewById(R.layout.menu);
And this at the end of your ImageRAW() method:
menu.addChild(rl);
As per Incredible's request, my onCreate function where I'm testing the functions:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
SetUtilContext(this);
Image(R.id.logo, 0, 0);
GetScreenSize();
ImageButton(R.id.start_btn
, (screenWidth/2)-(GetImageWidth(R.id.start_btn)/2)
, 48+GetImageHeight(R.id.logo));
ImageButton(R.id.options_btn
, (screenWidth/2)-(GetImageWidth(R.id.options_btn)/2)
, 96+GetImageHeight(R.id.logo)+GetImageHeight(R.id.start_btn));
ImageButton(R.id.about_btn
, (screenWidth/2)-(GetImageWidth(R.id.about_btn)/2)
, 144+GetImageHeight(R.id.logo)+(GetImageHeight(R.id.start_btn)*2));
ImageButton(R.id.exit_btn
, (screenWidth/2)-(GetImageWidth(R.id.exit_btn)/2)
, 192+GetImageHeight(R.id.logo)+(GetImageHeight(R.id.start_btn)*3));
PlayAudio(R.raw.theme, true);
ImageRAW(R.drawable.head, 0, GetImageHeight(R.id.logo));
}

Android: Add EditText to View error

bascially I want to have two views. One view is for the canvas, used for drawing rectangles and another view or adding of new editText boxes whenever a rectangle is drawn. When I run my program, an error occured "java.lang.NullPointerException". Is it possible to add the edittext boxes onto the canvas view so that I keep only one view?
My code are as follows:
public class MainActivity extends Activity {
public static DrawRect DR;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DR = new DrawRect(this);
rectbutton = (Button) findViewById(R.id.rectbutton);
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.RelativeLayout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.BELOW, R.id.rectbutton);
DR.setLayoutParams(lp);
mainLayout.addView(DR);
rectbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
addRect(); // in my DrawRect class i have this method to draw rect on canvas
addEditText();
}// onclick
});
}
private void addEditText(){
RelativeLayout editTextLayout = new RelativeLayout(this);
EditText editText = new EditText(this);
editTextLayout.addView(editText);
mainLayout.addView(editTextLayout);
}
}
Please Advice. Thank you.
Which line its giving error. if its giving error in
mainLayout.addView(editTextLayout);
means mainLayout is null.you can add EditText in canvas.
the problem is in
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.RelativeLayout);
check the id in layout xml.

Why isn't the listener set properly in this case

In my activity, I display a camera preview and a button (a view that looks like a button) that I draw over it. The problem is that when I set the click listener on that View, no matter where I click on the screen (even on the camera preview) the click event is fired.
This is the code :
FrameLayout preview = (FrameLayout) findViewById(id.camera_preview);
preview.addView(mCameraPreview);
CameraButtonView cameraButton = new CameraButtonView(getBaseContext());
preview.addView(cameraButton);
cameraButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(CameraTestActivity.this, MenuActivity.class);
startActivity(intent);
finish();
}
});
So the FrameLayout is the Camera Preview. The CameraButtonView is the view that gets drawn on it (somewhere at the bottom .. it doesn't take much space) and the listener is really set on the view.
Why is it that when I click anywhere on the FrameLayout my event gets triggered ? Also, how could I fix it so that only the button is subscribed to the event.
Camera Button View :
public class CameraButtonView extends View
{
Paint buttonPaint = new Paint();
Paint textPaint = new Paint();
SizeF size = new SizeF(500, 125);
public CameraButtonView(Context context)
{
super(context);
buttonPaint.setColor(Color.RED);
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(35);
}
#Override
public void onDraw(Canvas canvas)
{
RectF surface = RectangleHelper.create(0, 0, canvas.getWidth(), canvas.getHeight());
PointF alignedPoint = RectangleHelper.align(surface, size, ContentAlignment.BottomCenter, AlignmentType.Internal);
RectF content = RectangleHelper.create(alignedPoint, size);
Rect textRectangle = new Rect();
String buttonText = "Click to take a pic.";
buttonPaint.getTextBounds(buttonText, 1, buttonText.length(), textRectangle);
PointF alignedTextPoint = RectangleHelper.align(content, new SizeF(textRectangle.width(), textRectangle.height()), ContentAlignment.MiddleLeft, AlignmentType.Internal);
canvas.drawRect(content, buttonPaint);
canvas.drawText(buttonText, alignedTextPoint.x, alignedTextPoint.y, textPaint);
}
}
My guess is that the layoutparams on the CameraButtonView are larger than what the button actually looks like. Try to set the layoutparms to new LayoutParams(50,50) and see if it helps. Then you'll need to workout how to size it relatively.
Without seeing the code for CameraButtonView, I can't give a definitive answer.
UPDATE (working):
CameraButtonView cameraButton = new CameraButtonView(getBaseContext());
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(500, 125);
layoutParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
cameraButton.setLayoutParams(layoutParams);
preview.addView(cameraButton);

Android draw TextView from CustomView

I have a customview looking like this:
public class CustomView extends View {
protected Context c;
protected String text;
... // and some more useful member variables...
public CustomView(String text, Context c, ...) {
this.text = text; this.c = c;
...
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
LinearLayout ll = new LinearLayout(c);
TextView tv = new TextView(c);
tv.setText(text);
ll.addView(tv);
ll.draw(canvas);
}
And in my main activity, I do this:
RelativeLayout gamelayout = (RelativeLayout) findViewById(R.id.gamelayout);
CustomView customview = new CustomView("Textview text", this);
gamelayout.addView(customview);
My problem is, that simply nothing is drawn, the drawn TextView does not appear in the "gamelayout". What am I doing wrong?
TextView objects are not able to draw directly to the canvas, as you've done you need to assign to a Layout and then do this:
ll.layout(0, 0, canvas.getWidth(), canvas.getHeight()); //Modify values as needed
Personally, I'm surprised there aren't errors thrown for calling ll.draw(). Unless you have a need to drawing a TextView I prefer drawing the text to the canvas:
canvas.drawText(...)
See documentation here
Your LinearLayout isn't attached to your view
try this.addView(ll) to add your LinearLayout to your view.

set the canvas size - Android

I want to keep the same size of the bitmap for the canvas, because when I add the custom view to the LinearLayout shows the canvas with different size and I want to set the size of the canvas like bitmap object.
Part of the code:
public class TESTActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout l = new LinearLayout(this);
l.setOrientation(1);
Button b1 = new Button(this);
Button b2 = new Button(this);
View mV = new MyView(this);
l.addView(b1);
l.addView(b2);
l.addView(mV);
setContentView(l);
}
public class MyView extends View {
public MyView(Context c) {
super(c);
mBitmap = Bitmap.createBitmap(480, 300, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
...
}
...
}
}
The Canvas class holds the "draw" calls. Canvas(Bitmap bitmap) Construct a canvas with the specified bitmap to draw into. canvas will take the size of the the draw object. so by setting size of bitmap you can set size of canvas.
If you want to draw on canvas with your custom height and width you have to call setContentView(android.view.View yourView , android.view.Viewgroup.LayoutParam yourLayout) in your activity class.Because by default setContentView(View view) method use full width and height.So you have to use its overloaded method with two parameter along with your desired. See documentation for more info.And don`t use only LayoutParams() constructor to create its object. Use it by writing its full path like android.view.ViewGroup.LayoutParams. Because there are some other classes with same name in Android SDK.If you only use LayoutParams Eclipse may not find correct class so use full path.
MyView customView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customView = new MyView(getApplicationContext());
android.view.ViewGroup.LayoutParams lp = new android.view.ViewGroup.LayoutParams(100,200);//100 is width and 200 is height
setContentView(customView, lp);
customView.setOnClickListener(this);
}`

Categories

Resources