0

I am trying to add a variable within the class View, that I can reference in subViews like Button, Label etc.

I have done this successfully in Swift, but having trouble in Java. This is what it looks like in Swift.

Swift code to add variable without creating custom view

This code allows me to set styleName within all views that extend UIView. Allowing me to remove redundant custom views. Eg. myButton.styleName = "Hello World"

I have seen that extension is not possible. As seen here. Not sure if there is another way to achieve this. Thanks in advance!

Matthew Mitchell
  • 514
  • 4
  • 14
  • You can't add fields to existing Java classes. What *goal* are you trying to achieve, there's probably another way to reach that. This feels like a [XY problem](http://xyproblem.info). – Joachim Sauer Apr 02 '20 at 14:47
  • To put it simply I am trying to give a unique styleName to each control on my different activities, those styleNames are static variables in a different class which can be changed based on themes for different clients, the goal is to be able to search for different control attributes using the styleName, which return attributes like background and foreground color. Each control is then designed in code based on those attributes. The benefits of having the styleName within the control attributes is I can access it at any given time and not having to declare every styleName within each class. – Matthew Mitchell Apr 02 '20 at 15:01

0 Answers0