2017-10-23 4 views
0

ネットワークが切断されたときにダイアログを表示するネットワークアプリケーションを開発しようとしています。私はすべてを作ったが、ダイアログ内のテキストは中央にない。テキストをフルスクリーンで表示するダイアログ

package com.example.ayyappaboddupalli.networkchecker; 

import android.app.Application; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.IntentFilter; 
import android.net.ConnectivityManager; 
import android.support.v7.app.AlertDialog; 
import android.view.LayoutInflater; 
import android.view.View; 

/** 
* Created by ayyappaboddupalli on 10/23/2017. 
*/ 

public class ApplicationClass extends Application implements android.app.AlertDialog.OnDismissListener { 
    private static ApplicationClass mInstance; 
    private NetworkObserver networkObserver; 
    private NetworkDetector detector; 
    private AlertDialog.Builder ad = null; 
    private AlertDialog alertDialog; 

    public AlertDialog getAlertDialog() { 
     return alertDialog; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     networkObserver = new NetworkObserver(); 

    } 

    public static ApplicationClass getmInstance() { 

     if (mInstance == null) { 
      synchronized (Object.class) { 
       mInstance = mInstance == null ? new ApplicationClass() : mInstance; 
      } 
     } 
     return mInstance; 

    } 

    public NetworkObserver getNetworkObserver() { 
     if (networkObserver == null) { 
      networkObserver = new NetworkObserver(); 
      return networkObserver; 
     } else { 
      return networkObserver; 
     } 
    } 

    public void registerReceiver(Context context) { 
     detector = new NetworkDetector(); 
     context.registerReceiver(detector, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); 

    } 

    public void unRegisterReciver(Context context) { 
     try { 
      unregisterReceiver(detector); 

     } catch (Exception f) { 

     } 
    } 

    public void showDialog(Context context) { 
     if(alertDialog!=null) 
     { 
      alertDialog.dismiss(); 
     } 
      ad = new AlertDialog.Builder(context); 
      LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE); 
      View view = layoutInflater.inflate(R.layout.weak_net_dialog, null); 
      ad.setView(view); 
      alertDialog = ad.create(); 
      alertDialog.show(); 
      alertDialog.setCancelable(false); 

    } 

    @Override 
    public void onDismiss(DialogInterface dialog) { 
     alertDialog=null; 
    } 
} 

public class NetworkDetector extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 

    boolean isNetOn=isnetworkOnline(context); 
    ApplicationClass.getmInstance().getNetworkObserver().setValue(isNetOn); 

} 

public boolean isnetworkOnline(Context context) 
{ 
    try { 
     ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
     //should check null because in airplane mode it will be null 
     return (netInfo != null && netInfo.isConnected()); 
    } catch (NullPointerException e) { 
     e.printStackTrace(); 
     return false; 
    } 

} 

}

public class NetworkObserver extends Observable { 
    private boolean netWorkOff=false; 

    public boolean isNetWorkOff() { 
     return netWorkOff; 
    } 

    public void setValue(boolean netWorkOff) { 
     this.netWorkOff = netWorkOff; 
     setChanged(); 
     notifyObservers(); 
    } 
} 

@Override 
    public void update(Observable o, Object arg) { 

     if(ApplicationClass.getmInstance().getNetworkObserver().isNetWorkOff()) { 
      Toast.makeText(getApplicationContext(),"Network is online",Toast.LENGTH_LONG).show(); 
      if(ApplicationClass.getmInstance().getAlertDialog()!=null&&ApplicationClass.getmInstance().getAlertDialog().isShowing()) 
      ApplicationClass.getmInstance().getAlertDialog().dismiss(); 

     } 
     else 
     { 

      ApplicationClass.getmInstance().showDialog(this); 

//   Toast.makeText(getApplicationContext(),"Network is offline",Toast.LENGTH_LONG).show(); 

     } 
    } 
+0

あなたは –

+0

weak_net_dialog'レイアウトファイルを '共有することはできますが、' weak_net_dialog'という名前のカスタムレイアウトでテキストを中央にしようとしましたか? –

+0

リニアレイアウトを使用している場合、アラートダイアログテキストを含むレイアウトのlayout_gravity = "center"を追加します。または、relativelayoutが使用されている場合は、アンドロイドを追加してください:center_horizo​​ntal = true textview –

答えて

0

ウルxmlファイル内の行を次のように使用:あなたはrelativeLayout あなたの対話とEDITTEXTのmatch_parentするも設定幅を使用している場合、それは動作します

android:center_vertical = true 
android:center_horizontal=true 

+0

これは動作していません –

+0

dialogの幅と高さをmatch_parentに設定していますか? –

+0

@RushiAyyappaあなたが私とあなたのプロジェクトを共有しても大丈夫なら私は私のマシンで走って見て、それを修正して、それをuに送信しようと思います:) –

関連する問題