Question

I'm trying to access children of a LinearLayout in an onCreateView with getChildAt

currPaint = (ImageButton)paintLayout.getChildAt(0);

but i can't figure out why currPaint is null.

What can i do to reach the child?

Even if i try to call getChildCount the result is null. What am i doing wrong?

@Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {

                View rootView = inflater.inflate(R.layout.fragment_main, container,
                        false); 

                drawView = (DrawingView)rootView.findViewById(R.id.drawing);
                LinearLayout paintLayout = (LinearLayout)rootView.findViewById(R.id.paint_colors);//get the palette and first color button

                currPaint = (ImageButton)paintLayout.getChildAt(0); 
                currPaint.setImageDrawable(rootView.getResources().getDrawable(R.drawable.paint_pressed));

                return rootView;
            }

This is the xml layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFCCCCCC"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <!-- Top Buttons -->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/new_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:contentDescription="@string/start_new"
            android:src="@drawable/new_pic" />

        <ImageButton
            android:id="@+id/draw_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:contentDescription="@string/brush"
            android:src="@drawable/brush" />

        <ImageButton
            android:id="@+id/erase_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:contentDescription="@string/erase"
            android:src="@drawable/eraser" />

        <ImageButton
            android:id="@+id/save_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:contentDescription="@string/save"
            android:src="@drawable/save" />
    </LinearLayout>

    <!-- Custom View -->

    <com.example.tastycorpse.DrawingView
        android:id="@+id/drawing"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="3dp"
        android:layout_weight="1"
        android:background="#FFFFFFFF" />

    <!-- Color Palette -->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical" >

        <!-- Top Row -->

        <LinearLayout
            android:id="@+id/paint_colors"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF660000"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF660000" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FFFF0000"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FFFF0000" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FFFF6600"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FFFF6600" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FFFFCC00"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FFFFCC00" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF009900"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF009900" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF009999"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF009999" />
        </LinearLayout>

        <!-- Bottom Row -->

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF0000FF"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF0000FF" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF990099"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF990099" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FFFF6666"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FFFF6666" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FFFFFFFF"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FFFFFFFF" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF787878"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF787878" />

            <ImageButton
                android:layout_width="@dimen/large_brush"
                android:layout_height="@dimen/large_brush"
                android:layout_margin="2dp"
                android:background="#FF000000"
                android:contentDescription="@string/paint"
                android:onClick="paintClicked"
                android:src="@drawable/paint"
                android:tag="#FF000000" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
Was it helpful?

Solution

Why not give the button an ID and then get a reference to it with:

currPaint = (ImageButton)paintLayout.findViewById(R.id.brush_button);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top