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
Viewpager Intercepts All X-Axis Ontouch Events. How to Disable
Illegalstateexception: Can't Change Container Id of Fragment
Mkdir() Works While Inside Internal Flash Storage, But Not Sd Card
Autocompletetextview Not Completing Words Inside Parentheses
How to Create Xmpp Chat Client for Facebook
Android Runtime Layout Tutorial
Android Expandablelistview and SQLite Database
Android 1.5: Reading Sms Messages
Libgdx Spritebatch Render to Texture
Using Google Account to Log in to an Android Application
Alarmmanager Android Every Day
How to Create Converter for My Class in Android Retrofit Library
How to Remove Shared Preference While Application Uninstall in Android
Android: Create Circular Image with Picasso