How to get the orientation

Hi, I’m trying to get the orientation of the device to place a text inside a rectangle which variate it size depending on the device orientation (PORTRAIT or LANDSCAPE).

Using processing 3.5.3 in android mode.

Someone know’s how to do it? I don’t want to resize the textsize, just want the same size but putting \n when the space finishes.

Thanks!

EDIT----

Want that top right text to change depending on orientation or just put the text all in one lane.

Hey there Waboqueox,

Never tried Processing for Android before, but going to guess it’s similar to the computer version. If not, hopefully some of the information is still relevant.

When an interface has to adjust to various screen resolutions, you have to come up with a responsive design that suits your needs. This means that for some of the interface components you have to prevent filling in static values in advance. In your case these are the width values of the rectangles, smiley(?), and text boxes in the header.

A way to tackle it is to let your program calculate how wide it should be. I’m fond of using a function right at the start of void draw() that calculates values related to the size of my sketch’s window, such as the dimensions of spacing, margins, and grids. That way it’s not an issue to change the sketch resolution in a later stadium of developing it, since this function recalculates all values that influence the interface layout.

Orientation
To get the orientation you have to compare the width of the window with its height. Is one of these bigger than the other, it should be easy to validate whether it’s landscape or portrait. An if loop should do the trick.

Responsive interface
For the sake of simplicity I’m going to assume that your smiley has a static width of 100px (in both landscape and portrait mode) and is exactly in the center of the screen, meaning the space left of the smiley is the same size as the space on the right. This could be calculated as followed:

float total_space, space_left, space_right;

void setup() {
  size(400, 400);
}

void draw() {
  total_space = width - 100;
  space_left = space_right = total_space / 2;
}

Does this answer your question(s)?

1 Like

@Waboqueox ===

  • in order to get the current orientation:
this.getActivity().getResources().getConfiguration().orientation, supposing that you want only to know landscape vs portrait. Then you create some boolean and change your text if...
2 Likes

Oohh, thanks @Tiemen, I was so focus on get the orientation, that I didn’t think about that simple trick :stuck_out_tongue:

I have my goal reached and my text is finally how it should be.

My question is solved!!

And thanks @akenaton, I tried it before post and it didn’t work, but magically now its working :upside_down_face::upside_down_face:… Have problems using the android resources in processing. Going to try it again and if can’t get it, I will open a new post answering for help on it, because I don’t know how to start another activity

Just if is fast, or easily solved, here is my error:

  Intent intent = new Intent(this, OtherSketch/ OtherClass.class);
  startActivity(intent);

Have an error: "The constructor Intent(MySketch, Class<MySketch.OtherSketch/ OtherClass>) is undefined "

Sorry for the delay answering and lot of thanks :blush:.

@Waboqueox===
looking to the code you posted i think that i can understand why “magically” i’s working for the other question; you probably forgot that you are in a fragment…
So as for the other question the code must be

Intent intent = new Intent(this.getActivity(), AnotherActivity.class);
        startActivity(intent);

Now the constructor will be ok but you probably get other problems: try!

1 Like