1

I am using view binding with Java and have the following activity_main XML file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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"
    tools:context=".MainActivity">

    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

The official example in the docs shows that views can be accessed through a camel-case getter, like this:

binding.getName().setText(viewModel.getName());

According to the example in the docs then, I should be able to access the GridView from the binding like this

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    
    GridView grid = binding.getGridView();
}

However, no getGridView method exists on my binding class. How do I access the view?

Tyler V
  • 9,694
  • 3
  • 26
  • 52
Real Noob
  • 1,369
  • 2
  • 15
  • 29
  • Is your layout nested/included in a parent XML file? And have you enabled view binding in the gradle file? Also, have you tried to build the app? Sometimes the IDE is slow to recognize/find the auto-generated content... – Tyler V Jul 16 '22 at 14:27
  • @TylerV I have added the contents of my `build.gradle` file to the question. I get the error when I run the app by pressing `Shift + F10`. Thanks. :) – Real Noob Jul 16 '22 at 14:33
  • Show full code of access ```getViewMethod``` – Arul Jul 16 '22 at 14:37
  • @Seelenvirtuose I am voting to re-open since this question is about why the binding auto-generated method is missing, not "what does the error mean?" – Tyler V Jul 16 '22 at 14:42
  • @TylerV there is only one file called `activity_main.xml`. Thanks for voting to re-open the question. :) – Real Noob Jul 16 '22 at 14:44
  • @TylerV It goes like this: `MainActivity > onCreate > anonymous ArrayAdapter > getView`. :) – Real Noob Jul 16 '22 at 14:47
  • @TylerV Yes, it is inside an Adapter. Is that a problem? – Real Noob Jul 16 '22 at 14:48
  • What type is `binding`? You did not show the declaration ... – Seelenvirtuose Jul 16 '22 at 14:51
  • @TylerV The Java code is all inside the `MainActivity` class and there is only one layout file `activity_main`. Should I create a separate layout file where I place the layout, update the refrences and everything will work just fine? – Real Noob Jul 16 '22 at 14:53
  • If the code is all inside MainActivity, why are you making an ArrayAdapter? Please post sufficient details in your question (a minimal but complete activity code, adapter, + XML) to help us understand what you are asking - using comments like this is not effective. – Tyler V Jul 16 '22 at 14:54
  • @TylerV I am very new to Android so learning by doing things. I am just having a hard time finding tutorials that will explain everything. I pieced together my own code by reading multiple tutorials so I made many mistakes. :) – Real Noob Jul 16 '22 at 14:56
  • @Seelenvirtuose - it is declared in the question as an `ActivityMainBinding` - have a read about [view binding](https://developer.android.com/topic/libraries/view-binding) – Tyler V Jul 16 '22 at 14:56
  • @RealNoob - that's ok, it's a learning process. You should try to explain better what you are trying to do in the question - show the full code, not just the part you think has the current problem. What are you trying to display? – Tyler V Jul 16 '22 at 14:57
  • @TylerV I know I provided very little details and I am impressed by the fact that you were still able to pinpoint the probable cause of the error. :) – Real Noob Jul 16 '22 at 14:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/246490/discussion-between-tyler-v-and-real-noob). – Tyler V Jul 16 '22 at 14:58
  • @Seelenvirtuose - I clarified the problem with OP in chat and we came up with a more clear version of the original question - the root cause is outdated documentation for Android view binding in terms of what methods will be auto-generated on the binding class. – Tyler V Jul 16 '22 at 18:36

1 Answers1

1

Unfortunately, the view binding documentation code examples with Java are out of date. Instead of generating a camel-case getter function now the generated binding class has public (and final) fields so you can access them directly, like this:

GridView grid = binding.gridView;

In the future, if you type binding. and let the IDE (i.e. Android Studio) show a suggestion of what to type (usually it shows up in a popup/dropdown view) it can help find the right method even when the documentation is wrong.

Tyler V
  • 9,694
  • 3
  • 26
  • 52