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>
You get the width of the screen in pixels:
and for the width of the items you get the width in pixels but you also transform it into
dp:And then you try to use those two values together.
Either use
pixelsordp.Edit 2:
You should really read about the
inflate()methods of theLayoutInflaterclass. You need to inflate the views with the properLayoutParamsand this is done by providing theViewGroupthat will be the parent of the inflated layout file. So you need to do: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:
Edit 1:
Your current code will not work and you didn’t understand my answer.
dm.widthPixelsreturns the screen width(which yourHorizontalScrollViewfills) 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 properLayoutParamsto your views. You should inflate the child layout like this:Also, I would just use the code below(used in the
onCreatemethod) to find out the visible children on the screen(completely visible or partial visible):