2

I have fragment in that fragment i am try to call Toast when user enter wrong Login and Password but Toast is not visible when login button is pressed and log above toast is calling but still toast is not visible

This is my Fragment

Here is my fragment

public class Login_Fragment extends Fragment {

    EditText LoginUname,LoginPass;
    ImageButton SignIn;
    Context context;
    public static final String TAG="Login Fragment";


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.login_fragment,container,false);
        LoginUname= (EditText) view.findViewById(R.id.Login_Box);
        LoginPass= (EditText) view.findViewById(R.id.Pass_Box);
        SignIn= (ImageButton) view.findViewById(R.id.LoginButton);

        SignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String Phone=LoginUname.getText().toString();
                String Password=LoginPass.getText().toString();

                new AsyncTask(){
                    @Override
                    protected void onPostExecute(String result) {
                        super.onPostExecute(result);
                        Log.d("LOGIN FRAGMENT","Result: "+result); //GETTING RESULT FAIL HERE

                        if (result.equals("FAIL")){
                            Log.d("LOGIN FRAGMENT","Result is FAIL"); //THIS LOG SHOWING IN LOGCAT BUT TOAST IS NOT VISIBLE 
                            Toast.makeText(getActivity(), "Invalid Login And Password", Toast.LENGTH_LONG).show();

                        }
                        else if (result.equals("SUCCESS")){
                            Log.d("LOGIN FRAGMENT","Result is Success");

                        }

                    }
                }.execute();
                Log.d("LOGIN FRAGMENT","----LOGIN AND PASSWORD SENT");

            }
        });
        Registration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Registration_Fragment registrationFragment=new Registration_Fragment();
                FragmentManager fragmentManager=getFragmentManager();
                FragmentTransaction transaction=fragmentManager.beginTransaction();
                transaction.replace(R.id.FragmentLoginRegistration,registrationFragment);
                transaction.commit();
            }
        });

        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }
}

Log

08-23 14:04:38.115 29053-29053/com.boysjoys.com.pro_working1 D/LOGIN FRAGMENT: Result: FAIL
08-23 14:04:38.115 29053-29053/com.boysjoys.com.pro_working1 D/LOGIN FRAGMENT: Result is FAIL

i have already tried

Toast.makeText(getActivity(), "Invalid Login And Password", Toast.LENGTH_LONG).show();


Toast.makeText(getActivity().getApplicationContext(), "Invalid Login And Password", Toast.LENGTH_LONG).show();


Toast.makeText(Login_Fragment.this.getActivity(), "Invalid Login And Password", Toast.LENGTH_LONG).show();

and almost all method i can use to show toast visible and at last i tried runOnUiThread but its still not showing.

EDIT 1: Added Log to show that i am getting response from server- "FAIL"

Edit 2: I tried every answer i got but my Toast still not showing up.So is there any chance that my manifest,layout blocks Toast to comes up? if Yes please let me know so i will update my layout,Style,Manifest in my question

androidXP
  • 1,692
  • 3
  • 27
  • 58

5 Answers5

0

getActivity() will return null if run before onAttach(Activity). Instead, you should call getActivity() directly in your onPreExecute and onPostExecute methods or get a reference to it in onAttach:

public void onAttach (Activity attachedActivity) {
    activity = attachedActivity;
}
Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45
0

Add this method at last or in app activity and call where you want its working

public void showToast(@NonNull String msg) {
    Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
}
0

Actually your code is working fine for me. I have hardcoded the login Fail in doInBackground so that it should go inside the onPostExecute() login fail block. Here is the code

 new AsyncTask(){
                @Override
                protected Object doInBackground(Object[] params) {
                    return "FAIL"; // Hardcoded to make login fail
                }

                @Override
            protected void onPostExecute(Object result) {
                super.onPostExecute(result);
                Log.d("LOGIN FRAGMENT","Result: "+result);

                if (result.equals("FAIL")){
                    Log.d("LOGIN FRAGMENT","Result is FAIL");
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.d("LOGIN FRAGMENT","Result is FAIL 2");
                            Toast.makeText(getActivity(), "Invalid Login Or Password toast", Toast.LENGTH_LONG).show();
                        }
                    });

                }
                else if (result.equals("SUCCESS")){
                    Log.d("LOGIN FRAGMENT","Result is Success");

                }

            }
        }.execute();
