-3

I am using volley for network call


Toast in volley request is not showing. I don't get how to get context in alert dialog from fragment . So please give solution of how i can print toast in volley response.

public class TiffinMenuFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final AlertDialog.Builder alertdialog = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = (LayoutInflater) getActivity().getBaseContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.fragment_tiffinlayout, null);
    alertdialog.setView(v);
    alertdialog.setCancelable(false);


    final AlertDialog dialog = alertdialog.create();

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            String type=selectmenu.getSelectedItem().toString();
            Log.v("java1",type);


            String url_menu=UrlString.url_string;
            url_menu = url_menu.replace(" ", "%20");
            Log.v("url",url_menu);
            final StringRequest stringRequest=new StringRequest(Request.Method.GET, url_menu, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.v("Response",response);
                    JSONObject obj= null;
                    try {
                        obj = new JSONObject(response);
                        String objResponse=obj.getString("success");
                        Log.v("Response1",objResponse);
                        Toast.makeText(view.getContext(),"Task is Completed",Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                Log.v("type",error.getMessage());
                }
            });
            MySingleton.getInstance(getActivity().getApplicationContext()).addToRequestque(stringRequest);
            dialog.dismiss();

        }
    });
    cancle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();

        }
    });




    return dialog;

}
}
Bhoomi
  • 89
  • 9

6 Answers6

0

just do

Toast.makeText(getActivity(),"Text!",Toast.LENGTH_SHORT).show();
Shivanshu Verma
  • 609
  • 5
  • 19
  • Although this code might solve the problem, one should always add a explanation to it explaining why/how the problem is solved. – BDL Apr 05 '17 at 11:12
0

You context your using in Toast that is not correct use below one

 Toast.makeText(getActivity(),"Task is Completed",Toast.LENGTH_LONG).show();
Nikhil Sharma
  • 593
  • 7
  • 23
0
                Toast.makeText(dialog.getContext(),"Task is Completed",Toast.LENGTH_LONG).show();

Use this.

Fathima km
  • 2,539
  • 3
  • 18
  • 26
0

use:

Toast.makeText(getActivity().getApplicationContext(),"Task is
Completed",Toast.LENGTH_LONG).show();
Michał B
  • 62
  • 1
  • 6
0
  Toast.makeText(dialog.getActivity(),"Task is Completed",Toast.LENGTH_LONG).show();
Andie
  • 96
  • 3
  • 21
  • While this code may answer the question, providing additional [context](https://meta.stackexchange.com/q/114762) regarding _how_ and/or _why_ it solves the problem would improve the answer's long-term value. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit](http://stackoverflow.com/posts/43230652/edit) your answer to add an explanation, and give an indication of what limitations and assumptions apply. It also doesn't hurt to mention why this answer is more appropriate than others. – Dev-iL Apr 05 '17 at 14:15
-1

You cant touch the UI from background thread

To show toast use this code

activity.runOnUiThread(new Runnable() {
    public void run() {
        Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();
    }
});

or the request getting failed and not calling that onResponse function.

Ajay Venugopal
  • 1,544
  • 1
  • 17
  • 30
  • this thing is a background process which is also thread final StringRequest stringRequest=new StringRequest(Request.Method.GET, url_menu, new Response.Listener() { @Override public void onResponse(String response) {} – Ajay Venugopal Apr 05 '17 at 10:37
  • background operations are normally called thread and it cant touch the UI . For that you need to use the above code . Please try to learn some base classes that would be helpful for you. – Ajay Venugopal Apr 05 '17 at 11:45