Creating Static Label




In this tutorial, I am going to explain custom label.

For drawing label, we have to draw two same round rectangle with two differnt paint objects. First paint object will fill the inside color


Paint textDialog = new Paint();
textDialog.setARGB(255, 220, 220, 220);
textDialog.setAntiAlias(true);


and second paint object for the border color, here I am giving green color for border,


Paint borderDialog = new Paint();
borderDialog.setARGB(255, 0, 255, 0);
borderDialog.setStyle(Style.STROKE);
borderDialog.setStrokeWidth(3.5f);


Now, for drawing in onDraw(), we have to draw two round rectangle as,


canvas.drawRoundRect(rectDialog, 7, 7, textDialog);
canvas.drawRoundRect(rectDialog, 7, 7, borderDialog);

rectDialog, is the area on which, I am going to draw, here its

RectF rectDialog = new RectF(100, 100, 200, 130);

and 7 is the radius for the round rectangle.

For drawing inside the label, I am creating another Paint with some other funcionalities.

Paint textPaint = new Paint();
textPaint.setARGB(255, 0, 0, 255);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
textPaint.setTextSize(15);
textPaint.setAntiAlias(true);

Then, in onDraw, I am writing the text as,

canvas.drawText("Androidians", 105, 120, textPaint);

The complete source code is,


public class AndLabel extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ViewLabel(this));
}

class ViewLabel extends View {

Paint textDialog;
Paint borderDialog;
Paint textPaint;
RectF rectDialog;
public ViewLabel(Context context) {
super(context);
textDialog = new Paint();
textDialog.setARGB(255, 220, 220, 220);
textDialog.setAntiAlias(true);

textPaint = new Paint();
textPaint.setARGB(255, 255, 0, 0);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
textPaint.setTextSize(15);
textPaint.setAntiAlias(true);

borderDialog = new Paint();
borderDialog.setARGB(255, 0, 255, 0);
borderDialog.setStyle(Style.STROKE);
borderDialog.setStrokeWidth(3.5f);

rectDialog = new RectF(100, 100, 200, 130);
}

public void onDraw(Canvas canvas) {
super.onDraw(canvas);

canvas.drawRoundRect(rectDialog, 7, 7, textDialog);
canvas.drawText("Androidians", 105, 120, textPaint);
canvas.drawRoundRect(rectDialog, 7, 7, borderDialog);
}
}
}

Comments

Popular posts from this blog

AlarmManager and Notification in Android

Swipe Action and ViewFlipper in Android

Difference between SurfaceView and View in Android