Java tutorial
//package com.java2s; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.widget.ImageButton; public class Main { public static ImageButton makeImageButton(Context context, int id, int resId, int width, int height, OnClickListener onClickListener, OnTouchListener onTouchListener) { Drawable icon; ImageButton button = new ImageButton(context); button.setId(id); if (onClickListener != null) button.setOnClickListener(onClickListener); button.setBackgroundColor(Color.TRANSPARENT); icon = context.getResources().getDrawable(resId); icon.setBounds(0, 0, width, height); Bitmap iconBitmap = ((BitmapDrawable) icon).getBitmap(); Bitmap bitmapResized = Bitmap.createScaledBitmap(iconBitmap, width, height, false); button.setImageBitmap(bitmapResized); button.setVisibility(View.VISIBLE); if (onTouchListener != null) button.setOnTouchListener(onTouchListener); return button; } }