Custom View in Layout Editor?

I have some subclasses of LinearLayout. How do I use them in the layout editor? Currently when I try to look at the design of my view I see this:

android.content.res.Resources$NotFoundException: Could not resolve resource value: 0x7F040157.
 at android.content.res.BridgeResources.throwException(BridgeResources.java:693)
 at android.content.res.BridgeResources.getString(BridgeResources.java:506)
 at com.synchronoss.dcs.DropShadowBlock.dispatchDraw(DropShadowBlock.java:50)
 at android.view.View.draw(View.java:13340)
 at android.view.ViewGroup.drawChild(ViewGroup.java:2929)
 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)
 at android.view.View.draw(View.java:13340)
 at android.view.ViewGroup.drawChild(ViewGroup.java:2929)
 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)
 at android.view.View.draw(View.java:13340)
 at android.view.ViewGroup.drawChild(ViewGroup.java:2929)
 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)
 at android.view.View.draw(View.java:13461)
 at android.view.View.draw(View.java:13342)
 at android.view.ViewGroup.drawChild(ViewGroup.java:2929)
 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)
 at android.view.View.draw(View.java:13340)
 at android.view.ViewGroup.drawChild(ViewGroup.java:2929)
 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)
 at android.view.View.draw(View.java:13461)
 at android.view.View.draw(View.java:13342)
 at android.view.ViewGroup.drawChild(ViewGroup.java:2929)
 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)
 at android.view.View.draw(View.java:13461)
 at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:568)
 at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:334)
 at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325)
 at org.jetbrains.android.uipreview.RenderService.createRenderSession(RenderService.java:127)
 at org.jetbrains.android.uipreview.RenderUtil.renderLayout(RenderUtil.java:151)
 at com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel$8.run(AndroidDesignerEditorPanel.java:340)
 at com.intellij.openapi.application.impl.ApplicationImpl$6.run(ApplicationImpl.java:465)
 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
 at java.util.concurrent.FutureTask.run(FutureTask.java:138)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
 at java.lang.Thread.run(Thread.java:680)
 at com.intellij.openapi.application.impl.ApplicationImpl$1$1.run(ApplicationImpl.java:153)



Howard
3 comments

Try to rebuild project. If it doesn't help, may you attach the code of your custom view?

0

The class is a subclass of a LinearLayout. I don't know if there is something else I need to put into the code so that it can draw something in the design editor. I don't have to have it draw its normal images in the layout editor, but not generating an exception would be a plus.

Thanks



public class DropShadowBlock extends LinearLayout {
 
 private static final int LOC_LEFT = 0;
 private static final int LOC_TOP = 1;
 private static final int LOC_RIGHT = 2;
 private static final int LOC_BOTTOM = 3;

 int m_nUpperShadowID = -1;
 int m_nLowerShadowID = -1;

 public DropShadowBlock(Context context) {
 super(context);
 }

 public DropShadowBlock(Context context, AttributeSet attrs) {
 super(context, attrs);
 }

 public DropShadowBlock(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 }
 public void setUpperShadow(int nUpper) {
 m_nUpperShadowID = nUpper;
 }

 public void setLowerShadowt(int nLower) {
 m_nLowerShadowID = nLower;
 }

 @Override
 protected void dispatchDraw(Canvas canvas) {
 //  call super.dispatchDraw first to draw everything underneath then lay our transparency on top
 super.dispatchDraw(canvas);

 String strData  = getResources().getString(R.string.option_display_scrollview_shadow);

 if(strData.equalsIgnoreCase("TRUE"))
 {
 Paint p = new Paint();
 p.setColor(Color.WHITE);
 p.setAlpha(150);
 
 int nID = R.drawable.shadow_bottom;
 if( m_nUpperShadowID!=-1 )
 nID = m_nUpperShadowID;
 
 Bitmap bm = getBitmap( nID );
 canvas.drawBitmap(bm, null, getDropShadowArea(LOC_BOTTOM, canvas, bm), p);
 
 nID = R.drawable.shadow_top;
 if( m_nLowerShadowID!=-1 )
 nID = m_nLowerShadowID;
 
 bm = getBitmap( nID );
 canvas.drawBitmap(bm, null, getDropShadowArea(LOC_TOP, canvas, bm), p);
 }

 }
 
 /**
  * Get the correct bitmap from context resources
  *
  * @param id
  *            Resource id
  * @return Bitmap to draw on the view
  */
 private Bitmap getBitmap(int id) {
 return BitmapFactory.decodeResource(getContext().getResources(), id );
 }
 
 /**
  * Get the Rect to draw the Bitmap in
  *
  * @param loc
  *            Left, Top, Right or Bottom (0,1,2,3)
  * @param canvas
  *            The canvas to we're drawing on
  * @param bm
  *            The bitmap we're drawing
  * @return Rect for the area we are drawing the Bitmap onto the canvas
  */
 private Rect getDropShadowArea(int loc, Canvas canvas, Bitmap bm) {
 switch (loc) {
 case LOC_LEFT :
 return new Rect(0, 0, bm.getWidth(), canvas.getHeight());
 case LOC_TOP :
 return new Rect(0, 0, canvas.getWidth(), bm.getHeight());
 case LOC_RIGHT :
 return new Rect(canvas.getWidth() - bm.getWidth(), 0, canvas.getWidth(), canvas.getHeight());
 case LOC_BOTTOM :
 return new Rect(0, canvas.getHeight() - bm.getHeight(), canvas.getWidth(), canvas.getHeight());
 default :
 return null;
 }
 }

}



and here is what is in the xml of the layout:

  <com.synchronoss.dcs.DropShadowBlock
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="0.45"
     android:orientation="vertical" >


 </com.synchronoss.dcs.DropShadowBlock>  
0

For me it works. As you can see in the stacktrace the problem is that the IDE cannot find "option_display_scrollview_shadow" string resource. Please check that it is available (for example, it may be not available for the configuration chosen in the UI designer). BTW, you may use View#isInEditMode() to execute or not execute specific code in the design mode.

0

Please sign in to leave a comment.