Android Development

Guowei Lv

5 minute read

Let’s build a sliding menu from scratch by hand. First a glimpse of what the final result looks like.

Step 1: Horizontal Scroll View The secret is that this is just a customized HorizontalScrollView: class SlidingMenu @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : HorizontalScrollView(context, attrs, defStyleAttr) { … } The view can be divided into 2 parts: menu view and content view, and they are just put next to each other.

Android Custom View 102 (Part 11)

Touch event dispatching

Guowei Lv

3 minute read

It is interesting how Android view’s touch events are dispatched. Let’s explore it. Firstly, write a custom view and override its onTouchEvent() method. class TouchView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { override fun onTouchEvent(event: MotionEvent): Boolean { Log.d("TAG", "TouchView - onTouchEvent - ${event.action}") return super.onTouchEvent(event) } } Then, in MainActivity, on the view, call setOnTouchListener() and setOnClickListener().

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.