Aaron Waller
Aaron Waller

Reputation: 71

onClickListener() not working on FloatingActionButton and Toolbar Icon

the onClickListener of my FloatingActionButton and Toolbar Icon are not working. Here is the code:

    floatingActionButton = findViewById(R.id.pausebutton);
    floatingActionButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            EventHandlerClass.releaseMediaPlayer();
        }
    });

    search_ico=findViewById(R.id.search_ico);
    search_ico.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this,searchActivity.class));
        }
    });

It is weird because the "new OnClickListener()" is greyed out like this: Greyed out

My clicks are going through the FloatingActionButton, here is the code:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center|bottom"
    android:layout_marginBottom="60dp"
    android:src="@drawable/paused"
    app:fabSize="normal"
    app:backgroundTint="#38464E"
    app:elevation="20dp"
    android:id="@+id/pausebutton"/>

I have absolutely no Idea what is wrong, looks right to me. Does anyone have some Ideas what I could try?

Upvotes: 0

Views: 77

Answers (2)

Aaron Waller
Aaron Waller

Reputation: 71

I fixed it my own, the problem was that I had this line after the onClickListeners:

setContentView(R.layout.activity_main);

But this line hast to be right at the top of the OnCreate

Upvotes: 1

GHH
GHH

Reputation: 2019

You can click on it and press option + Enter. The IDE will tell you it can be replace with lambda enter image description here

it can make the code more clear

enter image description here

Upvotes: 0

Related Questions