public
Last active

Image view with long press hint

  • Download Gist
HintedImageButton.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
package in.lib.view;
 
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.Toast;
 
/**
* Extended image view to show the content description in a Toast view when
* the user long presses.
*
* Note: `android:contentDescription` must be set in order for the toast to
* work
*
* @author Callum Taylor <http://callumtaylor.net>
*/
public class HintedImageButton extends ImageButton implements OnLongClickListener
{
private OnLongClickListener mOnLongClickListener;
 
public HintedImageButton(Context context)
{
super(context);
 
setOnLongClickListener(this);
}
 
public HintedImageButton(Context context, AttributeSet attrs)
{
super(context, attrs);
 
setOnLongClickListener(this);
}
 
@Override public void setOnLongClickListener(OnLongClickListener l)
{
if (l == this)
{
super.setOnLongClickListener(l);
return;
}
 
mOnLongClickListener = l;
}
 
@Override public boolean onLongClick(View v)
{
if (mOnLongClickListener != null)
{
if (!mOnLongClickListener.onLongClick(v))
{
handleLongClick();
return true;
}
}
else
{
handleLongClick();
return true;
}
 
return false;
}
 
private void handleLongClick()
{
String contentDesc = getContentDescription().toString();
if (!TextUtils.isEmpty(contentDesc))
{
int[] pos = new int[2];
getLocationInWindow(pos);
 
Toast t = Toast.makeText(getContext(), contentDesc, 1200);
t.setGravity(Gravity.TOP | Gravity.LEFT, pos[0] - ((contentDesc.length() / 2) * 12), pos[1] - 128);
t.show();
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.