Reputation: 13
I'm new to Android development and I'm trying to implement a custom view to act as a "custom menu button" for my app.
I followed the instructions at http://developer.android.com/training/custom-views/create-view.html but by the end of the implementation I get a message saying "Unfortunately customviews1 has stopped" and the app just shut's down.
My approach is very simple and I cannot find any reference about solving this basic problem. Here's what I'm doing:
create a new Android project in Eclipse named "customviews1"
I run the project and it shows a "Hello World" TextView on the "activity_main.xml" layout file
I add a new class named MyCustomView that extends "View" to the "src" folder of the project
public class MyCustomView extends View {
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
I remove the "TextView" tag from activity_main.xml and add a "customview1" to it:
<com.example.customviews1.MyCustomView android:id="@+id/myCustomView1" />
I run the app again and I got the message saying "Unfortunately customviews1 has stopped" and the app shuts down.
Is there any code I'm missing here?
Thanks for any clue, Regards, Victor Reboucas
Upvotes: 0
Views: 232
Reputation: 493
Please try as following.I tried this one and success. first you have to override all three super class constructor.
To show some thing in view you have to override onDraw()
method.
public class MyCustomView extends View {
//variables and objects
Paint p;
//override all three constructor
public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyCustomView(Context context) {
super(context);
init();
}
//do what you want in this method
@Override
protected void onDraw(Canvas canvas) {
canvas.drawText("This is custom view this can be added in xml", 10, 100, p);
canvas.drawRect(20, 200, 400, 400, p);
super.onDraw(canvas);
}
//all the initialization goes here
private void init() {
p =new Paint();
p.setColor(Color.RED);
}
}
In xml
file include it as following example
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.sanath.customview.MyCustomView
android:id="@+id/myCustomView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
Hope this will help you
Upvotes: 0
Reputation: 481
I think that you cant do that on that way , you must overide all methods from view class like onDraw()
and other , read more about it
Upvotes: 0
Reputation: 8180
if you check your LogCat output you will find that an error saying that you have to specify layout_width and layout_height in your layout.
so write:
<com.example.customviews1.MyCustomView android:id="@+id/myCustomView1"
android:layout_width="match_parent" android:layout_height="match_parent"/>
and it should work.
Upvotes: 4