0

I have created a custom MyAspectButton that keeps the aspect:

public class MyAspectButton extends Button
{
    private float m_aspect = -1.f;

    // ...
    // ... Constructors, setters/getters ...
    // ...

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        if (aspect >= 0.f)
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            final int measuredWidth = getMeasuredWidth();
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(measuredWidth, MeasureSpec.EXACTLY);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.round(measuredWidth * aspect), MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

Now I want to have MyAspectLinearLayout, MyAspectRelativeLayout, and so on. Their onMeasure method will be the same.

How can I implement a bunch of these classes with as little copy-paste as possible?

I am aware of a concept of generic classes in Java, but here I would have to inherit from template parameter that compiler does not let me to do:

public class MyAspectWidget<T> extends T
{
    // ...
}

does not compile.

6
  • just write this measurement logic in standalone static class and use its methods at the onMeasure Commented Apr 25, 2015 at 13:11
  • 2
    @user2418306 this won't work, because OP already has base classes: Button, RelativeLayout, LinearLayout and so on. Commented Apr 25, 2015 at 13:13
  • @nikis AbstractLayoutButton extends Button, SpecificLayoutButton extends AbstractLayoutButton. Explain to me why this won't work? Commented Apr 25, 2015 at 13:18
  • 1
    @user2418306 and how does it solve the issue? every time in AbstractLayoutButton, AbstractLinearLayout, AbstractRelativeLayout he will have to copy-paste the same measurement code. Commented Apr 25, 2015 at 13:21
  • @nikis My bad. I thought OP wants a bunch of buttons for different layouts. Though Button and Layout are not similar classes, are they? Commented Apr 25, 2015 at 13:39

1 Answer 1

1

If I'm understanding correctly, you want to inherit from a bunch of different base classes, but apply the same behaviour to all of them:

MyAspectButton extends Button
MyAspectLinearLayout extends LinearLayout
MyAspectRelativeLayout extends RelativeLayout

What you are looking for is multiple inheritance, which Java does not support. Your only option is to have a single class that performs the measuring logic, which each of your child classes' onMeasure method call into:

public class MyAspectButton extends Button
{
    private float m_aspect = -1.f;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(MyMeasuringClass.MeasureWidth(this, widthMeasureSpec), MyMeasuringClass.MeasureHeight(this, heightMeasureSpec));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I also want to read m_aspect from attributes in public MyAspectButton(Context context, AttributeSet set). If I undestand you correcly, I will have to copy-paste all this stuff in all the custom classes as well?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.