Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onClickListener does not work in fragment

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();
    }

}
}    
like image 665
reedts Avatar asked Sep 19 '13 12:09

reedts


People also ask

How do I call fragment from activity OnClickListener in Android?

The answer to your problem is easy: replace the current Fragment with the new Fragment and push transaction onto the backstack. This preserves back button behaviour... Creating a new Activity really defeats the whole purpose to use fragments anyway... very counter productive.

What is the difference between onClick and OnClickListener?

These two methods have same purpose i.e to user can tap or click to perform an action. But way to implement it is a bit different. android:onClick was added in API level 4 to make it easier, more Javascript-web-like, and drive everything from the XML.

How do I use OnClickListener?

To implement View. OnClickListener in your Activity or Fragment, you have to override onClick method on your class. Firstly, link the button in xml layout to java by calling findViewById() method. R.

What is the only method of the view OnClickListener interface?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked.


2 Answers

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;
    }
like image 104
Mukesh Kumar Singh Avatar answered Oct 19 '22 14:10

Mukesh Kumar Singh


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;

like image 29
Piyush Avatar answered Oct 19 '22 14:10

Piyush