Abhijeet Mallick
  • 1,740
  • 2
  • 16
  • 21
  • As you can see updated question i also post screenshot and also i post logcat but for me its not working at all i don't know what i am doing wrong – androidXP Aug 23 '16 at 08:51
0

You should call show Toast on UI Thread, So just try like this:

SignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String Phone=LoginUname.getText().toString();
            String Password=LoginPass.getText().toString();

            new AsyncTask(){
                @Override
                protected void onPostExecute(String result) {
                    super.onPostExecute(result);
                    Log.d("LOGIN FRAGMENT","Result: "+result); //GETTING RESULT FAIL HERE

                    if (result.equals("FAIL")){
                        Log.d("LOGIN FRAGMENT","Result is FAIL"); //THIS LOG SHOWING IN LOGCAT BUT TOAST IS NOT VISIBLE 
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getActivity(), "Invalid Login And Password", Toast.LENGTH_LONG).show();
                            }
                        });

                    }
                    else if (result.equals("SUCCESS")){
                        Log.d("LOGIN FRAGMENT","Result is Success");

                    }

                }
            }.execute();
            Log.d("LOGIN FRAGMENT","----LOGIN AND PASSWORD SENT");

        }
    });
Cobain
  • 186
  • 1
  • 8
0

I recommended you take the Asynctask Call like that

public class Login_Fragment extends Fragment {

EditText LoginUname,LoginPass;
ImageButton SignIn;
Context context;
public static final String TAG="Login Fragment";


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.login_fragment,container,false);
    LoginUname= (EditText) view.findViewById(R.id.Login_Box);
    LoginPass= (EditText) view.findViewById(R.id.Pass_Box);
    SignIn= (ImageButton) view.findViewById(R.id.LoginButton);
 SignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String Phone=LoginUname.getText().toString();
            String Password=LoginPass.getText().toString();

              new Asynctask(Phone,Password).execute();

        }
    });

  Registration.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Registration_Fragment registrationFragment=new    Registration_Fragment();
            FragmentManager fragmentManager=getFragmentManager();
            FragmentTransaction    transaction=fragmentManager.beginTransaction();
              transaction.replace(R.id.FragmentLoginRegistration,registrationFragment);
            transaction.commit();
        }
    });

    return view;
 }

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    this.context=context;
}




 private class Asynctask extends AsyncTask<String, Integer, String> {


 String password,phone;

        public Asynctask(String phone, String password) {
            password=password;
            phone=phone;

        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected String doInBackground(String... params)
        {

       // you need to take doInbackground to return result
            return "SUCCESS";
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

          Log.d("LOGIN FRAGMENT","Result: "+result);

            if (result.equals("FAIL")){
                Log.d("LOGIN FRAGMENT","Result is FAIL");
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d("LOGIN FRAGMENT","Result is FAIL 2");
                        Toast.makeText(getActivity(), "Invalid Login Or Password toast", Toast.LENGTH_LONG).show();
                    }
                });

            }
            else if (result.equals("SUCCESS")){
                Log.d("LOGIN FRAGMENT","Result is Success");

            }


        }
    }

 }
Arjun saini
  • 4,223
  • 3
  • 23
  • 51
  • Its separate class but i am calling onPostexecute in fragment because i wanted to fetch result and when success i am getting user details so i need to use it like that – androidXP Aug 23 '16 at 09:55
  • that code is just to test answer below i remove it already sorry for confusion – androidXP Aug 23 '16 at 10:06
  • i m calling like tht but only onPost i m calling in fragment i think it doesnt make any difference even it help me to transfer result in fragment without any additional code – androidXP Aug 23 '16 at 10:13
  • i am executing doinbackground in separate class from that i am getting result in onPostexecute in fragment – androidXP Aug 23 '16 at 10:35