Remove findViewById()
- Add this after buildTypes in your build.gradle(Module:app)
viewBinding{
enabled = true
}
- Add id to your button
<Button
android:id="@+id/button"
android:layout_centerInParent="true"
android:text="Show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
- Type syntax below, in your MainActivity.class
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Correct", Toast.LENGTH_SHORT).show();
}
});
}