user2592686 user2592686 - 1 month ago 9
Android Question

Draw View from XML file under other TextView

I'm trying to draw a Canvas in a View and show this View under some TextViews I defined in my XML file.
Everytime I test the App it just doesn't start on the device.

However, the parts themself work:
When I change the

setContentView(R.layout.activity_main);
in the
onCreate()
to
setContetView(new CustomView(this));
it works, but of course without the textView.

My activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
android:id="@+id/sampleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Text"/>

<com.example.simon.drawtest.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/sampleText"
android:layout_alignParentBottom="true"/>

</RelativeLayout>


My MainActivity.java:

package com.example.simon.drawtest;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}


My CustomView.java:

package com.example.simon.drawtest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class CustomView extends View {

private Paint paint;

public CustomView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.GRAY);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLUE);
canvas.drawCircle(200, 200, 100, paint);
}
}

Answer

show your whole activity_main.xml...

both Views should be wrapped in e.g. FrameLayout

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/sampleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some Text"/>
    <com.example.simon.drawtest.CustomView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sampleText"
        android:layout_alignParentBottom="true"/>
</FrameLayout>

edit: ok, so you have RelativeLayout.. also note that with Android 5.0 elevation attribute was introduced and you may have some problems with drawing order like HERE

also... your TextView is first in XML so it will be drawn before your custom view (drawing by order if elevation isn't set)

also... android:layout_below="@+id/sampleText" below means under in vertical axis. try to remove this line

edit: user post exception stack trace in comments, so below I'm pasting proper answer for his case (comment copied):

your error stack says

Caused by: java.lang.NoSuchMethodException: [class android.content.Context, interface android.util.AttributeSet]

you have only one constructor with Context only, system needs also one with additional AttributeSet. check out THIS question & answer, apply proper constructors for your CustomView

Comments