Android dialog orienation issue
I used a customized dialog to show the face-book login page. I used a webview inside a linear layout to show the login page. It works fine. But when the orientation changes it doesn't fit to screen size. I need to have dialog box which resize with orientation. please help me with some sample code.
public FbDialog(Context context, String url, DialogListener listener) {
super(context);
mUrl = url;
mListener = listener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSpinner = new ProgressDialog(getContext());
mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);
mSpinner.setMessage("Loading...");
mContent = new LinearLayout(getContext());
mContent.setOrientation(LinearLayout.VERTICAL);
setUpTitle();
setUpWebView();
Display display = getWindow().getWindowManager().getDefaultDisplay();
final float scale =
getContext().getResources().getDisplayMetrics().density;
int orientation =
getContext().getResources().getConfiguration().orientation;
float[] dimensions =
(orientation == Configuration.ORIENTATION_LANDSCAPE)
开发者_运维百科? DIMENSIONS_DIFF_LANDSCAPE : DIMENSIONS_DIFF_PORTRAIT;
addContentView(mContent, new LinearLayout.LayoutParams(
display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)),
display.getHeight() - ((int) (dimensions[1] * scale + 0.5f))));
}
private void setUpTitle() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Drawable icon = getContext().getResources().getDrawable(R.drawable.facebook_icon);
mTitle = new TextView(getContext());
mTitle.setText("Facebook");
mTitle.setTextColor(Color.WHITE);
mTitle.setTypeface(Typeface.DEFAULT_BOLD);
mTitle.setBackgroundColor(FB_BLUE);
mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN);
mTitle.setCompoundDrawablePadding(MARGIN + PADDING);
// mTitle.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
mContent.addView(mTitle);
}
private void setUpWebView() {
mWebView = new WebView(getContext());
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setWebViewClient(new FbDialog.FbWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(mUrl);
mWebView.setLayoutParams(FILL);
mContent.addView(mWebView);
}
Assuming you have created the dialog layout using xml, you can create another xml for landscape layout and place it inside layout-land folder. This way this xml will be loaded when the orientation changes.
Well, in create two more folder:
/res/layout-port
/res/layout-land
Create two according-layout XMLs and put into those 2 folders. The corresponding layout will be inflated if orientation changes.
it looks like you cant detect the orientation change in a Dialog as there is no OnConfigurationChanged().
So when you rotate the screen from Portrait to Landscape is chops off the bottom of the dialog and you cant scroll down.
The simplest solution I found was to add vertical scrollbars to the webView and then during onCreate(), calculate the smallest value of width and height and use this value for both width and height. This creates a square dialog which will fit in both portrait and landscape views without having to worry about resizing. Its not a great solution, but it works...
int height = display.getHeight();
int width = display.getWidth();
int size = height > width ? width : height;
addContentView(mContent, new LinearLayout.LayoutParams(size - 10, size - 10));
精彩评论