1 event for multiple buttons

1

I have an event listener that listens only to 1 button (which is Button hello = view.findViewById(R.id.hello)).

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_general, container, false);
    Button hello = view.findViewById(R.id.hello);

    // Text to speech
    tts = new Tts();
    tts.init(getActivity(), "Reza");

    hello.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String id = getResources().getResourceEntryName(view.getId());
            tts.speak(id);
        }
    });
    return view;
}

Now I have multiple buttons and when you click on them, the same thing should happen. I do not want to copy and paste my code for all the other buttons.

This is what I have tried:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_general, container, false);
    // Get the button
    Button btn = view.findViewById(R.id.hello);
    switch (getResources().getResourceEntryName(view.getId())) {
        case "hello":
            btn = view.findViewById(R.id.hello);
            break;
        case "observed":
            btn = view.findViewById(R.id.observed);
            break;
    }

    // Text to speech
    tts = new Tts();
    tts.init(getActivity(), "Reza");

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String id = getResources().getResourceEntryName(view.getId());
            tts.speak(id);
        }
    });
    return view;
}

But I get the error:

android.content.res.Resources$NotFoundException: Unable to find resource ID #0xffffffff

There must be something wrong with my switch statement.

java
android
android-studio
asked on Stack Overflow Sep 14, 2020 by Reza

4 Answers

2

You can use the property onClick in the xml of all buttons like so

<Button
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="btnHandler"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/fragment_container"
    android:text="btn1"/>

<Button
    android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="btnHandler"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/btn1"
    android:text="btn2"/>

<Button
    android:id="@+id/btn3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="btnHandler"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/btn2"
    android:text="btn3"/>

And inside the btnHandler do the following:

In Kotlin:

fun btnHandler(view: View) {
    when(view.id) {
        R.id.btn1 -> Log.d(TAG, "btn1")
        R.id.btn2 -> Log.d(TAG, "btn2")
        R.id.btn3 -> Log.d(TAG, "btn3")
    }
}

In java use switch-case (if there is an error you fix it I am not using java at the current time and might make mistake)

public void btnHandler(view: View) {
    switch(view.id) {
        case R.id.btn1: 
            Log.d(TAG, "btn1");
            break;
        case R.id.btn2: 
            Log.d(TAG, "btn2");
            break;
        case R.id.btn3: 
            Log.d(TAG, "btn3");
            break;
    }
}
answered on Stack Overflow Sep 14, 2020 by Mohammad Elsayed
0

You are looking for id that may not exists

....
// Get the button
Button btn = view.findViewById(R.id.hello); /// here if the view is observed, does it contain R.id.hello?
switch (getResources().getResourceEntryName(view.getId())) {
answered on Stack Overflow Sep 14, 2020 by k1r0
0

Your issue lies within this area:

  switch (getResources().getResourceEntryName(view.getId())) {
            case "hello":
                btn = view.findViewById(R.id.hello);
                break;
            case "observed":
                btn = view.findViewById(R.id.observed);
                break;
        }

You are identifing the button and then using a switch statement on the fragment_general view thereby trying to assign the fragment ID to a button. So it will not find the ID. I would suggest implementing like so.

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_general, container, false);
            // Get the button
            Button btn = view.findViewById(R.id.hello);
            Button observeBtn = view.findViewById (R.id.observed);
    
        // Text to speech
        tts = new Tts();
        tts.init(getActivity(), "Reza");
    
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              makeTTS("Hello")
            }
        });
        observeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              makeTTS("Observe")
            }
        });

        return view;
    }

  public void makeTTS(String aText){
  
                tts.speak(aText);
  }
answered on Stack Overflow Sep 14, 2020 by James Palfrey
0

this will be easy to implement and simplify your code, check:

  1. Implement View.OnClickListener to class

    public class Test extends Activity implements View.OnClickListener

  2. On CreateView method set listeners for every button:

    btnHello = view.findViewById(R.id.hello);
    btnObserved = view.findViewById(R.id.observed);
    btnHello.setOnClickListener(this);
    btnObserved.setOnClickListener(this);
    
  3. Then, you have to set onClick method overrided, like this:

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.hello:
            case R.id.observed:
                tts.speak(view.getId());
            break;
        }
    }
    
answered on Stack Overflow Sep 14, 2020 by isthemartin

User contributions licensed under CC BY-SA 3.0