Igal
Igal

Reputation: 6093

Toast inside onClick method in Fragment

I have a following Fragment:

public class FragmentSocial extends Fragment implements ActionBar.TabListener, OnClickListener 
{

private Fragment mFragment;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().setContentView(R.layout.fragment_social);
}

public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // relevant code...
}

public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // relevant code...
}

public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

public void onClick(View v) {
    switch(v.getId())
    {
    case R.id.imgBtnFB:
        Toast.makeText(this, "FB pressed", Toast.LENGTH_LONG).show();
        break;
    case R.id.imgBtnTwitter:
        Toast.makeText(this, "Twitter pressed", Toast.LENGTH_LONG).show();
        break;
    }

}

I have a few image buttons in my fragment_social layout. For now I just want to make a Toast message to see what button was pressed. However, if I use this as context, I'm getting this error message: The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (FragmentSocial, String, int) I tried to change this to FragmentSocial.this, to FragmentSocial.this.getActivity(), tried to create private Context mContext and to instantiate it to mContext = (I tried various options here) inside the onCreate method - but nothing worked. I either didn't have an error message, but also didn't see the Toast, or got other errors.

So how can I create a Toast here?

Thank you!

Upvotes: 4

Views: 22451

Answers (5)

Ram Kishore Prajapati
Ram Kishore Prajapati

Reputation: 51

If you are using fragment then use this code

Toast.makeText(getActivity(), "You have insufficient points", Toast.LENGTH_LONG ).show();

Upvotes: 4

Ayush Goyal
Ayush Goyal

Reputation: 211

Either of the following lines work.

Toast.makeText(getActivity(), "hello",Toast.LENGTH_LONG).show();
Toast.makeText(getActivity().getApplicationContext(),"Hello",Toast.LENGTH_LONG).show();

Upvotes: 2

Adam
Adam

Reputation: 2570

Extending the answer by Marcin S., you could also setup the constructor inside the Fragment and pass in a context variable. You can then use this context anytime you wish to reference the activity. This method is also slightly quicker then getActivity().getBaseContext()

Add this to your FragmentSocial class:

Context context;
public FragmentSocial(Context context) {
    this.context = context;
}

When you ever say FragmentSocial fragmentSocial = new FragmentSocial(); in your main activity class, add the paramter this into the () to make it look like FragmentSocial fragmentSocial = new FragmentSocial(this);

Upvotes: 0

Intathep
Intathep

Reputation: 3388

This is way i do in fragment

//edit

 YourActivity activity;


@Override
public void onAttach(Activity activity) {
   this.activity = (YourActivity) activity;
}

@Override
public void onClick(View v) {
    Toast.makeText(this.activity, "message", Toast.LENGTH_LONG).show();
}

Upvotes: 0

Marcin S.
Marcin S.

Reputation: 11191

use:

getActivity().getBaseContext();

Upvotes: 13

Related Questions