OnClickListener not firing in fragment

onclicklistener in fragment
onclicklistener in fragment kotlin
setonclicklistener in fragment
fragment button onclick not working
setonclicklistener not working in fragment
onclicklistener java
onclick in android
findviewbyid in fragment

I've already searched through the forum for answers, but I couldn't find any question that completely matches my situation.

I'm having a problem firing an onclicklistener. The same piece of code works in other parts of application, which confuses me.

This is my code:

private static  final int RESULT_LOAD_IMAGES = 1;
TextView filepath_name;
EditText varighed, navn;
Spinner genre, kategori;
Button uploadImg, opret_knap;

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);

    uploadImg = (Button) view.findViewById(R.id.billede_button);
    opret_knap = (Button) view.findViewById(R.id.addOpskrift);
    varighed = (EditText) view.findViewById(R.id.opskrift_varighed);
    navn = (EditText) view.findViewById(R.id.opskrift_navn);
    genre = (Spinner) view.findViewById(R.id.opskrift_genre);
    kategori = (Spinner) view.findViewById(R.id.opskrift_kategori);

    filepath_name = (TextView) view.findViewById(R.id.billede_path);
    uploadImg.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
             startActivityForResult(galleryIntent, RESULT_LOAD_IMAGES);
         }

    });

    opret_knap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Tilføjopskrift();
        }
    });

    return view;
}

And the XML

<Button
        android:id="@+id/billede_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Tilføj billede"
        />
<TextView android:id="@+id/billede_path"
          android:layout_width="wrap_content"
          android:layout_height="32dp"
          android:layout_gravity="center" />

<Button android:id="@+id/addOpskrift"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="30dp"
        android:text="Opret"/>

In your XML you have given id opret_knap to you Button

<Button
    android:id="@+id/opret_knap"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="30dp"
    android:text="Opret"/>

But you are accessing it with addOpskrift

Change

opret_knap = (Button) view.findViewById(R.id.addOpskrift);

to

 opret_knap = (Button) view.findViewById(R.id.opret_knap);

How to Handle onClick in Fragments, FindViewById() is not working in fragments and where should I write the method? in fragment or in my activity? share. i'm not getting why you trying to create new class under fragment, there is specific methods for fragment – Saveen Jul 17 '16 at 6:33 @Saveen It can't be public within the Fragment. Thanks for pointing that out – cricket_007 Jul 17 '16 at 6:34

The problem seems to be that you are not specifying the layout your fragment should access.

Instead of:

super.onCreateView()

Use

View view = inflater.inflate(R.layout.xyz, container, false);

Hope it helps.

onclick event in fragment activity not working, The problem was in my activity_main.XML, where I had defined <Fragment> as a placeholder for all fragments and had set one fragment as  setOnClickListener(new OnClickListener() not working in Fragment Tag: android , android-fragments I am working on fragements of NAvigation Drawer .Andi copy the code of Activity to fragments to execute in fragment .

Your problem is that your not instanciate correctely the new view. You need use:

View view = inflater.inflate(R.layout.your_file_name, container, false);

instead of:

View view = super.onCreateView(inflater, container, savedInstanceState);.

Just copy and paste.

private static  final int RESULT_LOAD_IMAGES = 1;
TextView filepath_name;
EditText varighed, navn;
Spinner genre, kategori;
Button uploadImg, opret_knap;


@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
     View view = inflater.inflate(R.layout.your_file_name, container, false);
    uploadImg = (Button) view.findViewById(R.id.billede_button);
    opret_knap = (Button) view.findViewById(R.id.addOpskrift);
    varighed = (EditText) view.findViewById(R.id.opskrift_varighed);
    navn = (EditText) view.findViewById(R.id.opskrift_navn);
    genre = (Spinner) view.findViewById(R.id.opskrift_genre);
    kategori = (Spinner) view.findViewById(R.id.opskrift_kategori);

    filepath_name = (TextView) view.findViewById(R.id.billede_path);
    uploadImg.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
             startActivityForResult(galleryIntent, RESULT_LOAD_IMAGES);
         }

    });

    opret_knap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Tilføjopskrift();
        }
    });

    return view;
}

How to implement onClickListener method in Fragments?, In Genres Fragment, I have four ImageButtons and I want to add some action to them, like OnClickListener{ public GenresFragment() { // Required empty public constructor } @Override WhatsApp not working and not de… I'm new to Android development and I'm developing my first Android app about music that contains two fragments: Home Fragment and Genres Fragment. This app is a school project and it's kinda urgent.

onClick on Fragment doesn't work how to fix it?, Button; public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  Then you set button.setOnClickListener(new OnClickListener). It's better to set onClicks this way than through xml. Activities should only be used when communicating between fragments and not for fragment specific operations, because fragments are meant to be reusable and independent. I'm on my phone, so there's probably a few errors. Sorry.

Android Fragment button's onclick not working first time, For a particular fragment in my Android app, I am having the onClick as follows: btn_fav = (Button) view.findViewById(R.id.btn_fav); this.setFavText(btn_fav. In Genres Fragment, I have four ImageButtons and I want to add some action to them, like when clicking a button, it goes to another fragment So, in the Java file of that fragment, I already have the code for OnClickListener but I don't know what to put in the case condition of each button.

setOnClickListener not working in fragment Android, The fragment setOnClickListener not working in fragment Android OnClickListener() { @Override public void onClick(View v) { Toast. However, if you have a Button in a fragment, using the android:onClick attribute does not cause the event handler to fire in the Fragment class; instead it will fire in the Activity class.

Comments
  • What is this method? "Tilføjopskrift();"? I wouldnt recommend you to use special characters just like "ø"
  • It is a method that adds something into a arraylist, I've changed the name but no changes
  • I think you didn't post your whole XML, cause I don't see the definition of billede_button
  • Changing the name of the method will not debug any logic, I think... That recommendation targets something different: international readability
  • I think you must change your method name.
  • Thanks. I've changed it couple of times while trying to fix the issue. They match now, but yet it doesn't work.