Search code examples
androidandroid-viewandroid-custom-view

Android View methods call order


I'm creating my own custom NumberPicker in android and here's the code:

package fs.mohammad.fun;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

public class NumberPicker extends android.widget.NumberPicker {
    private final static String LOG_TAG = "M3@NumberPicker";

    public NumberPicker(Context context) {
        super(context);
        Log.d(LOG_TAG, "NumberPicker(context)");
    }

    public NumberPicker(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        Log.d(LOG_TAG, "NumberPicker(context, attributeSet)");
    }

    @Override
    public void addView(View child) {
        super.addView(child);
        Log.d(LOG_TAG, "addView(child)");
    }

    @Override
    public void addView(View child, int index) {
        super.addView(child, index);
        Log.d(LOG_TAG, "addView(child, index)");
    }

    @Override
    public void addView(View child, int width, int height) {
        super.addView(child, width, height);
        Log.d(LOG_TAG, "addView(child), width, height");
    }

    @Override
    public void addView(View child, ViewGroup.LayoutParams params) {
        super.addView(child, params);
        Log.d(LOG_TAG, "addView(child, params)");
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        Log.d(LOG_TAG, "addView(child, index, params)");
    }
}

and here's my layout code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/root_view"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="fs.mohammad.fun.MainActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <fs.mohammad.fun.NumberPicker
        android:id="@+id/number_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:descendantFocusability="blocksDescendants"
        />

</RelativeLayout>

I expect that the constructor be called first but here's my Log output:

08-13 08:33:08.384 17759-17759/fs.mohammad.fun D/M3@NumberPicker: addView(child, index, params)
08-13 08:33:08.384 17759-17759/fs.mohammad.fun D/M3@NumberPicker: addView(child, params)
08-13 08:33:08.384 17759-17759/fs.mohammad.fun D/M3@NumberPicker: NumberPicker(context, attributeSet)

My question is why the addView is called before constructor?


Solution

  • NumberPicker inflates it's children in constructor. So NumberPicker#addView called by LayoutInflator to add children to NumberPicker.

    Here is source code. https://github.com/android/platform_frameworks_base/blob/4535e11fb7010f2b104d3f8b3954407b9f330e0f/core/java/android/widget/NumberPicker.java#L678