findViewById() not finding custom view but it exists in layout

Somehow my acitivity_main.xml has the view BottomSelectElement (custom view) but I cannot find it in the activity, but I can find anything else.

activity_main.xml (removed unnecessary parts)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="it.bachmann.studytimer.ui.MainActivity">

    <!-- THIS IS THE VIEW I CANNOT FIND IN THE MainActivity.java (BottomSelectElement) -->
    <it.bachmann.studytimer.ui.elements.BottomSelectElement
        android:id="@+id/customBottomSelect"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_gravity="bottom|center" />

    <com.github.clans.fab.FloatingActionMenu

</android.support.design.widget.CoordinatorLayout>

my custom view class which is shown perfectly in the activity_main.xml

public class BottomSelectElement extends ConstraintLayout {

    private Spinner spinner;

    public BottomSelectElement(Context context) {
        super(context);

            init();
        }

        public BottomSelectElement(Context context, AttributeSet attributeSet) {
            super(context);
            init();
        }

        private void init() {
            inflate(getContext(), R.layout.bottom_select_element, this);
            spinner = findViewById(R.id.spinner);
            List<String> categories = Arrays.asList("foo", "bar", "baz");
            ArrayAdapter adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_text, categories);
            adapter.setDropDownViewResource(R.layout.spinner_text_checked);
            spinner.setAdapter(adapter);
        }

        public Spinner getSpinner() {
            return spinner;
        }
    }

and finally my MainActivity.java which doesn’t find the customBottomSelect, but it finds anything else.

public class MainActivity extends AppCompatActivity {

    private final String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    @Override
    protected void onStart() {
        super.onStart();
        BottomSelectElement bottomSelectElement = findViewById(R.id.customBottomSelect);
        Log.d(TAG, "onStart: bottomSelectElement " + bottomSelectElement); // this returns null, althought it should exist!
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }
}

I restarted Android Studio several times, rebuilt and cleaned it. It just doesn’t seem to find this id.

Answer

Because in the constructor, you had super(context) instead of super(context, attrs).

Makes sense, if you don’t pass in the attributes, such as the id, then the view will have no id and therefore not be findable using that id. 🙂

Answer ref : findViewById() returns null for custom component in layout XML, not for other components

Leave a Reply

Your email address will not be published. Required fields are marked *