0

I have a data submit form in my activity, it is all done. What i need is a toast message when submit button is clicked. For no network, there should appear a toast & for network avaliable, my activity continues the job.

Emm Jay
  • 360
  • 2
  • 20
  • Nice. What have you tried so far? – nKn Mar 16 '14 at 10:06
  • I mean that SO is not a site to request code. You have to try something by your own and if you have issues/problems, then come and ask a question, and of course, include the code you've tried in your question. – nKn Mar 16 '14 at 10:12
  • Check [this](http://stackoverflow.com/questions/16294607/check-network-available-in-android?answertab=active#tab-top) – Kunu Mar 16 '14 at 10:14
  • I gotcha! sorry for not posting code, i want that to be confidential. Please don't get me wrong. – Emm Jay Mar 16 '14 at 10:23

2 Answers2

0

How about check network availability in click event listener.

How to check the network availability?

Community
  • 1
  • 1
  • Sorry! that won't do the job. what i need id a toast that stops the onClick method if network is not avaliable. – Emm Jay Mar 16 '14 at 10:11
0

I used a custom class to tackle this problem:

public class ConnectionDetector {

private Context _context;

    public ConnectionDetector(Context context){
        this._context = context;
    }

    /**
     * Checking for all possible Internet providers
     * **/
    public boolean isConnectingToInternet(){
        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null)
        {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED)
                    {
                        return true;
                    }

        }
        return false;
    }
}

you can implement this in your button on click listener:

b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
                            ConnectionDetector cd=new ConnectionDetector(getApplicationContext());
                            if (!cd.isConnectingToInternet()){
                            Toast.makeText(getBaseContext(),"Not connected",Toast.LENGTH_SHORT).show();
                            }
                            else{
                                //your submit code
                            }

        }
    });

also add this permission to your manifest file:

android.permission.ACCESS_NETWORK_STATE

thanks for the tip @Warlock

gkapagunta
  • 188
  • 1
  • 11