Thursday, December 5, 2019

Internet Dialog in Android


Create XML with one Button(id is retry) and Image View.


<LinearLayout android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_margin="20dp"
    android:layout_height="wrap_content">


    <ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_below="@id/title"
        android:src="@drawable/internet"/>

    <Button
        android:id="@+id/retry"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@id/imageview"
        android:theme="@style/Button"
        android:gravity="center"
        android:layout_margin="10dp"
        android:text="Retry"
        android:textColor="@android:color/white"
        android:textStyle="bold"/>

</LinearLayout>


Here is dialog for internet
    public static void showInternetdialog(final Context context, final Fragment fragment) {
    final Dialog dialog;
    dialog = new Dialog(context);
    dialog.setContentView(R.layout.internet);
    final PetHealthTextView refresh = dialog.findViewById(R.id.retry);
    refresh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isConnected(context)) {
                if (fragment != null) {
                    FragmentTransaction ft = ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction();
                    ft.detach(fragment).attach(fragment).commit();
                } else {
                    ((Activity) context).recreate();
                }
                dialog.cancel();
            } else dialog.show();
            
        }
    });
    dialog.setCancelable(false);
    dialog.show();
    }
isConnected method
    public static boolean isConnected(Context context) {
    if (!ConnectivityReceiver.isConnected(context)) {
        return false;
    }
    return true;
    }
ConnectivityReceiver.class
public class ConnectivityReceiver extends BroadcastReceiver {
public static ConnectivityReceiverListener connectivityReceiverListener;
@Override
public void onReceive(Context context, Intent intent) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();

    if (connectivityReceiverListener != null) {
        connectivityReceiverListener.onNetworkConnectionChanged(isConnected);
    }
}

public static boolean isConnected(Context context) {
    ConnectivityManager
            cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();
}    public interface ConnectivityReceiverListener {
    void onNetworkConnectionChanged(boolean isConnected);
}

}

That is all. If any help related to this post please comment.

Thank you, guys.

Enjoy coding.

No comments:

Post a Comment