Reputation: 83
I have a webview and I want to be able to change the orientation of the displayed page programmtically. I prevent my activity from being recreated again when the user changes the orientation of the phone by setting this in my Manifest file.
android:screenOrientation="nosensor"
android:configChanges="orientation|screenSize|keyboardHidden"
However on one of my tabs I want to programmatically change the orientation of the webpage when the user changes it as well. So I want to listen to the changes in the phone orienatation and change that of the page view accordingly. Currently when the phone's orientation is changed to landscape the webpage still shows in portrait.
Can someone help?
Upvotes: 1
Views: 2638
Reputation: 82543
If you want to rotate only the WebView and nothing else, create a custom WebView like this:
public class VerticalWebView extends WebView {
final boolean topDown = true;
public VerticalWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void draw(Canvas canvas) {
if (topDown) {
canvas.translate(getHeight(), 0);
canvas.rotate(90);
} else {
canvas.translate(0, getWidth());
canvas.rotate(-90);
}
canvas.clipRect(0, 0, getWidth(), getHeight(), android.graphics.Region.Op.REPLACE);
super.draw(canvas);
}
}
(Change topDown
to false if you want to rotate the other way)
Now simply use it in your XML as follows:
<com.my.package.VerticalWebView
android:id="@+id/myview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.my.package.VerticalWebView>
Keep in mind that this just rotates the View shown on the screen, and any links etc. will fail to work as it does not remap touch coordinates to the new location of the corresponding point.
Upvotes: 1
Reputation: 499
Try this when you want to change to landscape/portrait. http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation(int)
Upvotes: 0