Question

In my application i can scaling an ImageView using this class

`public class resizeAvatar extends View {

private final Drawable sfondoAvatar;

public resizeAvatar(Context context) {
    super(context);

    sfondoAvatar = context.getResources().getDrawable(R.drawable.main_voice);
    setBackgroundDrawable(sfondoAvatar);

}

public resizeAvatar(Context context, AttributeSet attrs) {
    super(context, attrs);

    sfondoAvatar = context.getResources().getDrawable(R.drawable.main_voice);
    setBackgroundDrawable(sfondoAvatar);
}

public resizeAvatar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    sfondoAvatar = context.getResources().getDrawable(R.drawable.main_voice);
    setBackgroundDrawable(sfondoAvatar);
}

@Override
protected void onMeasure(int widthMeasureSpec,
                                   int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = width * sfondoAvatar.getIntrinsicHeight() / sfondoAvatar.getIntrinsicWidth();
    setMeasuredDimension(width, height);
}

} `

And writing in the layout:

<com.myapp.app.resizeAvatar android:id="@+id/mainvoice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_centerVertical="true"/>

But in this way i can't have the onClick event.. i need do the same but using an ImageButton.. Is there a way?

Was it helpful?

Solution

In the activity where you include this view and in the onCreate method do the following:

resizeAvatar myMainvoice  = (resizeAvatar) v.findViewById(R.id.mainvoice);
myMainVoice.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            { 
               //place your on click logic here 
            }
        });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top