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>
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
}
});
Related
I have an image, I want to put a TextView on a certain point(on the brown rectangle) in different devices with different screen sizes. My image is below:
CAUTION:the scaleType of the ImageView is CenterCrop
in image below I show where I want to put the TextView:
And this is my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/startpage"<!-- my background-->
android:scaleType="centerCrop"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#fff"/>
</RelativeLayout>
There's tons of options how to do that, but seeing your pics I'd simply make my TextView aligned to top-left corner and set its layout margin to right values in dp (which I'd simply figure out in any graphics program).
There are several ways. I am quite new so someone with more knowledge would most likely have a better answer. But, you can try to align with Relative Layout. Or you can use a layout gravity to change it so that it "gravitates" towards where you want it to be.
I suggest you to start with also draw 9-patch which is a tool that helps u to select where do u want the content to fit inside the picture.
The Draw 9-patch tool is a WYSIWYG editor that allows you to create
bitmap images that automatically resize to accommodate the contents of
the view and the size of the screen.
After that you will need to check alignment and such stuff. like top left, padding-top... etc
At last, I found it myself, see these java codes:
I find the display width and height, then calculate the scale, the according to the scale I scale the margins.
#Bind(R.id.name_tv)TextView nameTV;
public final static float bgWidth = 768;
public final static float bgHeight = 1136;
public float tVsRightMargin = 123;
public float nameTVtopMargin = 314;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ResourceManager.setActivity(this);
setContentView(R.layout.main);
ButterKnife.bind(this);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int displayWidth = displayMetrics.widthPixels;
int displayHeight = displayMetrics.heightPixels;
Log.v("MainActivity","displayWidth: "+displayWidth);
Log.v("MainActivity", "displayHeight: " + displayHeight);
float scale = Math.max(displayWidth/bgWidth, displayHeight/bgHeight);
Log.v("MainActivity", "scale: "+scale);
float bgScaledWidth = scale*bgWidth;
float bgScaledHeight = scale*bgHeight;
tVsRightMargin *= scale;
nameTVtopMargin *= scale;
if(bgScaledWidth>displayWidth) {
tVsRightMargin -= ((bgScaledWidth - displayWidth) / 2f);
}
if(bgScaledHeight>displayHeight){
nameTVtopMargin -= ((bgScaledHeight-displayHeight)/2f);
}
nameTV.setText("محمد");
nameTV.setTypeface(ResourceManager.getTypeface());
Log.v("MainActivity", "top margin: " + nameTVtopMargin);
Log.v("MainActivity", "right margin: " + tVsRightMargin);
((RelativeLayout.LayoutParams) nameTV.getLayoutParams()).setMargins(0, (int) nameTVtopMargin, (int) tVsRightMargin, 0);
}
The bottom property of below textView scales wrong, the TextView is on a different height on every Android device: See attached pictures.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bars_layout);
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.bar_holder);
BarView view = new BarView(getApplicationContext());
int width = (int) getApplicationContext().getResources().getDimension(R.dimen.bar_width_compare);
int height = 200;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
int left = (int) getApplicationContext().getResources().getDimension(R.dimen.bar_margin_left_right);
int right = 0;
int bottom = (int) getApplicationContext().getResources().getDimension(R.dimen.graph_margin_bar_compare_bottom);
params.setMargins(left, 0, right, bottom);
view.setBackgroundColor(getApplicationContext().getResources().getColor(R.color.bar_dark_blue));
view.setLayoutParams(params);
relativeLayout.addView(view);
TextView textView = new TextView(getApplicationContext());
textView.setText(" 20 ");
textView.setTextSize(20);
textView.setTextColor(getApplicationContext().getResources().getColor(R.color.black_text));
width = (int) getApplicationContext().getResources().getDimension(R.dimen.bar_width);
height = 100;
bottom = (int) getApplicationContext().getResources().getDimension(R.dimen.graph_margin_bar_bottom);
int offset = getApplicationContext().getResources().getDimensionPixelOffset(R.dimen.graph_margin_bar_bottom);
int pxSize = getApplicationContext().getResources().getDimensionPixelSize(R.dimen.graph_margin_bar_bottom);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(width, height);
params2.setMargins(0, 0, 0, bottom);
params2.addRule(Gravity.CENTER_HORIZONTAL);
params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params2.addRule(RelativeLayout.ALIGN_LEFT, view.getId());
textView.setLayoutParams(params2);
relativeLayout.addView(textView);
}
}
bars_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="40dp" >
<RelativeLayout
android:id="#+id/bar_holder"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp" >
</RelativeLayout>
</LinearLayout>
dimens.xml
<resources>
<dimen name="graph_margin_bar_bottom">40dp</dimen>
<dimen name="bar_width_compare">25dp</dimen>
<dimen name="bar_margin_left_right">10dp</dimen>
<dimen name="graph_margin_bar_compare_bottom">50dp</dimen>
<dimen name="bar_width">50dp</dimen>
</resources>
Here are 2 examples of phones, but it looks different on every phone...
Samsung Galaxy Tab 3 Lite (density1.0)
Samsung Galaxy S4 (density3.0)
Android groups all actual screen sizes into four generalized sizes: small, normal, large, and extra-large.
Create three floder for "layout" for normal,small screen
layout-large for large screen
layout-xlarge for extra-large screen
Add same bars_layout.xml into all floder and increase size as per screen size(For example if your keep size in layout floder 10 dp then for layout-large increase that size to 30 dp and for layout-xlarge make 60dp)
For dimens.xml small and normal screen add it to "values" floder
For large screen add to values-large floder
For extra-large add to values-xlarge
setMargins(int,int,int,int) is using pixels, not DIP, so on every device same size in pixels looks different, you need to convert DIP into pixels and then setMargins() with that value
multiply text height with densityDpi and textsize with scaledDensity values. So it will support different resolutions.
DisplayMetrics dm = context.getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
float spi = dm.scaledDensity;
textheight = 100 * densityDpi;
textSize = 20 * spi;
I have a layout with a background image. The image width should be the same as the screen width (match_parent). But it's ugly, the OS don't stretch the picture in height.
I heard about ImageView scaleType, so I have the background image in an ImageView instead of a layout background, but I can't config to be what I want.
How can I set to the layout or ImageView to scale the image (in width) to fit the width of the screen AND scale the height with the same scale?
I want the second screen in the picture:
UPDATE:
I'm trying from code, but I get 0 width + I can't believe this can't be done from xml with an ImageView.
Drawable imageDrawable = getResources().getDrawable(imageResource);
contentContainer.setBackgroundDrawable(imageDrawable);
Rect rect = imageDrawable.getBounds();
int originalWidth = rect.width();
int originalHeight = rect.height();
Display display = getActivity().getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = originalHeight * (width/originalWidth);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
contentContainer.setLayoutParams(layoutParams);
contentContainer.post(new Runnable() {
#Override
public void run() {
contentContainer.setScaleY(contentContainer.getScaleX());
Rect rect = contentContainer.getBackground().getBounds();
int originalWidth = rect.width();
int originalHeight = rect.height();
Display display = getActivity().getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = originalHeight * (width/originalWidth);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
contentContainer.setLayoutParams(layoutParams);
}
}
Use FrameLayout as parent and then add imageview as child.
Give the width match_parent and height fill_parent to the imageView and another ways is apply the android:layout_weight attribute try this may be it's work.
you can do this at runtime by measuring the width of the device like this:--
super.onCreate(savedInstanceState);
setContentView(<layout-resoure-id);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int imageSize = 0;
if (width > height)
imageSize = height;
else
imageSize = width;
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.getLayoutParams().width = imageSize;
imageView.getLayoutParams().height = imageSize;
imageView.setImageResource(R.drawable.pattu);
your xml:--
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"/>
</LinearLayout>
Enjoy...!
Have you tried playing with scaleType in you layout file?
<ImageView
...
android:scaleType="fitStart"
/>
This should scale your image to fit at the top of your layout, stretching or shrinking if necessary. Haven't had a chance to test (and I can't remember the height/width settings--sorry!), but this could be a solution.
ImageView imageView = (ImageView) findViewById(R.id.your_image_id);
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
int imageSize = height>width?width:height;
your configurations go here !
imageView.getLayoutParams().width = imageSize;
imageView.getLayoutParams().height = imageSize + (/* additional height logic*/);
you should put the imageView scaletype as fitXY and do this to stretch the image like the way you want!
<ImageView
...
android:scaleType="fitXY"
/>
I know I'm supposed to define different layouts for different screens, please read on.
I'm working on the app that will be run an "Android on the stick" plugged to either 720p or 1080p TV and left alone to show some "slide show".
I need something like android:layout_width="n%". I know I can do it with LinearLayout, but the screen is rather complex and such deep nesting is discouraged. I would like to stick to RelativeLayout.
I have designed the layout specifying the metrics in pixels, designing for 1080p and put the layout to layout-tvdpi or layout-xhdpi hoping Android would scale it properly down to 720p. It does not. All artefacts are overblown and overlap.
Is there a way to achieve what I want? Thanks.
As I mentioned in the question, the app I'm working on will be displayed on either 720p or 1080p TV. This allows me to commit the following blasphemy: All view sizes/paddings/margins and text sizes are defined in PX, not DP, not SP. This way I can control the exact screen layout.
Now, contrary to what the documentation made me to believe, Android will not resize any layout element that has size defined in PX. It will only do it for drawables or DP/SP.
Thus I had to resize on my own.
I define the layouts for 1080p. in Activity.onCreate() get the root layout and pass it to:
private static final float RATIO_720_1080 = 720f/1080f;
/**
* If the screen is 720p, the resources will resize
* to fit the screen.
* #param root Root view where the resources are found.
*/
public void resizeViews(ViewGroup root) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
// only resize if 720p
if (metrics.widthPixels != 1280) return;
int childCount = root.getChildCount();
for (int i = 0; i < childCount; i++) {
View v = root.getChildAt(i);
// digg deep
if (v instanceof ViewGroup) {
resizeViews((ViewGroup)v);
}
// do the size
ViewGroup.LayoutParams params = v.getLayoutParams();
// keep the match_parent constants intact
if (params.height > 0) {
params.height = Math.round(params.height * RATIO_720_1080);
}
if (params.width > 0) {
params.width = Math.round(params.width * RATIO_720_1080);
}
if (params instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) params;
marginParams.setMargins(Math.round(marginParams.leftMargin * RATIO_720_1080),
Math.round(marginParams.topMargin * RATIO_720_1080),
Math.round(marginParams.rightMargin * RATIO_720_1080),
Math.round(marginParams.bottomMargin * RATIO_720_1080));
}
v.setLayoutParams(params);
v.setPadding(Math.round(v.getPaddingLeft() * RATIO_720_1080),
Math.round(v.getPaddingTop() * RATIO_720_1080),
Math.round(v.getPaddingRight() * RATIO_720_1080),
Math.round(v.getPaddingBottom() * RATIO_720_1080));
// text size
if (v instanceof TextView) {
float size = ((TextView)v).getTextSize();
size *= RATIO_720_1080;
((TextView)v).setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
}
}
You can use a layout file like below. Notice the layout_sum attribute in the parent layout, and the layout_weight attribute in each child. The first child will occupy 70% of the parent, and the second child 30%.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="70"
android:text="A"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:text="B"/>
</LinearLayout>
I want to reset a textView height after I have added it to the main window in the xml file.
inside a RelativeLayout,
<TextView
android:id="#+id/text_l"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10sp"
android:layout_marginTop="145dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" >
</TextView>
I just want to change it from 50 to 70:
I tried:
TextView text = (TextView)findViewById(R.id.text_l);
text.setHeight(70);
but nothing changed.
You should change it via LayoutParams:
LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
textView.setLayoutParams(params);
EDIT
You should not use sizes in pixels in you code, use dimensions for this:
dimens.xml:
<dimen name="text_view_height">50dp</dimen>
In code:
params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);
Pragmatically you can set textview height like:
private TextView mTxtView;
int height = 50; //your textview height
mTxtView.getLayoutParams().height = height;
you can dynamically set width and height of textview by
private TextView mTxtView;
private int screenWidth, screenHeight;
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
LayoutParams params = mTxtView.getLayoutParams();
params.width = screenWidth-30;
mTxtView.setLayoutParams(params);
In Kotlin with DP to pixels translation
changeTextHeight.setOnClickListener { view ->
// random height for testing
val randomHeightInDP = Random().nextFloat() * (50.0f - 10.0f) + 10
// set Height in pixels
hello.layoutParams.height = convertDpToPixel(randomHeightInDP, applicationContext)
//refresh layout
hello.requestLayout()
}
Convert DP to pixels, see this post:
fun convertDpToPixel(dp: Float, context: Context): Int {
return (dp * (context.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)).toInt()
}
the sample way for this if you want to use dimen
first, you should set size in dimen XML file.
<dimen name="text_height">50dp</dimen>
<dimen name="text_width">50dp</dimen>
and
text_l.getLayoutParams().height =
getResources().getDimensionPixelSize(R.dimen.text_height);
text_l.getLayoutParams().width =
getResources().getDimensionPixelSize(R.dimen.text_width);
Or
if you want to set just int (for example we wanna set 50dp height and 100dp width)
text_l.getLayoutParams().height = 50 * 3;
text_l.getLayoutParams().width= 100 * 3;
I know this is an old question, but for the sake of others who might find this, there are scenarios where you should call textView.requestLayout() after changing the layout parameters. While it my be fine to omit it if you are simply changing the layout parameters as a one time thing before the layout is drawn. In my case, I wanted to change the height parameter of a TextView based on a radio button selection using onCheckedChangedListener, but the TextView height would only update the first time it was drawn. Adding requestLayout() solved this problem.
TextView tv;
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
if(!tv.isInLayout()) {//make sure it isn't currently in a layout pass
tv.requestLayout();//request a layout pass
}