Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter AttributeSet values in android

I am having custom view which will take attribute set(xml value) as constructor value

public CustomView(Context context)  // No Attributes in this one.
{
    super(context);
    this(context, null, 0);
}

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this(context, attrs, 0)
}

public CustomView(Context context, AttributeSet attrs, int default_style) {
    super(context, attrs, default_style);
    readAttrs(context, attrs, defStyle);
    init();
}

In Fragment class i am setting the view as

CustomView customView = (CustomView) view.findViewById(R.id.customView); 

where custom view contains various value such as height,width,padding etc.

i want to modify those values based on required condition and set it back to custom view. I placed setting width height code in onDraw method and called invalidte view. But above method will set the every time if i called invalidate method in CustomView class. how to overcome this so that i can pass modified attribute set value in constructor only.?

Edit: I need to modify the view values(initialize with new values) which is set during attribute constructor so that i will get a refreshed view with a new values. Override @OnDraw or 'Invalidate' is not a good function for me where inside invalidate i have written the methods which will execute in each second interval.

like image 691
Madhukar Hebbar Avatar asked Jan 25 '26 13:01

Madhukar Hebbar


1 Answers

I see that your CustomView can have multiple attributes and you want to modify some of these attributes based on some condition and pass this in the constructor.

Few best practices while designing a custom view:

  1. If you have custom attributes, make sure that you expose them via setters and getters. In your setter method, call invalidate();
  2. Don't try modifying any attributes inside onDraw() or onMeasure() methods.
  3. Try your best to avoid writing Custom constructors for your Custom View.

So the ideal way to solve your problem is to instantiate your CustomView and then modify the attributes, either externally (in your Activity or Fragment), or have a method inside the CustomView.java and then invoke it externally. Doing this will still give you the same result you are looking for.

like image 56
Henry Avatar answered Jan 27 '26 01:01

Henry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!