How to Use Isineditmode() to See Layout with Custom View in the Editor

How to use isInEditMode() to see layout with custom View in the editor

isInEditMode()should be used inside the Custom View constructor.
Try the following code:

     public class GraphView extends View implements Grapher
{

public GraphView(Context context, AttributeSet attrs) {
super(context, attrs);
if(!isInEditMode())
init(context);
}

public GraphView(Context context) {
super(context);
if(!isInEditMode()){
touchHandler = new TouchHandler(this);
init(context);
}
}

What is isInEditMode() and if(isInEditMode()) return;

As you have no answers for #2 and #3, there it is:

If you have a single instruction in your if, you can omit the curly braces. Plus java doesn't work by indentation but with braces and semi-colons. So you can put the single instruction on the same line as the if, it won't make any differences, as long as there is the semi-colon.

For the return;, this method returns void, so you can't put any object to return. This however allows you to exit the method, i.e. when you have a bad/unexpected value, as return immediately ends the method the moment it is called. return; means about the same as return void; (however I'm don't know if this would compile).

View.isInEditMode() doesn't skip code

I've replaced the problematic code:

 ViewGroup v = (ViewGroup)MyBanner.this;
v.addView(rl);

to the following one:

 this.addView(rl);

Now eclipse shows my view without errors.



Related Topics



Leave a reply



Submit