25

I've been creating custom views for a while now, but never got to make them in a way that the Android Studio preview displays them correctly.

I initialize the custom attributes using:

if (!isInEditMode()) {
...
}

But this results in an invisible component on preview. Any ideas on how to achieve this?

Zoe
  • 27,060
  • 21
  • 118
  • 148

3 Answers3

41

You need to rebuild project. After that custom views in preview will work

mohax
  • 4,435
  • 2
  • 38
  • 85
4

Have a look here. I don't think you need to use that if statement. I never used it, and views always get displayed in previews

Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
  • Thanks for the info, I use isInEditMode, because if I don't the preview displays an error suggesting to use that. So I'm not sure what stuff can be done or not in a view constructor in order to avoid these errors. – Maximiliano Ambrosini Feb 17 '17 at 14:37
  • [here](http://stackoverflow.com/questions/15423149/how-to-use-isineditmode-to-see-layout-with-custom-view-in-the-editor) you might find a better answer – Luca Nicoletti Feb 17 '17 at 14:37
0

When inflating your layout in your custom View clss, try with attachToRoot set to true.

Michał Dobi Dobrzański
  • 1,449
  • 1
  • 20
  • 19