Android Custom Layout - Ondraw() Never Gets Called

Android Custom Layout - onDraw() never gets called

By default, onDraw() isn't called for ViewGroup objects. Instead, you can override dispatchDraw().

Alternatively, you can enable ViewGroup drawing by calling setWillNotDraw(false) in your TableView constructor.

EDIT:

For #2:
- Initialize it in the constructor, then just call rect.set() in your onLayout() method.

For #3:
- Yes, as far as I'm aware the super call will handle it, you shouldn't have to handle that unless you need to customize the way the children are drawn.

Why is the onDraw Not Being Called in Custom View for Android?

You can remove View_path = new Drawpath_view(MainActivity.this); from your code,
as you already attached view in xml.

onDraw doesn't get called on custom view?

Some improvements/problems you can make/fix...

You can move these lines to your constructor:

if(colorString != null)
color = Color.parseColor("#" + colorString);

Since this colorString never changes and the way you are doing it now you are calculating it every time onDraw is called (and he will be called a lot of times).

Second please move your super.onDraw(canvas) to your first line.

Third you need to define your LayoutParams on your constructor. If a View has 0 width/height its draw will never be called!

this.setLayoutParams(new LayoutParams(150, 150));

At last please make sure you are using your RoundTagItem. You can add this view to your xml using a tag like this: <your.package.RoundTagItem> where your.package is the package that you are using (com.something.blabla). If you use it like this please be sure you define the layout_width and the layout_height.

You can also add your view programmatcly by adding to your root view (you get it by using findViewById(R.layout.your_root)) or by setting your view as the main content.

RoundedTagItem myView = new RoundedTagItem(this);
setContentView(myView);

Android: View onDraw never called

Try this code working for me.

public class MainActivity extends ActionBarActivity
{
private RelativeLayout rl;
private GridLayout glActionMenu;
private int width;
private MyCustomView customView;
private int fromX;
private int fromY;
private int toX;
private int toY;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// MyCustomView customView = new MyCustomView(this, 0, 0, 100,200);
// setContentView(customView);

setContentView(R.layout.activity_main);
rl = (RelativeLayout) this.findViewById(R.id.mainContent);

// RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(500, 500);
// customView = new MyCustomView(MainActivity.this, 0, 0, 100, 200);
// customView.setBackgroundColor(Color.DKGRAY);
// rl.addView(customView);

rl.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
@SuppressLint("NewApi")
@Override
public void onGlobalLayout()
{
rl.getViewTreeObserver().removeOnGlobalLayoutListener(this);
setWidth(rl.getMeasuredWidth());
}
});
// setWidth(rl.getMeasuredWidth());
}

private void setWidth(int width)
{
this.width = width;

glActionMenu = (GridLayout) this.findViewById(R.id.glActionMenu);

for (int i = 0; i < glActionMenu.getChildCount(); i++)
{
GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
lp.width = width / 5;
lp.height = lp.width;

if ((i >= 5 && i < 10) || (i >= 15 && i < 20))
{
lp.height = lp.width / 2 + lp.width / 4;
}
glActionMenu.getChildAt(i).setLayoutParams(lp);
}

glActionMenu.getChildAt(glActionMenu.getChildCount() - 1).addOnLayoutChangeListener(new View.OnLayoutChangeListener()
{

@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
{
Toast.makeText(v.getContext(), "hello", Toast.LENGTH_LONG).show();

ImageButton ib1 = (ImageButton) findViewById(R.id.ibOpenPictureActivity);

ib1.setBackgroundColor(Color.RED);
ImageButton ib2 = (ImageButton) findViewById(R.id.ibOpenAlbumActivity);
ib2.setBackgroundColor(Color.GREEN);

glActionMenu.setBackgroundColor(Color.WHITE);

fromX = ib1.getLeft() + ib1.getWidth() + rl.getLeft();
fromY = ib1.getTop() + ib1.getHeight() / 2 + glActionMenu.getTop();
toX = ib2.getLeft();
toY = ib2.getTop() + ib2.getHeight() / 2;

// customView = new MyCustomView(MainActivity.this, 0, 0, 100, 200);
// customView.setBackgroundColor(Color.DKGRAY);
// rl.addView(customView);
// customView.invalidate();
}

});
glActionMenu.postDelayed(new Runnable()
{

@Override
public void run()
{
customView = new MyCustomView(MainActivity.this, fromX, fromY, toX, toY);
rl.addView(customView);

}
}, 10);
}
}

Why onDraw() is not called if Custom View extends ViewGroup?

that's the expected behavior, unless the flag setWillNotDraw is forced to false. ViewGroup are not expected to do any drawing on their own, but to delegate the drawing to their children. If you want to override this behavior, you can either call setWillNotDraw(false) or override dispatchDraw(Canvas canvas) instead of onDraw



Related Topics



Leave a reply



Submit