Identifying the error in using a View constructor missing AttributeSet - android

As a beginning Android programmer who is not a beginning programmer, I am alarmed by the amount of time it took me until I realized that the crash was resulting from omitting to use the constructor taking AttributeSet as a second parameter and writing instead the following code for a custom view defined in an XML layout.
////////MyView.java////////
public class MyView extends View {
public SimpleView(Context context) {
super(context);
}
...
}
My question is this: What could I have done (in Eclipse, the ADT variant) to be able to determine this error quickly? I look rather carefully at lint messages and was hoping that that habit would spare me spending hours for a single error.
The manual does of course clearly state that custom views defined in an XML layout should use the (Context, AttributeSet) variant of the View constructor, but it is hopeless to program at any level while recalling with precision every last detail in the reference pages.

There should be the following error in the Graphical Layout:
Custom view MyView is not using the 2- or 3-argument View
constructors; XML attributes will not work
But I agree that a compile-time error would be better...

Related

Passing values to custom view in android

Cheers,
I have an app that receives user input (2 numbers, width and height) and in theory depending on that input I have a custom view that should draw a grid (width and height).
Note:
These 2 values should be received before view attempts to draw itself.
These 2 values aren't constant and therefore I don't think XML approach can help.
I was told that adding another parameter to the View constructor is evil.
Do not confuse my 2 values with canvas.getWidth or etc.. these are values needed simply to draw something, nothing else.
My View is also a ViewGroup.
Main issue arises with Views declared in XML files.
I have temporarily solved this issue by making an SchemeContext class which contains those 2 static values and I simply set them in onCreate (before onCreateView) then use them in custom View onDraw when needed (SchemeContext.width). This is not really what people would call OOP I'm forcing global variables upon java and those are set on time because of the fragment lifecycle.
I've seen this answer How to pass variables to custom View before onDraw() is called?.
But it's more of a workaround than a solution (and probably not the fastest one). There has to be a sensible solution I don't think 3D games on android resort to these workarounds (SurfaceView with OpenGL is still a View right? :d).
If there is an obvious solution and this is an obvious double I'll remove the question.
I haven't tried this, but I think it would be possible to do this fairly cleanly by overriding the LayoutInflater.Factory. That way, you can intercept the creation of the views that need additional parameters passed to their constructors, and let the rest of them fall through to default inflation.
For example, in your activity, before you inflate the view hierarchy:
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
MyInflaterFactory factory = new MyInflaterFactory();
// Pass information needed for custom view inflation to factory.
factory.setCustomValue(42);
inflater.setFactory(factory);
For your implementation of the factory:
class MyInflaterFactory implements LayoutInflater.Factory {
public void setCustomValue(int val) {
mCustomVal = val;
}
#Override
public View onCreateView (String name, Context context, AttributeSet attrs) {
if (name.equals("com.package.ViewWithCustomCreation")) {
return new ViewWithCustomCreation(context, attrs, mCustomVal);
}
return null;
}
private int mCustomVal;
}
I was told that adding another parameter to the View constructor is evil.
Nonsense.
There are three (and in the newest APIs, four) different View constructors, each used in a different situation. (See this thread.) If you wanted to be able to declare your view in XML, for example, then you'd have to provide a constructor with exactly the right parameters, and have it call the corresponding superclass constructor. But there's nothing wrong with defining your own constructor (or even several of them) that call the superclass constructor intended for creating views programmatically.
The overriding principle is that every object must be valid when its constructor returns. So unless you can provide reasonable default values in your constructor, you have little choice but to accept the object's properties as constructor parameters.

How to find a View in a BridgeContext?

TL;DR: Is there anything in com.android.layoutlib.bridge.android.BridgeContext that can substitute for Activity#findViewById(...)? I've looked at the source, but I can't find anything.
When running on a real device, an attached view's #getContext() returns the Activity. The view can cast it and call #findViewById(...) to obtain a reference to some other view.
But when running in a WYSIWYG editor, #getContext() returns an instance of a different class. I'm getting com.android.layoutlib.bridge.android.BridgeContext. This class isn't part of the public API, so I'm planning to access it via reflection and degrade gracefully if the implementation changes.
If you're wondering why my view wants a reference to another view... I've created a view that appears to have a hole in it. It works by delegating its drawing to another view. If the view with the hole is placed on top of other views, then it appears to punch a hole through any views beneath it, all the way down to the view it's using for drawing. It works perfectly on a real device, but it would be nice to have it also work in the WYSIWYG editor.
It's bad to assume that View.getContext(), or any other platform method that returns Context, can be cast directly to more concrete classes, like Activity. There exist classes like ContextThemeWrapper which can easily destroy your assumption.
I would recommend restructuring what you are doing so that you have a parent layout that can act as an intermediary for the hole-y View and what's below it.
Or you could have a setter which would provide the View for you.
A last option is to call View.getParent() a bunch of times to get the root View and call findViewById() on that:
ViewParent parent;
while(getParent() != null) {
parent = getParent();
}
View root = (View) parent;
root.findViewById(R.id.my_view);
BTW, BridgeContext is used in the WYSIWYG in place of Activity because it only mocks the Android View/Layout/Rendering system, it doesn't emulate it completely. This can be seen in other ways like how it renders shadows or shape drawable rounded corners.
I awarded the bounty to dandc87 because his answer led me to the solution. However, the code snippet in his answer crashes with a ClassCastException because the root ViewParent is not a View. The mods keep rejecting my edits, so here's the complete and correct solution:
private View findPeerById(int resId) {
View root = this;
while(root.getParent() instanceof View) {
root = (View) root.getParent();
}
return root.findViewById(resId);
}

