My code for opening an input dialog reads as follows:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(\"Dialog Title\");
In the following code snippet I show how to host an arbitrary LinearLayout
in a DialogFragment
. One using AlertDialog
(where the soft keyboard doesn't work) and another way without using AlertDialog
(where the soft keyboard does work!):
public static LinearLayout createLinearLayout(Context context)
{
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
String html;
html = "\n";
WebView webView = new WebView(context);
webView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
webView.loadDataWithBaseURL("http://www.google.com", html, "text/html", "UTF-8", null);
linearLayout.addView(webView);
return linearLayout;
}
public void showWithAlertDialog()
{
DialogFragment dialogFragment = new DialogFragment()
{
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder
.setTitle("With AlertDialog")
.setView(createLinearLayout(getActivity()));
return builder.create();
}
};
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
dialogFragment.show(fragmentTransaction, "dialog");
}
public void showWithoutAlertDialog()
{
DialogFragment dialogFragment = new DialogFragment()
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
getDialog().setTitle("Without AlertDialog");
getDialog().setCanceledOnTouchOutside(false);
return createLinearLayout(getActivity());
}
};
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
dialogFragment.show(fragmentTransaction, "dialog");
}