0

Trying to redraw on canvas when tab is selected:

@Override
public void onTabChanged(String tabId) {
   for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
        {
         mTabHost.getTabWidget().getChildAt(i).setSelected(false); //unselected
        }
         mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setSelected(true); // selected
}

Then on the custom view i do:

public class TabIndicator extends LinearLayout {
...
public void setSelectedTab(boolean isSelected){
    this.isSelected = isSelected;

    if(isSelected){
        this.invalidate();
        this.postInvalidate();
    }
}
 @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if(isSelectedTab()){
        Paint paint2 = new Paint();
        Path path2 = new Path();
        paint2.setStyle(Paint.Style.STROKE);
        paint2.setStrokeWidth(10);
        paint2.setColor(Color.parseColor(getResources().getString(R.string.NAVY_BLUE)));
        path2.moveTo(width*(1/4f), height);
        path2.lineTo(width*(3/4f), height);
        path2.close();
        canvas.drawPath(path2, paint2);
    }
}

Nothing happens even though draw is called repeatedly. (Checked via log.d) It only draws the first time when the Layout is created.

mmBs
  • 8,421
  • 6
  • 38
  • 46
JY2k
  • 2,879
  • 1
  • 31
  • 60

1 Answers1

2

You are extending a ViewGroup

http://developer.android.com/reference/android/view/ViewGroup.html

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

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

https://groups.google.com/forum/?fromgroups#!topic/android-developers/oLccWfszuUo

Check Romain Guy's comment

I could find a link for a similar question i answered before. Has an example do check the link below

onDraw method not called in my application

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256