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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:28:26+00:00 2026-06-17T03:28:26+00:00

I want to calculate how much views are displayed on screen at a time

  • 0

I want to calculate how much views are displayed on screen at a time if view width is fixed. For that I get one Layout add some views in it with fixed size and run it.

But as per my calculation I get wrong number of child to displayed on screen as it shows on screen.

Please tell me where I am wrong?

Here is my code…

   In Activity ...
    ----
     LinearLayout featured_listlayout_horizontallayout=(LinearLayout)findViewById(R.id.featured_listlayout_horizontallayout);
            LayoutInflater inflater=LayoutInflater.from(getApplicationContext());
            for(int i=0;i<20;i++){
                LinearLayout childItem=(LinearLayout)inflater.inflate(R.layout.childitemlayout,null);
                Button btn=(Button)childItem.findViewById(R.id.btn);
                btn.setText("Item"+(i+1));
                featured_listlayout_horizontallayout.addView(childItem);
            }

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    final int height = dm.heightPixels;
    float screenWidth = dm.widthPixels;//Screen Width in pixel

    float itemWidth=getResources().getDimension(R.dimen.featured_text);//itemWidth in DP
    itemWidth=convertDpToPixel(itemWidth, getApplicationContext());// convert itemWidth into pixel

    System.out.println("screenWidth "+screenWidth+" itemWidth "+itemWidth);

    float noOfItem=screenWidth/itemWidth;
    System.out.println("noOfItem "+noOfItem);

    -----

    convertPixelsToDp method:


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

  convertDpToPixel method:

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

    activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/featured_listlayout_horizontallayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="5dp" >
            </LinearLayout>
        </HorizontalScrollView>

    </RelativeLayout>


    childitemlayout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="@dimen/featured_text"
        android:layout_height="@dimen/featured_image"
        android:orientation="vertical" 
        android:background="#0000ff">

        <Button android:id="@+id/btn"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Button"
                android:background="#ff00ff"/>


    </LinearLayout>



    dimen.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
             <dimen name="featured_text">80dp</dimen>
             <dimen name="featured_image">60dp</dimen>
    </resources>
  • 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-17T03:28:27+00:00Added an answer on June 17, 2026 at 3:28 am

    But as per my calculation I get wrong number of child to displayed on
    screen as it shows on screen.

    You get the width of the screen in pixels:

    float screenWidth = dm.widthPixels;
    

    and for the width of the items you get the width in pixels but you also transform it into dp:

    float itemWidth=getResources().getDimension(R.dimen.featured_text);
    itemWidth=convertPixelsToDp(itemWidth, getApplicationContext());
    

    And then you try to use those two values together.

    Either use pixels or dp.

    Edit 2:

    You should really read about the inflate() methods of the LayoutInflater class. You need to inflate the views with the proper LayoutParams and this is done by providing the ViewGroup that will be the parent of the inflated layout file. So you need to do:

    LinearLayout childItem = (LinearLayout) inflater.inflate(
                        R.layout.aaaaaaaaaaaa, featured_listlayout_horizontallayout, false);
    

    If you don’t do this, your inflated views will not have the set size on them(as you have probably seen they will wrap their content). After you’ve done the modification above and you want to find out the number of children that wopuld be visible(completely or partially) before you actually add them in the layout(using the dimension you set in the xml layout) you just divide the screen width to the dimension from the resources:

    int screenWidth = dm.widthPixels;
    int itemWidth=getResources().getDimension(R.dimen.featured_text);
    int noOfItem=screenWidth/itemWidth;
    // if we have a remainder from the division it means there is extra space
    // so we need to check if we have more items so there is another child partially showing
    int rem = screenWidth % itemWidth;
    if (rem != 0) {
        noOfItem += 1;
    }
    

    Edit 1:

    Your current code will not work and you didn’t understand my answer. dm.widthPixels returns the screen width(which your HorizontalScrollView fills) in pixels. getResources().getDimension(R.dimen.featured_text) will return the value of that dimension resource in pixels. There is no need for methods to transform those values, just use them directly. Even so your code will not work right as you wanted because you don’t take care of assigning the proper LayoutParams to your views. You should inflate the child layout like this:

    LinearLayout childItem = (LinearLayout) inflater.inflate(
                        R.layout.aaaaaaaaaaaa, featured_listlayout_horizontallayout, false);
    

    Also, I would just use the code below(used in the onCreate method) to find out the visible children on the screen(completely visible or partial visible):

    featured_listlayout_horizontallayout.post(new Runnable() {
    
            @Override
            public void run() {
                DisplayMetrics dm = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm);
                // screen width
                final float screenWidth = dm.widthPixels;
                final int count = featured_listlayout_horizontallayout
                        .getChildCount();
                int realWidth = 0;
                int visibleChildren = 0;
                for (int i = 0; i < count; i++) {
                    final View child = featured_listlayout_horizontallayout
                            .getChildAt(i);
                    realWidth += child.getWidth();
                    if (realWidth < screenWidth) {
                        visibleChildren++;
                    } else {
                        visibleChildren++;
                        break;
                    }
                }
                // visibleChildren now has the visible children on the screen
            }
    
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

basically i have one time currentTime = DateFormat.getTimeInstance().format(new Date()); and i want to calculate
What I want is to calculate how much time the caret will move from
I am developing one web application in that I want to check load time
I want to calculate how much time elapsed between a datetime and now in
I want to calculate distance in my custom repository class. The problem is that
I want to calculate the smallest integer with exactly k bits set, that is
I want to calculate the execution time of my ASP.NET pages and display it
I want to calculate the difference between two times, one of which is the
I want to run a analyse for my website , calculate how much traffic
I've been busy with some AJAX code. I want to calculate how much data

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.