Hello,
I have tried asking elsewhere but got no replies so
I am now trying here.
1. I want to write an appliction
with a LinearLayout containing a Canvas and a
Button. When the button is pressed a circle is
drawn centered on the canvas, and when the
button is pressed again it disappears.
2. Is it better to code this or use an XML file.
How would the code look like in each case.
3. Would it be better to draw the button on the
canvas. What are the best practices in this case.
In the end I will need about six buttons in my app
for doing different things.
For now I have the following code but I cannot see the
button,
------------------------------------------------------------
---------------------
package com.example;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class ToggleCircle extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
Panel panel = new Panel(this);
Button button = new Button(this);
layout.addView(panel);
layout.addView(button);
setContentView(panel);
}
private class Panel extends View {
private Paint paint;
public Panel(Context context) {
super(context);
paint = new Paint();
}
@Override
public void onDraw(Canvas canvas) {
drawBlack = !drawBlack;
canvas.drawColor(drawBlack ? Color.BLACK : Color.WHITE);
//canvas.drawCircle(getHeight()/2, getWidth()/2, 10, paint);
canvas.drawCircle(20, 20, 10, paint);
}
private boolean drawBlack = false;
}
}
----------------------------------------------------------------------------
The button does not seem to be displayed.
Besides this code being wrong, I have another problem. Yesterday
the emulator was working, today it hangs after displaying the date
and time and the battery being charging 50% (not sure how a battery
makes sense for an emulator but anyway it hangs):
[2011-08-22 14:31:52 - ToggleCircle] ------------------------------
[2011-08-22 14:31:52 - ToggleCircle] Android Launch!
[2011-08-22 14:31:52 - ToggleCircle] adb is running normally.
[2011-08-22 14:31:52 - ToggleCircle] Performing
com.example.ToggleCircle activity launch
[2011-08-22 14:31:52 - ToggleCircle] Automatic Target Mode: using
existing emulator 'emulator-5554' running compatible AVD 'my_avd'
[2011-08-22 14:31:52 - ToggleCircle] Uploading ToggleCircle.apk onto
device 'emulator-5554'
[2011-08-22 14:31:53 - ToggleCircle] Installing ToggleCircle.apk...
[2011-08-22 14:31:57 - ToggleCircle] Success!
[2011-08-22 14:31:57 - ToggleCircle] Starting activity
com.example.ToggleCircle on device emulator-5554
[2011-08-22 14:32:01 - ToggleCircle] ActivityManager: Starting: Intent
{ act=android.intent.action.MAIN
cat=[android.intent.category.LAUNCHER] cmp=com.example/.ToggleCircle }
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en