11

I've got some problems with the onClicklistener in the fragment. If I click on the button nothing happens at all. I get neither a message from the onClicklistener in Logcat nor does a Toast appear on the screen, but I can't find the error in the code. Any ideas?

I'd appreciate any help! Thanks a lot! And sorry for my bad english

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import android.util.Log;

public class InputFragment extends Fragment
{   
EditText input_text;
String text;
Button translate_button;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{ 
    View InputFragmentView = inflater.inflate(R.layout.input_fgmt, container, false);

    input_text = (EditText) InputFragmentView.findViewById(R.id.input_field);

    translate_button = (Button) InputFragmentView.findViewById(R.id.translate);


    translate_button.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            Log.d("Test", "onClickListener ist gestartet");
            Toast.makeText(getActivity().getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
            saveInString();

        }
    });

    return inflater.inflate(R.layout.input_fgmt, container, false);
}

public void saveInString()
{
    if(text.equals(null))
    {
        Toast.makeText(getActivity().getApplicationContext(), "Das Feld ist leer!", Toast.LENGTH_SHORT).show();
    }
    else
    {
        Toast.makeText(getActivity().getApplicationContext(), "Speichern...", Toast.LENGTH_LONG).show();
        text = input_text.getText().toString();
        Toast.makeText(getActivity().getApplicationContext(), "Fertig", Toast.LENGTH_SHORT).show();
    }

}
}    
reedts
  • 143
  • 1
  • 1
  • 8
  • 3
    Try returning `InputFragmentView` instead of `inflater.inflate(R.layout.input_fgmt, container, false);` – Kevin Sep 19 '13 at 12:07
  • 1
    Java variables name convention starts in lower case, use **inputFragmentView** instead of 'InputFragmentView'. – Rudolf Real Nov 21 '14 at 00:50

5 Answers5

34

I think problem is here in your code

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{ 
.....
....
 //problem is here below line
 return inflater.inflate(R.layout.input_fgmt, container, false);
}

return your already inflated view

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    { 
        View inputFragmentView = inflater.inflate(R.layout.input_fgmt, container, false);
    .....
    ....

     return inputFragmentView;
    }
Gratien Asimbahwe
  • 1,606
  • 4
  • 19
  • 30
Mukesh Kumar Singh
  • 4,512
  • 2
  • 22
  • 30
  • 1
    Thank you, that was the problem! – reedts Sep 19 '13 at 12:21
  • 1
    I have the same issue as @reedts, but it had nothing to do with what I returned (I was returning the already inflated view). But otherwise the same - the click event is never fired it seems. – Ted Jul 20 '15 at 17:07
3

change

 return inflater.inflate(R.layout.input_fgmt, container, false);

to

 return InputFragmentView ;

Also change with this:

translate_button.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View view)
    {
        Log.d("Test", "onClickListener ist gestartet");
        Toast.makeText(getActivity().getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
        saveInString();

    }
});

and import as a import android.view.View;

Piyush
  • 18,895
  • 5
  • 32
  • 63
2

Do this changes to your code

  1. on class definition:

     public class InputFragment extends Fragment implements View.OnClickListener
    
  2. on calling setOnClickListener:

        translate_button.setOnClickListener(new View.OnClickListener()
        {
              @Override
              public void onClick(View InputFragmentView)
              {
                      Log.d("Test", "onClickListener ist gestartet");
                      Toast.makeText(getActivity().getApplicationContext(), "Test", Toast.LENGTH_LONG).show();                                    
                      saveInString();
              }
         });
    
  • 6
    You don't need to implemenet OnClickListener to the class if you are setting it manually on the button directly. – Carnal Sep 19 '13 at 12:14
2

The best way to get all views created you should to redefinition the methode :

@Override
public void onActivityCreated(Bundle saved) {
    super.onActivityCreated(saved);
     input_text = (EditText) InputFragmentView.findViewById(R.id.input_field);

translate_button = (Button) InputFragmentView.findViewById(R.id.translate);


translate_button.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View view)
    {
        Log.d("Test", "onClickListener ist gestartet");
        Toast.makeText(getActivity().getApplicationContext(), "Test",        Toast.LENGTH_LONG).show();
        saveInString();

    }
});

because this methode called after all view have been created. you should read the life cycle of Fragment.

Sayros
  • 95
  • 3
  • 14
0

setOnItemClickListener works with me with fragment instead of setOnClickListener

Abdelah
  • 9
  • 1