No such method exception in RangeSeekBar

I've referred RangeSeekBar to implement Seekbar with two thumbs.
I'm getting following error in log:
Caused by: java.lang.NoSuchMethodException: RangeSeekBar(Context,AttributeSet)
and if I try to add following method in RangeSeekBar.java, I'm getting error as : The blank final field numberType may not have been initialized
public RangeSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
I'm banging my head from few hours, but haven't find any fruitful solution.
Any help appreciated.Any other way to implement SeekBar With Two thumb is also welcome.
Any custom view in android should have a constructor like you wrote. So, keep it. The problem is, you have a final field in your class, that should be initialized in constructor. So, remove final modifier from that variable, or set it to some value in constructor.
About RangeSeekBar:
this class is using generic types for detecting what type of number you want to use (float, double, or just int values). So, do not put this as a view in xml layout file, and add it in code like this:
someViewGroup.addView(new RangeSeekBar<Integer>(0, 10, context));
which someViewGroup is the view you want to put RangeSeekBar in it. (its parent).

Android : how to create custom component

Any one please guide me,
How to create a custom component in xml and java file
pass attributes from xml (this i somewhat completed)
how to pass attributes dynamically from new customClass() -- constructor method. -- i dont know how to implement this point
for Eg : I created a custom component with two textview as a single component. In xml i created two component by xml attributes. Now i want to create two new component by java code
by calling the constructor of my customClass.java I dont know how to create a new component dynamically from java code and how to display that in a view(Say a relative layout) Please help me provide any link or blog.
I have spent 1 week for this, But i didnt get any solution please help me
its very simple:
in your layout xml file simply put the following lines of xml code:
<com.example.project.MyComponent
android:id="#+id/myid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Now, write a class named as your component:
public class MyComponent extends RelativeLayout {
public MyComponent(Context context, AttributeSet attr) {
super(context,attr);
}
#Override
public void onFinishInflate() {
// this is the right point to do some things with View objects,
// as example childs of THIS View object
}
}
Remember the constructor: this constructor is needed by the LayoutInflater to
find your component. And, dont forget to call super(...) when required.
You can do this by calling the constructor with context in its parameter and then setting the attributes with getter setters. You can find a good tutorial at Android tech point
MyComponent mycomponent = new MyComponent(context);
myComponent.setFirstTextView("text1");
myComponent.setSecondTextView("text2");
And then finally
layout.addView(myComponent);

android getting variables to constructor of custom view

android newbie here.
My first game involves a custom view, which is going to draw a game board and some scoreboards on the screen. I need to know how many players there are in order to get the number of scoreboards up, and I need this to be in known in the constructor of the custom view, so that appropriate variables are initialised on time.
My current implementation is like this, is this the correct way to get variables into the custom view constructor?...
I instantiate the custom view from my activity like this:
numPlayers=2;
setContentView(R.layout.gamescreen);
mBoardView = (BoardView) findViewById(R.id.board_view);
And in the custom view constructor:
public BoardView(Context context, AttributeSet attrs){
super(context, attrs);
vNumPlayers = ((GuappsXOMainGame)getContext()).getNumP();
That's how I have it now and it seems to work well enough, but is it better to be doing something along the lines of the answer to this question:?
android:how to instantiate my custom view with attributeset constructor
When a custom view (or SurfaceView) is the only view, often in games, then it is very easy to pass parameters like level, players, sound on etc by creating an instance of this view before setContentView(). An example:
MyView myView = new MyView(level, players, soundOn);
setContentView(myView);
In your case, your custom view is instantiated when the layout is created, hence you have to communicate back to the activity object to get the values. This is also possible. I have done this by setting static variables in the activity before calling setContentView(my_layout) then in the constructor for the custom view just say level = MyActivity.level
Or perhaps the way you are doing already where you obtain the instance of the activity and call a public method or variable.
In the link you provided an attribute is "hard-wired" into the XML layout. I don't see the advantage of that, when one can simply enter the value into the custom view class itself as a "final" variable.

Categories

Resources