Search code examples
androidandroid-custom-view

Custom Layout in android: button is not seen


So guys,

I made custom layout which extends ViewGroup.Earlier, I made custom view, but later I discovered that it can't contain children like buttons. I did extending viewgroup because I want to add buttons, like in linear layout, just with property of glowing on touch. Anything else will like in linear layout.

WrapLayout class:

public class WrapLayout extends ViewGroup { 
boolean drawGlow = false;
float glowX = 0;
float glowY = 0;
float radius = 20;
Paint paint = new Paint();
{
    paint.setAntiAlias(true);
    paint.setColor(3515877);
    paint.setAlpha(50);
};
public WrapLayout(Context context, AttributeSet attrs) {

    super( context, attrs );
    setWillNotDraw(false);
}
public WrapLayout(Context context, AttributeSet attrs, int defStyle) {

    super( context, attrs, defStyle );
    setWillNotDraw(false);
}
public WrapLayout(Context context) {
    super(context);
    setWillNotDraw(false);
}
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if(drawGlow)
        canvas.drawCircle(glowX, glowY, radius, paint);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    // TODO Auto-generated method stub
}
@Override
public boolean onTouchEvent(MotionEvent event){
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        drawGlow = true;
    }else if(event.getAction() == MotionEvent.ACTION_UP)
        drawGlow = false;
    glowX = event.getX();
    glowY = event.getY();
    this.invalidate();
    return true;
}

}

Then I initialized my activity_main.xml file like this:

<com.example.secondcustomlayout.WrapLayout
.....
>
<Button
android:id="@+id/button1"
...
</Button>
</com.example.secondcustomlayout.WrapLayout>

MainActivity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

RESULT: Nothing but blank screen.

SOLUTION: What can I do?

With regards


Solution

  • you override onLayout but you not assign the position to the children, that's why you do not see nothing. If you don not want to override onLayout, you should use (extends) one of the concrete implementation of ViewGroup, provided by the framework