Reputation: 60
In my android-app I'm trying to create a button with two lines of text. One will display what the button is doing when (short) clicked, the other one should show the effect when long-clicked and that the button can be long-clicked. Just like on the the standerd keyboard of the android system (see picture).
What i've tried:
Bitmap canvasBitmap = Bitmap.createBitmap(64, 24, Bitmap.Config.ARGB_8888);
Canvas imageCanvas = new Canvas(canvasBitmap);
Paint imagePaint = new Paint();
imagePaint.setTextAlign(Align.CENTER);
imagePaint.setTextSize(14f);
imageCanvas.drawText(String, pxwidth/2, pxheight, imagePaint);
image = new BitmapDrawable(canvasBitmap);
((TextView) findViewById(R.id.button)).setCompoundDrawables(null,image,null,null);
But it isn't suported at my currend API version (API 9 / GINGERBREAD / Android 2.3)
Can you please help me out?
Upvotes: 2
Views: 434
Reputation: 60
I used this Custom View
as solution (with thanks to Gabe Sechan for the idea):
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.widget.Button;
class LongClickButton extends Button {
private String mLongClickString;
private String mOnLongClick;
private Paint mTextPaint;
public LongClickButton(Context context, AttributeSet attrs){
super(context, attrs);
mTextPaint = new Paint();
mTextPaint.setTextSize(this.getTextSize()/2);
mTextPaint.setColor(Color.DKGRAY);
mTextPaint.setShadowLayer(0, 1, 1, Color.GRAY);
mTextPaint.setTextAlign(Align.RIGHT);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LongClickButton, 0, 0);
try{
mLongClickString = a.getString(R.styleable.LongClickButton_longClickText);
mOnLongClick = a.getString(R.styleable.LongClickButton_onLongClick);
} finally {
a.recycle();
}
}
public String getLongClickString(){
return mLongClickString;
}
public String getOnLongClick(){
return mOnLongClick;
}
public void setLongClickString(String LongClickString){
mLongClickString = LongClickString;
invalidate();
requestLayout();
}
public void setOnLongClick(String OnLongClick){
mOnLongClick = OnLongClick;
invalidate();
requestLayout();
}
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawText(mLongClickString, this.getWidth()-(this.getPaddingRight()/4*3), (this.getPaddingTop()/2)+mTextPaint.getTextSize(), mTextPaint);
}
}
Upvotes: 0
Reputation: 93561
If you need to target 9, then you can't use that function, and you'll need to use another technique. One way I can think of is to use an ImageButton instead of a button, prerender the image of the entire button, and apply it to the new ImageButton in xml.
Upvotes: 1