Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
Android Studio - Define class that requires context and attributes in main activity

I am trying to use a custom class in my main activity.

DrawingCanvas drawingCanvas = new DrawingCanvas();

To define the class I have to pass it two arguments. This is my constructor.

public DrawingCanvas(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

I know I need to do something like below, but I am not sure what to pass for the attributes set.

DrawingCanvas drawingCanvas = new DrawingCanvas(this, );
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I assume DrawingCanvas is extending from the View class or one that inherits from it.

In this case, it's convenient to have the constructors similar to this:

    public DrawingCanvas(@NonNull Context context) {
        this(context, null);
    }

    public DrawingCanvas(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawingCanvas(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // more code…
    }

That way you can choose the most adequate one. As you can see, when there's no AttributeSet, you can just pass null.

Now you would only need to do it like this:

DrawingCanvas drawingCanvas = new DrawingCanvas(this);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda