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 8880593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:11:34+00:00 2026-06-14T20:11:34+00:00

I have identified the width and height of the screen programatically. Now I want

  • 0

I have identified the width and height of the screen programatically. Now I want to use that width and height in my xml file. Kindly give me a way to achive this.

Here is my Activity class.

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DisplayMetrics display = this.getResources().getDisplayMetrics(); 
        float width = display.widthPixels;
        float height = display.heightPixels;
        Toast.makeText(TestProjectActivity.this, "width and height is: "+width + ": "+height, Toast.LENGTH_LONG).show();

        float w = convertPixelsToDp(width, this);
        float h = convertPixelsToDp(height, this);

        Toast.makeText(TestProjectActivity.this, "DP" +w+":  "+h, Toast.LENGTH_LONG).show();
    }

    public static float convertPixelsToDp(float px,Context context){
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float dp = px / (metrics.densityDpi / 160f);
        return dp;
    }

Now I want to send the w and b to the xml for the further work.

Any help would be appricieted?
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-14T20:11:35+00:00Added an answer on June 14, 2026 at 8:11 pm

    You cannot pass information back to XML. At most, you can get your widget using findViewById and then programmatically set the widget size.

    I am developing the application which runs on any screen device. So the issue with the screen size. If I am setting the width of textview to 200dp, then its ok in my device but on other device,its not getting appear. So I just want a way where I can use this above detected width and height in my XML

    You should then use a LinearLayout to give your image a size relative to the available width (like a percentage of the width of the screen

    Here is how your XML would look (pseudo code)

    LinearLayout (layout_width: match_parent, layout_height: wrap_content)
      ImageView (layout_width: 0dip, layout_height: wrap_content, layout_weight: 50)
      ImageView (layout_width: 0dip, layout_height: wrap_content, layout_weight: 50)
    

    In the above example, each image view will take 50% of the screen width, no matter the number of pixels.

    I think you need to Google a good tutorial about layouts in Android.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have identified the query constructs my users normally use. Would it make sense
We have a binary file from which we have identified the following dates (as
I want to use the scroll view inside the tableviewcell. I have written the
I have a UserControl that I want to participate in data binding. I've set
I have some particles that I've identified in a larger image, and need to
I have a project in Eclipse that I use as a library (I export
I have an activity that sets xml by calling the below xml. This xml
i have a code that i want to modify to get a zoom ability.
I have a fixed-width flat file. To make matters worse, each line can either
I have this markup <div id=line width=100%; height: 44px;> <div id=identifier style=position: absolute;left:0px width:

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.