Problem with canvas in android studio

0

Good afternoon, I hope someone can guide me that I'm a bit lost, the problem is that I'm drawing a canvas in my project where I have an interface with 2 buttons and an editText, and at the moment of running the app the canvas covers my interface and I can not see it, the question is there is some way to send the canvas to the background or super put the interface, beforehand thanks

here the code:

package preferences.proyecto.carlos.preferences;

import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.Layout;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ConstraintLayout layout1 = (ConstraintLayout) findViewById(R.id.layout1);
        Lienzo fondo = new Lienzo(this);
        layout1.addView(fondo);
    }

    class Lienzo extends View{

        public Lienzo(Context context){
            super(context);
        }

        protected void onDraw(Canvas canvas){
            canvas.drawRGB(255, 255, 255);
            int ancho = canvas.getWidth();
            int alto = canvas.getHeight();
            Paint pincel1 = new Paint();
            pincel1.setARGB(255, 255, 0, 0);
            // drawLine (comienza en x,comienza en y,limite en x,limite en y,pincel)
            canvas.drawLine(70, 0, 70, alto, pincel1);
            canvas.drawLine(73, 0, 73, alto, pincel1);
            pincel1.setARGB(255, 0, 0, 255);
            int cantLineas = alto / 30;
            for (int fila = 0; fila < cantLineas; fila++) {
                canvas.drawLine(0,((fila * 60 )+ 100), ancho, ((fila * 60 )+ 100),
                        pincel1);
            }
        }
    }
}

and there's the interface:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="preferences.proyecto.carlos.preferences.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="Guardar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="Borrar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.63" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.23000002" />
</android.support.constraint.ConstraintLayout>
    
asked by Carlos 23.10.2017 в 00:23
source

1 answer

0

When you create the canvas view and add it to the ConstraintLayout by default, it is placed over the three views that the Layout already has (The two Button and the EditText).

The addView (View) method is overloaded. Try the following method addView (View child, int index)

In your case:

layout1.addView(fondo, 0);

I hope it helps you.

    
answered by 26.10.2017 / 19:29
source