Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9012925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:07:22+00:00 2026-06-16T03:07:22+00:00

The answer to this question may be too advanced for me, but I would

  • 0

The answer to this question may be too advanced for me, but I would still like to give it a try. I am able to determine and set the value for the screen size within a method, but it does not have scope outside of that method. My question is, how do I give it scope inside the entire class? Also inside the entire project? Thanks. Here is what I am using to determine the screen size, but it only works inside a single method:

    DisplayMetrics metrics = this.getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:07:23+00:00Added an answer on June 16, 2026 at 3:07 am

    My question is, how do I give it scope inside the entire class? Also inside the entire project?

    If you only want to worry about setting those values once, extending the Application class would probably be a good start.

    public class MyApplication extends Application {
        private Point mDimensions;
    
        @Override public void onCreate() {
            // this will only get run on app startup
            DisplayMetrics metrics = this.getResources().getDisplayMetrics();
            int width = metrics.widthPixels;
            int height = metrics.heightPixels;
            mDimensions = new Point(width, height);
        }
    
        public int getWidth() {
            return mDimensions.x;
        }
    
        public int getHeight() {
            return mDimensions.y;
        }
    }
    

    To access the getter methods, set the android:name attribute on the application tag in your project’s manifest:

    <application android:name="com.myname.MyApplication" ... ></application>
    

    You can then cast the result of any getApplication() and getApplicationContext() call to MyApplication and call the methods; e.g.

    int width = ((MyApplication) getApplication()).getWidth(); 
    

    Do note that since the width and height values are initialized only once, they will not change on screen orientation changes. If you want to take that into account too, a simple util method accepting a context instance (can be e.g. an Activity or Service) is probably a more straightforward solution.

    public static Point getDimensions(Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        return new Point(width, height);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question may sound too silly, however , I don't find concrete answer any
This question may have been asked before, but I had trouble finding an answer,
This may be a simple question but I can;t find the answer anywhere. Here
I know this is a total newbie question, but the answer may not be
My question may be too easy, but I have not found the answer, sorry
This may be a stupid question but I just can't find the answer. What
This seems like a simple question, but I am having trouble finding the answer.
this may be a stupid question as I can't seem to find an answer
This maybe too basic but I couldn't find an exact answer. I'll be happy
This question was asked by someone else, but no decent answer was given. Let

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.