I have a dialog fragment in my application which I want to occupy the whole screen. Since I need my fragment to extend my custom layout and want to add views to this dialog dynamically I wrote the code as below :-
my_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<com.example.MyDialogRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dialogRootLayout"
android:orientation="vertical"
android:background="#FFFFFF"
>
</com.example.MyDialogRelativeLayout>
Now inside my dialog java class I wrote :-
MyDialogFragment.java
public class MyDialogFragment extends DialogFragment {
WebView webView;
MyDialogRelativeLayout dialogLayout;
String url;
public MyDialogFragment(String url)
{
this.url = url;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.my_custom_dialog,container, false);
dialogLayout = (MyDialogRelativeLayout) rootView.findViewById(R.id.dialogRootLayout);
return rootView;
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int height = dm.heightPixels;
int width = dm.widthPixels;
Log.d("Dialog","Height = "+height+" Widht = "+width);
getDialog().getWindow().setLayout(width-50,height);
RelativeLayout.LayoutParams paramsWeb = new RelativeLayout.LayoutParams(width,height);
final WebView wv = new WebView(getActivity());
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(url);
wv.setWebViewClient(new WebViewClient(){
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
wv.loadUrl("file:///android_res/raw/nointernet.html");
}
});
dialogLayout.addView(wv,paramsWeb);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
Button closeButton = new Button(getActivity());
closeButton.setText("Close");
closeButton.setBackground(null);
dialogLayout.addView(closeButton,params);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDialog().dismiss();
}
});
}
public class myWebViewClient extends WebViewClient{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
My problem is that, the webview doesnot occupy the whole window of the dialog which is drawn to the screen. There is a black layer/area on top of the dialog and the webview is below it.
How to get rid of the top black portion and make the webview fill the whole dialog ?
Use the following for setting the height and width inside your fragment class: getDialog().getWindow().setLayout(width, height);
For removing the black part(title) use the following in your fragment class: getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);