0

I have a simple layout like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.empresa.prueba.MainActivity">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:text="Button"
        app:layout_anchor="@+id/include"
        app:layout_anchorGravity="center_vertical|center_horizontal"/>
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        app:srcCompat="@drawable/image2"/>

</LinearLayout>

I want to change the label of the button while it is being created (I'm using onCreateView methods in activity instead of a custom factory). The problem is that I never get the button widget control when android calls onCreateView for each view in the layout.

I have added a log to the onCreateView Activity overrides to see what views are being created this way:

@Override
    public View onCreateView(View v, String s, Context c, AttributeSet as)
    {
        Log.i("Inflating ---> ",s);
        return super.onCreateView(v,s,c,as);
    }

    @Override
    public View onCreateView(String s , Context c, AttributeSet as)
    {
        Log.i("Inflating ---> ",s);
        return super.onCreateView(s,c,as);
    }

But the output I get is:

I/Inflating --->: LinearLayout
I/Inflating --->: LinearLayout
I/Inflating --->: ViewStub
I/Inflating --->: ViewStub
I/Inflating --->: FrameLayout
I/Inflating --->: FrameLayout
I/Inflating --->: android.support.v7.widget.FitWindowsLinearLayout
I/Inflating --->: android.support.v7.widget.FitWindowsLinearLayout
I/Inflating --->: android.support.v7.widget.ViewStubCompat
I/Inflating --->: android.support.v7.widget.ViewStubCompat
I/Inflating --->: android.support.v7.widget.ContentFrameLayout
I/Inflating --->: android.support.v7.widget.ContentFrameLayout
I/Inflating --->: LinearLayout
I/Inflating --->: LinearLayout

As you can see neither the button nor the Imageview are passed to the onCreateView methods so, it is not possible for me to modify anything on this views. It seems this methods only receive containers?

If anyone know why this happens I would be really appreciate it.

Cheers.

Notbad
  • 5,936
  • 12
  • 54
  • 100

1 Answers1

-2

onCreateView life cycle method is for setting layout to fragment. In this life cycle method we can not do any updating, because the layout is not yet created.

If you want to update any view's label we can do in onViewCreated life cycle method.

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
prakash
  • 162
  • 3
  • 1
    `Activity#onCreateView()` that implements `android.view.LayoutInflater.Factory2#onCreateView()` is not the same as `Fragment#onCreateView()` – laalto Aug 27 '17 at 10:34