Android Development

Simple Toolbar Example

Straightforward toolbar example

Guowei Lv

1 minute read

Use NoActionBar theme <resources> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources> Create toolbar layout <?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="@color/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> </androidx.appcompat.widget.Toolbar> Setting the layout_height to wrap_content will make it the correct default height. Since our app has a light theme, the texts color in toolbar will be black. To change it to white, we need to set the theme of the toolbar to ThemeOverlay.

Android Custom View 102 (Part 9)

A RatingBar Example

Guowei Lv

1 minute read

There is nothing new here, just another exercise. Two things to be careful with: Try to call validate() as fewer times as possible, it is expensive. If you want to handle the touch events in this manner, remember to return true from onTouchEvent(). Otherwise, only the first event will be triggered(most likely DOWN in this case) and not the succeeding ones (like the MOVES). Source Code

Guowei Lv

1 minute read

Let’s take a look at what we are going to build. The secret is to clip canvas before drawing the text! private fun drawText(canvas: Canvas, paint: Paint, start: Int, end: Int) { canvas.save() rect.set(start, 0, end, height) canvas.clipRect(rect) val textString = text.toString() paint.getTextBounds(textString, 0, textString.length, bounds) val x = width / 2 - bounds.width() / 2 val fontMetrics = changePaint.fontMetrics val dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.