Java tutorial
/* * This is the source code of Telegram for Android v. 3.x.x. * It is licensed under GNU GPL v. 2 or later. * You should have received a copy of the license in this archive (see LICENSE). * * Copyright Nikolai Kudashov, 2013-2016. */ package org.telegram.ui.Cells; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; import android.graphics.drawable.Drawable; import android.support.v4.content.ContextCompat; import android.util.TypedValue; import android.view.Gravity; import android.widget.FrameLayout; import android.widget.TextView; import org.telegram.messenger.AndroidUtilities; import org.telegram.messenger.LocaleController; import org.telegram.messenger.R; import org.telegram.ui.Components.ForegroundFrameLayout; import org.telegram.ui.Components.LayoutHelper; public class TextColorCell extends ForegroundFrameLayout { private TextView textView; private boolean needDivider; private int currentColor; private Drawable colorDrawable; private static Paint paint; public TextColorCell(Context context) { super(context); setElevation(AndroidUtilities.dp(2)); setBackgroundColor(ContextCompat.getColor(context, R.color.card_background)); if (paint == null) { paint = new Paint(); paint.setColor(ContextCompat.getColor(context, R.color.divider)); paint.setStrokeWidth(1); } colorDrawable = getResources().getDrawable(R.drawable.switch_to_on2); textView = new TextView(context); textView.setTextColor(ContextCompat.getColor(context, R.color.primary_text)); textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16); textView.setLines(1); textView.setMaxLines(1); textView.setSingleLine(true); textView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL); addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, 17, 0, 17, 0)); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(48) + (needDivider ? 1 : 0), MeasureSpec.EXACTLY)); } public void setTextAndColor(String text, int color, boolean divider) { textView.setText(text); needDivider = divider; currentColor = color; colorDrawable.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY)); setWillNotDraw(!needDivider && currentColor == 0); invalidate(); } @Override protected void onDraw(Canvas canvas) { if (needDivider) { canvas.drawLine(getPaddingLeft(), getHeight() - 1, getWidth() - getPaddingRight(), getHeight() - 1, paint); } if (currentColor != 0 && colorDrawable != null) { int x; int y = (getMeasuredHeight() - colorDrawable.getMinimumHeight()) / 2; if (!LocaleController.isRTL) { x = getMeasuredWidth() - colorDrawable.getIntrinsicWidth() - AndroidUtilities.dp(14.5f); } else { x = AndroidUtilities.dp(14.5f); } colorDrawable.setBounds(x, y, x + colorDrawable.getIntrinsicWidth(), y + colorDrawable.getIntrinsicHeight()); colorDrawable.draw(canvas); } } public static void setDividerColor(Context context, boolean night) { if (paint != null) { paint.setColor(ContextCompat.getColor(context, night ? R.color.divider_dark : R.color.divider_light)); } } }