In one of my activities there is button which pressing this button opens up a dialog fragment and inside this dialog fragment a web view appears. I use this web view for getting users information for an online store, but when the form loads on the web view, and I want to enter information soft keyboard does not come up. Displaying the web view in another activity is okay and everything is working fine and I can enter the data.
My main activity which calls the dialog fragment:
public class MainActivity extends AppCompatActivity implements InformationDialog44.InformationDialogInfo {
Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
activity=this;
Button button=(Button)findViewById(R.id.showButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InformationDialog44 dialog=new InformationDialog44();
dialog.show(getSupportFragmentManager(),"parvaz-tag");
}
});
}
}
And this is the main layout(content_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="ir.parvaz.uitests.MainActivity"
tools:showIn="@layout/activity_main">
<Button
android:id="@+id/showButton"
android:text="Show Dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
and this is the fragment layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="10dp"
android:padding="10dp"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Hello World!"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<WebView
android:id="@+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</LinearLayout>
and the fragment code:
public class InformationDialog44 extends DialogFragment {
InformationDialog44 myself;
InformationDialogInfo listener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
super.onCreateDialog(savedInstanceState);
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view=inflater.inflate(R.layout.dialog2,null);
builder.setView(view);
WebView myWebView=(WebView)view.findViewById(R.id.myWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, final String url) {
Log.i("parvaz-uitests","URL is: "+url);
}
}
);
myWebView.setBackgroundColor(Color.BLUE);
myself=this;
myWebView.loadUrl("http://www.google.com");
return builder.create();
}
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
listener=(InformationDialogInfo)activity;
}
catch(ClassCastException e)
{
Log.e("parvaz-error","Implement InformationDialogInfo. ");
dismiss();
}
}
public interface InformationDialogInfo
{
void onOKClick(InformationDialog44 informationDialog);
void onCancelClick(InformationDialog44 informationDialog);
}
}
Is there any restriction(probably for security reasons) to not allowing the keyboard on web view in dialog fragments?