본문 바로가기

Develop/Android SDK

안드로이드 웹뷰(webview)안에서 alert 처리

웹뷰안에서 alert 를 하면 서버 host url이 나타난다.. 그걸 없애주기위해선 아래처럼 따로 구현을 해줘야함.


webView.setWebChromeClient(new ChromeClient() {
   
   @Override
   public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result){
      new AlertDialog.Builder(view.getContext())
         .setTitle("메세지")
         .setMessage(message)
         .setPositiveButton(android.R.string.ok,
               new AlertDialog.OnClickListener(){
                  public void onClick(DialogInterface dialog, int which) {
                     result.confirm();
                  }
               })
         .setCancelable(true)
         .create()
         .show();

      return true;
   };
   
   @Override
   public boolean onJsConfirm(WebView view, String url, String message, final android.webkit.JsResult result){
      new AlertDialog.Builder(view.getContext())
         .setTitle("메세지")
         .setMessage(message)
         .setPositiveButton(android.R.string.ok,
               new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                     result.confirm();
                  }
               })
         .setNegativeButton(android.R.string.cancel,
               new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                     result.cancel();
                  }
               })
         .create()
         .show();

      return true;
   };
   
  });