I have a small issue here is the code i have so far:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GraphicView(this));
TextView mainLabel = (TextView)findViewById(R.id.title);
mainLabel.setText("Android Circle Path");
}
static class GraphicView extends View{
public GraphicView(Context context){
super(context);
}
@Override
public void onDraw(Canvas canvas){
}
}
}
And the main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="30dip"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip">
<TextView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dip"
android:textSize="24.5sp"
android:id="@+id/title">
</TextView>
</LinearLayout>
</LinearLayout>
When i try to set the text in the onDraw() function the app crashes, what am i doing wrong?
A view’s
onDrawmethod should be used to render to that view alone. You should not be calling UI update methods in another view. Move the code for setting the title into your activity’sonCreatemethod, since that only needs to be done once. Then just draw your circle in your custom view. Also, since your custom view does not implement ViewParent, it shouldn’t be used to hold the title TextView. Reorganize your xml to move the title view outside the GraphicsView