Мое диалоговое окно с предупреждением «нет интернета» не отображается в полноэкранном режиме, а также упоминаю, что моя кнопка после того, как я нажимаю, не обновляясь, просто исчезает. это не сработало..
Это мой код..
Файл класса:
public class NetworkChangeListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (!Common.isConnectedToInternet(context)){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View view = LayoutInflater.from(context).inflate(R.layout.alert_dialog,null);
builder.setView(view);
AppCompatButton btnRetry = view.findViewById(R.id.btnRetry);
AlertDialog dialog = builder.create();
dialog.show();
dialog.setCancelable(false);
dialog.getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
btnRetry.setOnClickListener(view1 -> {
dialog.dismiss();
onReceive(context,intent);
});
}
}
}
XML-файл:
<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:background = "#f7f7f7"
android:id = "@+id/nointernet">
<ImageView
android:id = "@+id/imageView"
android:layout_width = "212dp"
android:layout_height = "188dp"
android:scaleType = "centerCrop"
android:layout_centerHorizontal = "true"
android:layout_marginTop = "250dp"
android:src = "@drawable/ic_baseline_signal_wifi_off_24"></ImageView>
<TextView
android:id = "@+id/text"
android:layout_width = "253dp"
android:layout_height = "52dp"
android:text = "Error!"
android:textAlignment = "center"
android:textSize = "25dp"
android:layout_below = "@id/imageView"
android:layout_centerInParent = "true"></TextView>
<TextView
android:id = "@+id/text2"
android:layout_width = "337dp"
android:layout_height = "63dp"
android:text = "No internet connection"
android:textAlignment = "center"
android:textSize = "21dp"
android:layout_below = "@id/text"
android:layout_centerInParent = "true"></TextView>
<Button
android:id = "@+id/btnRetry"
android:layout_width = "213dp"
android:layout_height = "49dp"
android:text = "Try again"
android:layout_below = "@id/text2"
android:layout_centerInParent = "true"></Button>
</RelativeLayout>
Основная деятельность:
@Override
protected void onStart() {
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkChangeListener,intentFilter);
super.onStart();
recreate();
}
@Override
protected void onStop() {
unregisterReceiver(networkChangeListener);
super.onStop();
}




установите minWidth и minHeight 800dp/1000dp в RelativeLayout вашего файла макета диалогового окна.
<RelativeLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:background = "#f7f7f7"
android:id = "@+id/nointernet"
minWidth = "1000dp"
minHeight = "1000dp">
// rest of the views.
</RelativeLayout>
я добавил это, и он отлично работает ..
<style>
<item name = "android:layout_width">match_parent</item>
<item name = "android:layout_height">match_parent</item>
<item name = "backgroundColor">#f7f7f7</item>
<item name = "android:windowBackground">#f7f7f7</item>
</style>
И код для кнопки, которая перезагрузит веб-страницу после воссоздания активности:
btnRetry.setOnClickListener(view1 -> {
if (Common.isConnectedToInternet(context)) {
dialog.dismiss();
onReceive(context,intent);
((MainActivity)context).recreate();
}
});
я решил эту проблему, но я не могу перезагрузить веб-страницу, когда интернет активен... я хочу перезагрузить страницу при нажатии кнопки... я не могу вызвать "reload();" функция в моем классе «NetworkChangeListener», потому что мой веб-просмотр вызывается в MainActivity (protected void onCreate(Bundle saveInstanceState))..