[android-beginners] Re: My First Custom View
Bump...anyone? On Jun 15, 7:42 pm, Dave Smith dasmith1...@gmail.com wrote: Hi all - I'm trying to get a custom view example running out of the Professional Android Application Development book (the CompassView). I have checked over my code numerous times, and still the same problem occurs...whenever I run the application I only see a black screen and the view never pops up. I have stepped through the debugger a few times and I have two theories: 1. I have a problem with onMeasure()...my code is currently calling setMeasuredDimension with 0,0 2. The view might be invisible...I read back the value of CompassView.isShown() and it read false Here is the measure code that is being used: @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // The compass is a circle that fills as much screen as possible. // Set the measured dimensions to the shortest screen boundary. int measuredWidth = measure(widthMeasureSpec); int measuredHeight = measure(heightMeasureSpec); int d = Math.min(measuredWidth, measuredHeight); //setMeasuredDimension must be called by onMeasure setMeasuredDimension(d, d); } private int measure(int measureSpec){ int result = 0; //Decode the spec value int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(specMode); if(specMode == MeasureSpec.UNSPECIFIED){ //Return a default value result = 200; } else { //Return full bounds since we are filling available space result = specSize; } measure() is returning MeasureSpec.EXACTLY for mode and 0 for size from both width and height. How is that possible? I can attach more code if necessary, because my other problem is that onDraw() never gets called...but I thought that might have something to do with having no size. If it matters, I ran this under Android 1.1 and 1.5 to be sure I didn't miss something in a version update. Thanks in advance! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Beginners group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] My First Custom View
Hi all - I'm trying to get a custom view example running out of the Professional Android Application Development book (the CompassView). I have checked over my code numerous times, and still the same problem occurs...whenever I run the application I only see a black screen and the view never pops up. I have stepped through the debugger a few times and I have two theories: 1. I have a problem with onMeasure()...my code is currently calling setMeasuredDimension with 0,0 2. The view might be invisible...I read back the value of CompassView.isShown() and it read false Here is the measure code that is being used: @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // The compass is a circle that fills as much screen as possible. // Set the measured dimensions to the shortest screen boundary. int measuredWidth = measure(widthMeasureSpec); int measuredHeight = measure(heightMeasureSpec); int d = Math.min(measuredWidth, measuredHeight); //setMeasuredDimension must be called by onMeasure setMeasuredDimension(d, d); } private int measure(int measureSpec){ int result = 0; //Decode the spec value int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(specMode); if(specMode == MeasureSpec.UNSPECIFIED){ //Return a default value result = 200; } else { //Return full bounds since we are filling available space result = specSize; } measure() is returning MeasureSpec.EXACTLY for mode and 0 for size from both width and height. How is that possible? I can attach more code if necessary, because my other problem is that onDraw() never gets called...but I thought that might have something to do with having no size. If it matters, I ran this under Android 1.1 and 1.5 to be sure I didn't miss something in a version update. Thanks in advance! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Beginners group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] My First Custom View Totally Busted
Hi all - I'm trying to get up to speed with custom views, and I took most of this code from examples, but I cannot get it to do anything. When I run the code below in the emulator I just get a blank screen, and when I set debug breakpoints it shows that onDraw() never gets called! It's got to be something dumb and simple, but I don't see it. I'm just trying to get a circle painted for starters. I even tried adding an mv.invalidate() call to the onCreate() method to try and get it to redraw but nothing changed. Help! Thanks in advance! [main.xml] ?xml version=1.0 encoding=utf-8? LinearLayout xmlns:android=http://schemas.android.com/apk/res/ android android:orientation=vertical android:layout_width=fill_parent android:layout_height=fill_parent com.examples.customview.myView android:id=@+id/myView android:layout_width=fill_parent android:layout_height=fill_parent/ /LinearLayout [/main.xml] [myActivity.java] package com.examples.customview; import android.app.Activity; import android.os.Bundle; public class Compass extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myView mv = (myView)this.findViewById(R.id.myView); } } [/myActivity.java] [myView.java] package com.examples.customview; import android.content.Context; import android.graphics.*; import android.view.*; import android.util.AttributeSet; import android.content.res.Resources; public class myView extends View { [...private members...] public myView(Context context){ super(context); initView(); } public myView(Context context, AttributeSet attrs){ super(context, attrs); initView(); } public myView(Context context, AttributeSet attrs, int defaultStyle){ super(context, attrs, defaultStyle); initView(); } protected void initView(){ setFocusable(true); circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG); circlePaint.setColor(R.color.background_color); circlePaint.setStrokeWidth(1); circlePaint.setStyle(Paint.Style.FILL_AND_STROKE); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // The compass is a circle that fills as much screen as possible. // Set the measured dimensions to the shortest screen boundary. int measuredWidth = measure(widthMeasureSpec); int measuredHeight = measure(heightMeasureSpec); int d = Math.min(measuredWidth, measuredHeight); //setMeasuredDimension must be called by onMeasure setMeasuredDimension(d, d); } private int measure(int measureSpec){ int result = 0; //Decode the spec value int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(specMode); if(specMode == MeasureSpec.UNSPECIFIED){ //Return a default value result = 200; } else { //Return full bounds since we are filling available space result = specSize; } return result; } @Override protected void onDraw(Canvas canvas){ //Find the center point int px = getMeasuredWidth() / 2; int py = getMeasuredHeight() / 2; int radius = Math.min(px, py); //Draw the background canvas.drawCircle(px, py, radius, circlePaint); } } [/myView.java] --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Beginners group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---