How to force portrait orientation my app on an android box?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
So I have this app that's set to display on portrait mode. When I run it on android, it goes "portrait" but only in the center of the screen. It doesn't do what you'll except. For example on tablets where it goes portrait mode and fills the entire screen.
I tried every well-known root access app but they can't give me root access to be able to edit build.prop. KingRoot even said that my device is too strong.
So, what I planned now is do it via code. My app is quite simple so it won't give me a lot of problems on the design side of things. It just contains a webview and nothing else.
I want it to work even on non-Android TV boxes because we use various kinds on android boxes and it'll be a problem for me if it will only work on real Android TV boxes.
After some experimenting, I made this code.
if(forcePotrait){
webview.setRotation(-90f);
int measuredWidth = 0;
int measuredHeight = 0;
Point size = new Point();
WindowManager w = getWindowManager();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
w.getDefaultDisplay().getSize(size);
measuredWidth = size.x;
measuredHeight = size.y;
}else{
Display d = w.getDefaultDisplay();
measuredWidth = d.getWidth();
measuredHeight = d.getHeight();
}
int scrData = getScreenDimension();
ConstraintLayout.LayoutParams lytParams = new ConstraintLayout.LayoutParams(scrData[1], scrData[0]);
lytMain.removeAllViews();
lytMain.addView(webview, 0, lytParams);
}
getScreenDimension
is a method that get the screen size of the device.
It rotates the WebView
but it occupies just a portion of the screen and displays just a portion of the page as well. I'm using ConstraintLayout
as my base layout. I also turned on the Show Layout Bounds
on Developer Options
and it seems like the WebView
is not drawn properly because the red line is a few dps of the center which isn't anywhere near the WebView
s display.
I found this and used it on my project: https://stackoverflow.com/a/26711189/10464730. This is the code in the link.
public class VWebView extends WebView {
final boolean topDown = true;
public VWebView(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);
}
}
Now, I don't need to rotate the WebView
manually. But the WebView
still doesn't fill the entire screen.
With the code below which uses values that are close to what I get on screen size methods, I get the screenshot result.
ConstraintLayout.LayoutParams lytParams = new ConstraintLayout.LayoutParams(1300, 1280);
webview.setLayoutParams(lytParams);
android
add a comment |
So I have this app that's set to display on portrait mode. When I run it on android, it goes "portrait" but only in the center of the screen. It doesn't do what you'll except. For example on tablets where it goes portrait mode and fills the entire screen.
I tried every well-known root access app but they can't give me root access to be able to edit build.prop. KingRoot even said that my device is too strong.
So, what I planned now is do it via code. My app is quite simple so it won't give me a lot of problems on the design side of things. It just contains a webview and nothing else.
I want it to work even on non-Android TV boxes because we use various kinds on android boxes and it'll be a problem for me if it will only work on real Android TV boxes.
After some experimenting, I made this code.
if(forcePotrait){
webview.setRotation(-90f);
int measuredWidth = 0;
int measuredHeight = 0;
Point size = new Point();
WindowManager w = getWindowManager();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
w.getDefaultDisplay().getSize(size);
measuredWidth = size.x;
measuredHeight = size.y;
}else{
Display d = w.getDefaultDisplay();
measuredWidth = d.getWidth();
measuredHeight = d.getHeight();
}
int scrData = getScreenDimension();
ConstraintLayout.LayoutParams lytParams = new ConstraintLayout.LayoutParams(scrData[1], scrData[0]);
lytMain.removeAllViews();
lytMain.addView(webview, 0, lytParams);
}
getScreenDimension
is a method that get the screen size of the device.
It rotates the WebView
but it occupies just a portion of the screen and displays just a portion of the page as well. I'm using ConstraintLayout
as my base layout. I also turned on the Show Layout Bounds
on Developer Options
and it seems like the WebView
is not drawn properly because the red line is a few dps of the center which isn't anywhere near the WebView
s display.
I found this and used it on my project: https://stackoverflow.com/a/26711189/10464730. This is the code in the link.
public class VWebView extends WebView {
final boolean topDown = true;
public VWebView(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);
}
}
Now, I don't need to rotate the WebView
manually. But the WebView
still doesn't fill the entire screen.
With the code below which uses values that are close to what I get on screen size methods, I get the screenshot result.
ConstraintLayout.LayoutParams lytParams = new ConstraintLayout.LayoutParams(1300, 1280);
webview.setLayoutParams(lytParams);
android
Is the Android box running Android TV? See: developer.android.com/training/tv/start/layouts
– Morrison Chang
Nov 29 '18 at 2:46
As much as possible I want it to work even on non-Android TV boxes.
– rminaj
Nov 29 '18 at 2:57
If you force an app orientation to portrait mode only, you will get the first case. Why don't you add support for the landscape view design? For TV, it is naturally a landscape oriented device, so it is natural to design a TV app in landscape view. But, if you need both viewing modes, then support both and you can programmatically configure the app orientation to landscape mode when it detects a TV device.
– ecle
Nov 29 '18 at 3:43
The app is a kiosk app so I need it on portrait mode. On the support for landscape mode, the layout I have only contains a webview. Do I really need to have a layout_land version of the layout?
– rminaj
Nov 29 '18 at 3:58
add a comment |
So I have this app that's set to display on portrait mode. When I run it on android, it goes "portrait" but only in the center of the screen. It doesn't do what you'll except. For example on tablets where it goes portrait mode and fills the entire screen.
I tried every well-known root access app but they can't give me root access to be able to edit build.prop. KingRoot even said that my device is too strong.
So, what I planned now is do it via code. My app is quite simple so it won't give me a lot of problems on the design side of things. It just contains a webview and nothing else.
I want it to work even on non-Android TV boxes because we use various kinds on android boxes and it'll be a problem for me if it will only work on real Android TV boxes.
After some experimenting, I made this code.
if(forcePotrait){
webview.setRotation(-90f);
int measuredWidth = 0;
int measuredHeight = 0;
Point size = new Point();
WindowManager w = getWindowManager();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
w.getDefaultDisplay().getSize(size);
measuredWidth = size.x;
measuredHeight = size.y;
}else{
Display d = w.getDefaultDisplay();
measuredWidth = d.getWidth();
measuredHeight = d.getHeight();
}
int scrData = getScreenDimension();
ConstraintLayout.LayoutParams lytParams = new ConstraintLayout.LayoutParams(scrData[1], scrData[0]);
lytMain.removeAllViews();
lytMain.addView(webview, 0, lytParams);
}
getScreenDimension
is a method that get the screen size of the device.
It rotates the WebView
but it occupies just a portion of the screen and displays just a portion of the page as well. I'm using ConstraintLayout
as my base layout. I also turned on the Show Layout Bounds
on Developer Options
and it seems like the WebView
is not drawn properly because the red line is a few dps of the center which isn't anywhere near the WebView
s display.
I found this and used it on my project: https://stackoverflow.com/a/26711189/10464730. This is the code in the link.
public class VWebView extends WebView {
final boolean topDown = true;
public VWebView(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);
}
}
Now, I don't need to rotate the WebView
manually. But the WebView
still doesn't fill the entire screen.
With the code below which uses values that are close to what I get on screen size methods, I get the screenshot result.
ConstraintLayout.LayoutParams lytParams = new ConstraintLayout.LayoutParams(1300, 1280);
webview.setLayoutParams(lytParams);
android
So I have this app that's set to display on portrait mode. When I run it on android, it goes "portrait" but only in the center of the screen. It doesn't do what you'll except. For example on tablets where it goes portrait mode and fills the entire screen.
I tried every well-known root access app but they can't give me root access to be able to edit build.prop. KingRoot even said that my device is too strong.
So, what I planned now is do it via code. My app is quite simple so it won't give me a lot of problems on the design side of things. It just contains a webview and nothing else.
I want it to work even on non-Android TV boxes because we use various kinds on android boxes and it'll be a problem for me if it will only work on real Android TV boxes.
After some experimenting, I made this code.
if(forcePotrait){
webview.setRotation(-90f);
int measuredWidth = 0;
int measuredHeight = 0;
Point size = new Point();
WindowManager w = getWindowManager();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
w.getDefaultDisplay().getSize(size);
measuredWidth = size.x;
measuredHeight = size.y;
}else{
Display d = w.getDefaultDisplay();
measuredWidth = d.getWidth();
measuredHeight = d.getHeight();
}
int scrData = getScreenDimension();
ConstraintLayout.LayoutParams lytParams = new ConstraintLayout.LayoutParams(scrData[1], scrData[0]);
lytMain.removeAllViews();
lytMain.addView(webview, 0, lytParams);
}
getScreenDimension
is a method that get the screen size of the device.
It rotates the WebView
but it occupies just a portion of the screen and displays just a portion of the page as well. I'm using ConstraintLayout
as my base layout. I also turned on the Show Layout Bounds
on Developer Options
and it seems like the WebView
is not drawn properly because the red line is a few dps of the center which isn't anywhere near the WebView
s display.
I found this and used it on my project: https://stackoverflow.com/a/26711189/10464730. This is the code in the link.
public class VWebView extends WebView {
final boolean topDown = true;
public VWebView(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);
}
}
Now, I don't need to rotate the WebView
manually. But the WebView
still doesn't fill the entire screen.
With the code below which uses values that are close to what I get on screen size methods, I get the screenshot result.
ConstraintLayout.LayoutParams lytParams = new ConstraintLayout.LayoutParams(1300, 1280);
webview.setLayoutParams(lytParams);
android
android
edited Dec 4 '18 at 5:36
rminaj
asked Nov 29 '18 at 2:36
rminajrminaj
358
358
Is the Android box running Android TV? See: developer.android.com/training/tv/start/layouts
– Morrison Chang
Nov 29 '18 at 2:46
As much as possible I want it to work even on non-Android TV boxes.
– rminaj
Nov 29 '18 at 2:57
If you force an app orientation to portrait mode only, you will get the first case. Why don't you add support for the landscape view design? For TV, it is naturally a landscape oriented device, so it is natural to design a TV app in landscape view. But, if you need both viewing modes, then support both and you can programmatically configure the app orientation to landscape mode when it detects a TV device.
– ecle
Nov 29 '18 at 3:43
The app is a kiosk app so I need it on portrait mode. On the support for landscape mode, the layout I have only contains a webview. Do I really need to have a layout_land version of the layout?
– rminaj
Nov 29 '18 at 3:58
add a comment |
Is the Android box running Android TV? See: developer.android.com/training/tv/start/layouts
– Morrison Chang
Nov 29 '18 at 2:46
As much as possible I want it to work even on non-Android TV boxes.
– rminaj
Nov 29 '18 at 2:57
If you force an app orientation to portrait mode only, you will get the first case. Why don't you add support for the landscape view design? For TV, it is naturally a landscape oriented device, so it is natural to design a TV app in landscape view. But, if you need both viewing modes, then support both and you can programmatically configure the app orientation to landscape mode when it detects a TV device.
– ecle
Nov 29 '18 at 3:43
The app is a kiosk app so I need it on portrait mode. On the support for landscape mode, the layout I have only contains a webview. Do I really need to have a layout_land version of the layout?
– rminaj
Nov 29 '18 at 3:58
Is the Android box running Android TV? See: developer.android.com/training/tv/start/layouts
– Morrison Chang
Nov 29 '18 at 2:46
Is the Android box running Android TV? See: developer.android.com/training/tv/start/layouts
– Morrison Chang
Nov 29 '18 at 2:46
As much as possible I want it to work even on non-Android TV boxes.
– rminaj
Nov 29 '18 at 2:57
As much as possible I want it to work even on non-Android TV boxes.
– rminaj
Nov 29 '18 at 2:57
If you force an app orientation to portrait mode only, you will get the first case. Why don't you add support for the landscape view design? For TV, it is naturally a landscape oriented device, so it is natural to design a TV app in landscape view. But, if you need both viewing modes, then support both and you can programmatically configure the app orientation to landscape mode when it detects a TV device.
– ecle
Nov 29 '18 at 3:43
If you force an app orientation to portrait mode only, you will get the first case. Why don't you add support for the landscape view design? For TV, it is naturally a landscape oriented device, so it is natural to design a TV app in landscape view. But, if you need both viewing modes, then support both and you can programmatically configure the app orientation to landscape mode when it detects a TV device.
– ecle
Nov 29 '18 at 3:43
The app is a kiosk app so I need it on portrait mode. On the support for landscape mode, the layout I have only contains a webview. Do I really need to have a layout_land version of the layout?
– rminaj
Nov 29 '18 at 3:58
The app is a kiosk app so I need it on portrait mode. On the support for landscape mode, the layout I have only contains a webview. Do I really need to have a layout_land version of the layout?
– rminaj
Nov 29 '18 at 3:58
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531021%2fhow-to-force-portrait-orientation-my-app-on-an-android-box%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531021%2fhow-to-force-portrait-orientation-my-app-on-an-android-box%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is the Android box running Android TV? See: developer.android.com/training/tv/start/layouts
– Morrison Chang
Nov 29 '18 at 2:46
As much as possible I want it to work even on non-Android TV boxes.
– rminaj
Nov 29 '18 at 2:57
If you force an app orientation to portrait mode only, you will get the first case. Why don't you add support for the landscape view design? For TV, it is naturally a landscape oriented device, so it is natural to design a TV app in landscape view. But, if you need both viewing modes, then support both and you can programmatically configure the app orientation to landscape mode when it detects a TV device.
– ecle
Nov 29 '18 at 3:43
The app is a kiosk app so I need it on portrait mode. On the support for landscape mode, the layout I have only contains a webview. Do I really need to have a layout_land version of the layout?
– rminaj
Nov 29 '18 at 3:58