3

How can I show no internet connection message like in facebook app that appears below the tab view.

Facebook message

Mihir
  • 2,064
  • 2
  • 21
  • 28
  • Try this open source project! https://github.com/johnkil/Android-AppMsg Check for connection to internet and display no internet connection notification ! Hope its helps! :) – Araib karim Jul 15 '13 at 06:09
  • its bit different in case of Facebook because it comes from behind the tab view and stays there and not disappear like toast. – Mihir Jul 15 '13 at 07:18

1 Answers1

5

You can design this in your XML file and then set its visibility to invisible like this:

<RelativeLayout
    android:id="@+id/relativelayout_connection"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:alpha="0.8"
    android:background="@android:color/black" >

    <TextView
        android:id="@+id/textView_noInternetConnection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:text="No Internet Connection."
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/white" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView_noInternetConnection"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/textView_noInternetConnection"
        android:src="@drawable/warrning" />
</RelativeLayout>

In your code use the broadcast receiver like this:

private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean noConnectivity = intent.getBooleanExtra(
                ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
        /*String reason = intent
                .getStringExtra(ConnectivityManager.EXTRA_REASON);*/
        boolean isFailover = intent.getBooleanExtra(
                ConnectivityManager.EXTRA_IS_FAILOVER, false);

        @SuppressWarnings("deprecation")
        NetworkInfo currentNetworkInfo = (NetworkInfo) intent
                .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        // NetworkInfo otherNetworkInfo = (NetworkInfo)
        // intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

        if (noConnectivity){    
            if(mRelativeLayout != null)
            mRelativeLayout.setVisibility(View.VISIBLE);

        }
        else if (currentNetworkInfo.isConnected()) {
            mRelativeLayout.setVisibility(View.GONE);


        } else if (isFailover) {
            Toast.makeText(getApplicationContext(), "Failover",
                    Toast.LENGTH_LONG).show();
        } else {
            mRelativeLayout.setVisibility(View.GONE);               
        }
    }       
};
Leigh
  • 28,765
  • 10
  • 55
  • 103
Aboulfotoh
  • 153
  • 1
  • 9