esQmo esQmo - 2 months ago 6
Java Question

calling a method having parameters context and attributeset from MainActivity

I have an app that displays random characters vertically on the screen (like in the matrix movie) and I would like to add buttons to change the color of these chars when pressed, but I'm unable to call the method

setColor
of
paintTxt
variable from
MainActivity
.

This is my code,

EffetMatrix effetMatrix = new EffetMatrix();//Error in ()
final Paint paintTxt = effetMatrix.paintTxt;
paintTxt.setColor(Color.RED);


But the editor shows an error:


EffetMatrix(Context, AttributSet) in EffetMatrix cannot be applied to ()


the EffetMatrix class code

package com.esqmo.apps.effetmatrix;

import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.content.Context;
import android.widget.Button;

/**
* Created by esQmo on 04/10/2016.
*/
public class EffetMatrix extends View {
private static final Random ALEATOIRE = new Random();
private int larg, haut;
private Canvas toile;
private Bitmap toileBmp;
private int taillePolice = 40;
private int tailleColonne;
private char[] chars = "01".toCharArray();
private int[] posTxtParColonne;
public Paint peindreTxt, peindreArrPlan, peindreArrPlanBmp, peindreInitArrPlan;

public EffetMatrix(Context context, AttributeSet attrs) {
super(context, attrs);

peindreTxt = new Paint();
peindreTxt.setStyle(Paint.Style.FILL);
peindreTxt.setColor(Color.BLUE);
peindreTxt.setTextSize(taillePolice);

peindreArrPlan = new Paint();
peindreArrPlan.setStyle(Paint.Style.FILL);
peindreArrPlan.setColor(Color.BLACK);
peindreArrPlan.setAlpha(5);

peindreArrPlanBmp = new Paint();
peindreArrPlanBmp.setColor(Color.BLACK);

peindreInitArrPlan = new Paint();
peindreInitArrPlan.setColor(Color.BLACK);
peindreInitArrPlan.setAlpha(255);
peindreInitArrPlan.setStyle(Paint.Style.FILL);
}

@Override
protected void onSizeChanged(int l, int h, int ancl, int anch) {
super.onSizeChanged(l, h, ancl, anch);

larg = l;
haut = h;

toileBmp = Bitmap.createBitmap(larg, haut, Bitmap.Config.ARGB_8888);
toile = new Canvas(toileBmp);
toile.drawRect(0, 0, larg, haut, peindreInitArrPlan);
tailleColonne = larg / taillePolice;

posTxtParColonne = new int[tailleColonne + 1];

for (int x = 0; x < tailleColonne; x++) {
posTxtParColonne[x] = ALEATOIRE.nextInt(larg / 2) + 1;
}

}

private void dessineTexte() {
for (int i = 0; i < posTxtParColonne.length; i++) {
toile.drawText("" + chars[ALEATOIRE.nextInt(chars.length)], i * taillePolice,
posTxtParColonne[i] * taillePolice, peindreTxt);
if (posTxtParColonne[i] * taillePolice > larg && Math.random() > 0.980) {
posTxtParColonne[i] = 0;
}
posTxtParColonne[i]++;
}
}

private void dessineToile() {
toile.drawRect(0, 0, larg, haut, peindreArrPlan);
dessineTexte();
}

@Override
protected void onDraw(Canvas toile) {
super.onDraw(toile);
toile.drawBitmap(toileBmp, 0, 0, peindreArrPlanBmp);
dessineToile();
invalidate();
}
public void setCustomColor(int color){
peindreTxt.setColor(color);
invalidate();
}


}


note: variable peindreText = paintText

The main activity code:

package com.esqmo.apps.effetmatrix;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.effect.Effect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

final Button boutton_vert = (Button) findViewById(R.id.b_v);
final Button boutton_bleu = (Button) findViewById(R.id.b_b);
final Button boutton_rouge = (Button) findViewById(R.id.b_r);
final Button boutton_rose = (Button) findViewById(R.id.b_ro);

boutton_bleu.setOnClickListener(this);
boutton_vert.setOnClickListener(this);
boutton_rouge.setOnClickListener(this);
boutton_rose.setOnClickListener(this);

}

@Override
public void onClick(View v) {

}


public void passerVert(View v) {

}

public void passerRouge(View v) {

}

public void passerRose(View v) {

}

public void passerBleu(View v) {


}
}


main.xml




<com.esqmo.apps.effetmatrix.EffetMatrix
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/arrPlan"/>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#040404"
android:id="@+id/linearLayout">

<Button
android:layout_width="70dp"
android:layout_height="match_parent"
android:text="@string/button_vert"
android:onClick="passerVert"
android:id="@+id/b_v"
android:textAppearance="@android:color/holo_blue_dark" />

<Button
android:layout_width="70dp"
android:layout_height="match_parent"
android:text="@string/button_bleu"
android:onClick="passerBleu"
android:id="@+id/b_b"/>

<Button
android:layout_width="70dp"
android:layout_height="match_parent"
android:text="@string/button_rouge"
android:onClick="passerRouge"
android:id="@+id/b_r" />

<Button
android:layout_width="70dp"
android:layout_height="match_parent"
android:text="@string/button_rouge"
android:onClick="passerRose"
android:id="@+id/b_ro" />



</LinearLayout>





PS: I'm a noob in programming.
Sorry for my english

Answer

Don't create a new instance of the custom view again. Instead get a reference to the view using its id.

See the following code,

EffetMatrix effetMatrix = (EffetMatrix) findViewById(R.id.arrPlan);
effetMatrix.setCustomTextColor(Color.RED);

Inside EffetMatrix class create a method named setCustomColor as follows,

class EffetMatrix {
    ...

    public void setCustomTextColor(int color){
        // Set the color to the paintTxt object
        paintTxt.setColor(color);
        // invalidate the view to apply the changes
        invalidate();
    }

    ...
}

This is how you implement it in your code,

public class MainActivity extends AppCompatActivity implements  View.OnClickListener {
    EffetMatrix effetMatrix;

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

        effetMatrix = (EffetMatrix) findViewById(R.id.arrPlan);        
        ...
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.b_b:
                effetMatrix.setCustomTextColor(Color.BLUE);
                break;
            case R.id.b_r:
                effetMatrix.setCustomTextColor(Color.RED);
                break;
            case R.id.b_ro:
                effetMatrix.setCustomTextColor(Color.MAGENTA);
                break;
        }
    }
}