I just got to know about ViewBinding and I am not sure if I should or shoold not use it. Bascially I have been using findViewByID and I think that it is quite convenient to use. I read about ViewBinding but honestly did not understand how to use it. For example I have the following code:
public class MainActivity extends AppCompatActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_mainActivity);
setSupportActionBar(myToolbar);
Button orderButton = findViewById(R.id.Bestellen_Button);
orderButton.setOnClickListener(this);
getSupportActionBar().setDisplayShowTitleEnabled(false);
Button statisticsButton = findViewById(R.id.Statistik_Button);
statisticsButton.setOnClickListener(this);
}
How can I now use the View Binding. I get errors when I do it as stated on the Android developer page (https://developer.android.com/topic/libraries/view-binding?utm_medium=studio-assistant-stable&utm_source=android-studio-3-6):
private ResultProfileBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ResultProfileBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
}
How can I reference for instance the statistics Button:
Button statisticsButton = binding.findViewById(R.id.Statistik_Button);
statisticsButton.setOnClickListener(this);
I'd appreciate every comment and would be thankful for your help.
Update: I still have big problems with the ViewBinding. I replaced 'findViewById' approach in the upper class by the View binding method and I have the following code:
public class MainActivity extends AppCompatActivity implements OnClickListener {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_mainActivity);
setSupportActionBar(myToolbar);
Button orderButton = findViewById(R.id.Bestellen_Button);
orderButton.setOnClickListener(this);
getSupportActionBar().setDisplayShowTitleEnabled(false);
Button statisticsButton = findViewById(R.id.Statistik_Button);
statisticsButton.setOnClickListener(this);
*/
Toolbar myToolbar = binding.toolbarMainActivity;
setSupportActionBar(myToolbar);
binding.BestellenButton.setOnClickListener(this);
binding.StatistikButton.setOnClickListener(this);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
@Override
public void onClick (View view){
if(view.getId() == R.id.Bestellen_Button) {
Intent intent_Selection = new Intent(this, Selection_Menu_Activity.class);
startActivity(intent_Selection);
}
if(view.getId() == R.id.Statistik_Button) {
Intent intent_Statistik = new Intent(this, CocktailY_Activity.class);
startActivity(intent_Statistik);
}
}
}
However when I run the app I get the following error message:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.td.barapp/com.example.td.barapp.MainActivity}: java.lang.NullPointerException: Attempt to read from field 'android.support.v7.widget.Toolbar com.example.td.barapp.databinding.ActivityMainBinding.toolbarMainActivity' on a null object reference
And the line 'Toolbar myToolbar = binding.toolbarMainActivity;' is referenced. Does someone have an opinion why this error occurs? I'd appreciate every help.
Here is the corresponding XML layout file of the class:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity"
tools:ignore="ExtraText">
'<!--Learning: The following lines define a toolbar -->'
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_mainActivity"
android:layout_width="match_parent"
android:layout_height="53dp"
android:background="#435cb53f"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="@android:color/holo_green_light" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_mainActivity_2"
android:layout_width="match_parent"
android:layout_height="53dp"
android:background="#435cb53f"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="@android:color/holo_green_light" />
<ImageView
android:id="@+id/imageView"
android:layout_width="427dp"
android:layout_height="250dp"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent=".35"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.123"
app:layout_constraintWidth_percent="1"
app:srcCompat="@mipmap/vienna_test" />
<Button
android:id="@+id/Statistik_Button"
android:layout_width="256dp"
android:layout_height="95dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#435cb53f"
android:text="@string/Statistik_Button"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.886" />
<Button
android:id="@+id/Bestellen_Button"
android:layout_width="255dp"
android:layout_height="96dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#435cb53f"
android:text="@string/Bestellen_Button"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.614" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="153dp" />
<TextView
android:id="@+id/textView_ToolBar_MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="App"
android:textColor="@android:color/white"
android:textSize="24sp"
android:visibility="visible"
app:fontFamily="@font/roboto_bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.536"
app:layout_constraintStart_toStartOf="@+id/toolbar_mainActivity"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
You should not do it like this.
Button statisticsButton = binding.findViewById(R.id.Statistik_Button);
statisticsButton.setOnClickListener(this);
The whole concept of binding is introduced to replace the findViewById.
So In order to refer to Statistik_Button just use binding.
it will show the all elements that have the id's in the layout then from that you can choose the views based on that and perform necessity tasks.
In this case, you can refer to button with id Statistik_Button like this
binding.StatistikButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
I hope this helped...