2017-10-23 17 views
0

네트워크 연결이 끊어졌을 때 대화 상자를 표시하는 네트워크 앱을 개발하려고합니다. 모든 것을 만들었지 만 대화 상자 안의 텍스트는 가운데에 있지 않습니다. UR XML 파일의 줄을 다음텍스트를 전체 화면으로 가운데 맞춤 대화 상자

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을 사용하는 경우 android : center_horizontal = true를 텍스트 뷰에 추가하십시오. –

답변

0

사용 : 당신이 당신의 대화와 EDITTEXT

의 match_parent하는 RelativeLayout의 에게도 설정 폭을 사용하는 경우

android:center_vertical = true 
android:center_horizontal=true 

그것은 작동합니다

+0

이 사람이 작동하지 않습니다 –

+0

너비와 높이를 match_parent로 설정하고 있습니까? –

+0

@RushiAyyappa 만약 당신이 나에게 내 컴퓨터에서 실행하여 그것을 보게 될 것입니다 그것을 해결하고 그것을 보내려고 나와 함께 프로젝트를 공유 할 수 있습니다 :) –

0

시험해보기 :

android:layout_gravity="center"