0

I am trying to test an app for the resolution. I am able to get the screen or devices resolution by the following code: Get Screen Height and Width of android device

However, the app is running at the different resolution than devices resolution. How would i get the resolution that the App is running at rather then devices screen resolution.

  • Can you elaborate on your question? Do you mean that the images that your app is using are at a lower resolution? Below is a link to an answer resolving px to dp for converting resolutions between devices, which might shed some light: https://stackoverflow.com/questions/8309354/formula-px-to-dp-dp-to-px-android?noredirect=1&lq=1 – cjnash Mar 12 '18 at 15:05
  • Thank you for replying, I am running one app in different resolution by modifying the app config file. Then I want to test if that app launched in that resolution. For example, screen revolution is at 1680X 1080 but I am launching the app in 1080X 720. The n check the app launched in 1080X720 settings. – user2782345 Mar 12 '18 at 15:15

1 Answers1

0

While I may not fully understand your problem, I think this is along the lines of what you are looking for. This will get the current view and measure the height and width. If you are doing automated tests then I think ViewTreeObserver is safer than using View, which is why I included ViewTreeObserver instead. I am pretty sure that this will return the width and height of the current view, which will be different then what the screen resolution is.

ViewTreeObserver viewTreeObserver = YOUR_VIEW_TO_MEASURE.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                YOUR_VIEW_TO_MEASURE.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                int viewHeight = YOUR_VIEW_TO_MEASURE.getHeight();
                int viewWeight = YOUR_VIEW_TO_MEASURE.getWidth();

            }
        });
    }

If you want to compare this to the current resolution of the device, not the view, it looks like you have already found and linked a correct solution in your question.

Note: if you are testing your program, make sure that you are calling this after you have created your view because if you call this before it might return 0 or the incorrect view sizes.

cjnash
  • 1,228
  • 3
  • 19
  • 37
  • Thank you so much for the ans, I am in UI testing class and I am launching a third party app. How would i get the YOUR_VIEW_TO_MEASURE for that app? – user2782345 Mar 12 '18 at 16:13
  • @user2782345 that would just be the view that you are currently in. Try some of these solutions: https://stackoverflow.com/questions/5273436/how-to-get-activitys-content-view – cjnash Mar 12 '18 at 16:26
  • The problem is I am in the test class so I do not have this.findviewbyid. What would be my this? – user2782345 Mar 12 '18 at 16:34
  • hmm, you may want to try: `View YOUR_VIEW_TO_MEASURE = View.getRootView()` or something like `View YOUR_VIEW_TO_MEASURE = findViewById(R.id.testing);` where you pass in the id of the view you are testing – cjnash Mar 12 '18 at 16:38
  • Getting the following error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewTreeObserver android.view.View.getViewTreeObserver()' on a null object reference – user2782345 Mar 12 '18 at 16:59
  • So it looks like whichever method you are using, they are returning null, which then cannot get the height or width of a null reference. I really can't help that much more without seeing your code. If I were you I would post a new question with regards to getting the View of a Context from a test class. – cjnash Mar 12 '18 at 17:29