-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNO INTERNET DIALOG
More file actions
124 lines (110 loc) · 5.07 KB
/
NO INTERNET DIALOG
File metadata and controls
124 lines (110 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//SOURCE CODE JAVA ---------------------------------------------------------------------------------------------------------------------------------------
public class MainActivity extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
//Intiallize webSetting
WebSettings webSetting = webView.getSettings();
webSetting.setJavaScriptEnabled(true);
//Initialize connectivityManager
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
//get active network info
NetworkInfo networkInfo =connectivityManager.getActiveNetworkInfo();
//Checknetwork status
if(networkInfo == null|| !networkInfo.isConnected() || !networkInfo.isAvailable()){
//when internet is inactive
//Initialize dialog
Dialog dialog = new Dialog(this);
//Set content view
dialog.setContentView(R.layout.alert_dialog);
//set outside touch
dialog.setCanceledOnTouchOutside(false);
//Set dialog width & height
dialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);
//Set transparent background
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//Set animation
dialog.getWindow().getAttributes().windowAnimations = android.R.style.Animation_Dialog;
//Initialize dialog variable
Button btnTryAgain = dialog.findViewById(R.id.btn_try_again);
btnTryAgain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
recreate();
}
});
//Show Dialog
dialog.show();;
}else{
//When Internet active
//Load url
webView.loadUrl("https://m.youtube.com");
}
}
}
//XML LAYOUT--------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/webView"/>
</RelativeLayout>
//RESOURCES FILES ------------------------------------------------------------------------------------------------------------------------
//----------------(A) ALERT DIALOG ------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp"
android:gravity="center"
android:background="@drawable/bg_round">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/no_internet"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ooops !"
android:textStyle="bold"
android:fontFamily="monospace"
android:textColor="@android:color/white"
android:textAppearance="@style/TextAppearance.AppCompat.Large"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Iternet Connection found.\n Check your connection"
android:textAlignment="center"
android:layout_marginTop="50dp"
android:fontFamily="serif"
android:textColor="@android:color/white"
android:textAppearance="@style/TextAppearance.AppCompat.Small"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_try_again"
android:text="Try Again"
android:textAllCaps="false"
android:textStyle="bold"
android:textColor="@android:color/white"
android:layout_marginTop="30dp"
android:background="@android:drawable/bottom_bar"/>
</LinearLayout>
//----------------(B) SHAPE OF DIALOG -----------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#009688"/>
<corners android:radius="20dp"/>
</shape>