Search code examples
javaandroidviewandroid-custom-view

How could I change one attribute of a custom view that has been created before?


I have a class that creates Custom Views. I called them Plant Views. Each of them have these attributes : Name, degree, info and image. I would like to change the info text of the one that has for name : "Test" for example, after a click on a button from an other class. I tried to set Tags to these custom Views but I haven't really understood how it works.

PlantView.java

public class PlantView extends FrameLayout {

//Views
private UnderlinedTextView nameView;
private TextView infoView;
private TextView degreeView;
private CircleImageView imageView;
private LinearLayout planteBg;

//Attributes
private String nameText;
private String infoText;
private String degreeText;
private Drawable plantImage;

private boolean isExtended = false;

/****************
 * CONSTRUCTEURS
 ***************/

/**
 * Constructeur de la classe.
 *
 * @param context the context.
 */
public PlantView(@NonNull Context context) {
    super(context);
    obtainStyledAttributes(context, null, 0);
    init();
}

/**
 * Constructeur.
 *
 * @param context the context.
 * @param attrs   the attributes from the layout.
 */
public PlantView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    obtainStyledAttributes(context, attrs, 0);
    init();
}

/**
 * Constructeur.
 *
 * @param context      the context.
 * @param attrs        the attributes from the layout.
 * @param defStyleAttr the attributes from the default style.
 */
public PlantView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    obtainStyledAttributes(context, attrs, defStyleAttr);
    init();
}


/**********
* FONCTIONS
***********/

//Fonction utile pour récupérer les attributs si l'on ajoute une "PlantView" directement en XML
private void obtainStyledAttributes(Context context, AttributeSet attrs, int defStyleAttr) {
    if (attrs != null) {
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PlantView, defStyleAttr, 0);
        nameText = typedArray.getString(R.styleable.PlantView_name);
        infoText = typedArray.getString(R.styleable.PlantView_info);
        degreeText = typedArray.getString(R.styleable.PlantView_degree);
        plantImage = typedArray.getDrawable(R.styleable.PlantView_android_src);

    }
}
//Fonction permettant d'initialiser la vue
private void init() {
    inflate(getContext(), R.layout.plantview, this);
    nameView = findViewById(R.id.nomPlante);
    infoView = findViewById(R.id.info);
    degreeView = findViewById(R.id.degree);
    imageView = findViewById(R.id.image);
    planteBg = findViewById(R.id.plante);

    setupView();
}
//Fonction permettant d'"installer" la vue
private void setupView() {

    nameView.setText(nameText);
    infoView.setText(infoText);
    degreeView.setText(degreeText +" °C");
    imageView.setImageDrawable(plantImage);
    planteBg.setOnClickListener(new OnClickListener() {
        @SuppressLint("NewApi")
        @Override
        public void onClick(View v) {

            if(isExtended){
                TransitionManager.beginDelayedTransition(planteBg, new TransitionSet()
                        .addTransition(new ChangeBounds()));


                ViewGroup.LayoutParams params = planteBg.getLayoutParams();
                params.height = LayoutParams.WRAP_CONTENT;

                planteBg.setLayoutParams(params);
                isExtended = false;

            }else if(isExtended == false) {

                TransitionManager.beginDelayedTransition(planteBg, new TransitionSet()
                        .addTransition(new ChangeBounds()));

                ViewGroup.LayoutParams params = planteBg.getLayoutParams();
                params.height = 500;
                planteBg.setLayoutParams(params);
                isExtended = true;
            }
        }
    });
}

/**
 * Fonctions permettant de changer les attributs
 * de la vue par programmation
 */
public void setName(String name) {
    nameView.setText(name);
}
public void setInfo(String info){
    infoView.setText(info);
}
public void setDegree(String degree){
    degreeView.setText(degree+" °C");
}
public void setImage(Bitmap image){
    imageView.setImageBitmap(image);
}

Solution

  •     On your setters after you've set the text call invalidate();
        public void setName(String name) {
        nameView.setText(name);
    invalidate();
        }
        public void setInfo(String info){
            infoView.setText(info);
    invalidate();
        }
        public void setDegree(String degree){
            degreeView.setText(degree+" °C");
    invalidate();
        }
        public void setImage(Bitmap image){
            imageView.setImageBitmap(image);
    invalidate();
        }