Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – onClickListener() not working on FloatingActionButton and Toolbar Icon

Posted on November 11, 2022 By

Solution 1 :

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

Solution 2 :

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

Problem :

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?

Comments

Comment posted by stackoverflow.com/questions/9577338/…

In this example the user explicitly casts the findViewById as a Button. Does that help you at all?

Comment posted by EveRegalado

I remember that rarely, when copying and pasting between files from other android projects, the project did not compile … the code was correct, there were no code errors … until I saw that I had not placed or deleted the line “setContentView (R.layout.activity_main);” during copy and paste… Good luck in your project

Comment posted by Aaron Waller

This isn’t solving the problem, I still can’t click the FloatingActionButton

READ  [FIXED] android - "Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0." -REACT-NATIVE
Powered by Inline Related Posts
Android Tags:android, floating-action-button, java, onclicklistener, xml

Post navigation

Previous Post: [FIXED] java – MVVM with Room and LiveData – How to get List from LiveData
Next Post: [FIXED] android – Xamarin.Forms.Droid Resource, Ambiguous Terms/ Build Action’s Randomly Changing Missing Resources

Related Posts

[FIXED] performance – Unable to find `systrace.py` on my local android sdk folder Android
[FIXED] java – Handling Press Back Button without reloading fragment in Android Android
[FIXED] java – how to make barcode work with device keys? Android
[FIXED] java – how to make code check if appium server timed out Android
[FIXED] java – How to hide the home fragment when I’m in the 2nd fragment? Android
[FIXED] android – Need to override [onCreateInputConnection] function for TextView Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Which is stronger 19 gauge or 23 gauge?
  • Does BMW still use GM transmissions?
  • Is primary or secondary market research better?
  • What does it mean when ADT says low battery?
  • Can a baby sleep on a regular mattress?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme