Это так глупо. Я вместе взломал что-то, что сработало для меня, надеюсь, оно сработает и для вас
public class AuthRequestDialogFragment extends DialogFragment
{
@InjectView(R.id.dauth_userinput)
public EditText userinput;
@InjectView(R.id.dauth_passinput)
public EditText passinput;
@OnClick(R.id.dauth_login)
public void login(View view) {
((Callback) getTargetFragment()).login(userinput.getText().toString(), passinput.getText().toString());
this.dismiss();
}
@OnClick(R.id.dauth_cancel)
public void cancel(View view) {
((Callback) getTargetFragment()).cancel();
this.dismiss();
}
public static interface Callback
{
public void login(String username, String password);
public void cancel();
}
@Override
public void onStart() {
super.onStart();
WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
getDialog().getWindow().setLayout(width*2/3, height/5*2);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.dialog_authrequest, container);
ButterKnife.inject(this, view);
getDialog().setTitle("Authorization required");
return view;
}
}
И
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/dauth_requsertext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The server requires a username and password."
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"/>
<RelativeLayout
android:id="@+id/dauth_centercontainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="@+id/dauth_usertext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"/>
<TextView
android:id="@+id/dauth_passtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Password"
android:layout_below="@+id/dauth_usertext"
android:layout_alignLeft="@+id/dauth_usertext"
android:layout_alignStart="@+id/dauth_usertext"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dauth_userinput"
android:ems="12"
android:layout_alignBottom="@+id/dauth_usertext"
android:layout_toRightOf="@id/dauth_usertext"
android:layout_toEndOf="@id/dauth_usertext"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dauth_passinput"
android:layout_marginTop="20dp"
android:inputType="textPassword"
android:ems="12"
android:layout_alignBottom="@+id/dauth_passtext"
android:layout_toRightOf="@id/dauth_passtext"
android:layout_toEndOf="@id/dauth_passtext"
android:layout_alignLeft="@id/dauth_userinput"
android:layout_alignStart="@id/dauth_userinput"/>
</RelativeLayout>
<Button
android:id="@+id/dauth_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"/>
<Button
android:id="@+id/dauth_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log In"
android:layout_alignTop="@+id/dauth_cancel"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_toLeftOf="@+id/dauth_cancel"/>
</RelativeLayout>
И
public class WebViewFragment extends Fragment implements AuthRequestDialogFragment.Callback {
@Override
public void login(String username, String password) {
Log.d(this.getClass().getName(), "Login");
myWebViewClient.login(username, password);
}
@Override
public void cancel() {
Log.d(this.getClass().getName(), "Cancel");
myWebViewClient.cancel();
}
И самое главное:
private class MyWebViewClient extends WebViewClient {
private WebView myView;
private HttpAuthHandler httpAuthHandler;
private String host;
private String realm;
@Override
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
AuthRequestDialogFragment authRequestDialogFragment = new AuthRequestDialogFragment();
FragmentManager fragmentManager = ((getActivity()).getSupportFragmentManager());
authRequestDialogFragment.setTargetFragment(WebViewFragment.this, 0);
authRequestDialogFragment.show(fragmentManager, "dialog");
this.httpAuthHandler = handler;
this.myView = view;
this.host = host;
this.realm = realm;
}
public void login(String username, String password) {
httpAuthHandler.proceed(username, password);
myView = null;
httpAuthHandler = null;
host = null;
realm = null;
}
public void cancel() {
super.onReceivedHttpAuthRequest(myView, httpAuthHandler, host, realm);
myView = null;
httpAuthHandler = null;
host = null;
realm = null;
}
}
Использует зависимость:
compile 'com.jakewharton:butterknife:6.0.0'