I want to put 8 image thumbs in one horizontal line, using the whole available width.
The images are retrieved from a webservice which lets me specify the dimensions.
I tried the following:
int widthPx = container.getWidth();
LinearLayout thumbs = (LinearLayout)curView.findViewById(R.id.thumbs);
for(int i=0; i<pics.length; i++) {
ImageView iv = new ImageView(mContextt);
int thumbSize = widthPx / 8;
try {
String url = "http://someurl/" + pics[i] + "&width=" + thumbSize + "&height=" + thumbSize;
URL imgUrl = new URL(url);
Drawable imgD = Drawable.createFromStream(imgUrl.openStream(), "src");
iv.setImageDrawable(imgD);
}
catch (Exception e) {
Log.e(TAG, "loading image failed");
e.printStackTrace();
}
thumbs.addView(iv);
}
The LinearLayout thumbs has android:layout_width="fill_parent set. The thumbs produced by this code are significantly smaller then 1/8 of the width. Why is this? What would be the correct way?
Update:
This code is inside onCreateView of a Fragment. While my width value is calculated based on the root-view and is correct, thumbs.getWidth() returns 0, although the view is inflated before and should also have a width of 480 because of layout_width is set to fill_parent. I'm not sure if that's a problem.
Is my assumption correct that the layout of the created ImageViews is set to wrap_content by default? If not, how to set this with Java code?
Add the ImageViews with a fixed size, i.e.:
thumbs.addView (iv, new LayoutParams(thumbSize, thumbSize));
To answer (partially) the questions in the comments:
The ImageView API says:
takes care of computing its measurement from the image so that it can be used in any layout manager
so it is probably assuming 60px for a 160 dpi (I may be wrong there).
I'd suggest using this:
widthPx = getWindowManager().getDefaultDisplay().getWidth();
Related
This is my code to create dynamic imageviews. Image get aligned really well but I need to make it small. How can I scale the image?
This is a in a LinearLayout:
for(int i=0;i<value1;i++) {
ImageView image = new ImageView(Home.this);
image.setImageResource(R.drawable.bulb);
image.setScaleX(1);
image.setScaleY(1);
image.setScaleType(ImageView.ScaleType.MATRIX);
rR.addView(image);
}
This is my code to create dynamic imageviews . Image get aligned really well but i need to make it small. How can i scale the image?
Please set the proper height and width of your dynamic image view.
Please try below code it's helpful to you.
for (int i = 0; i < 3; i++)
{
ImageView image = new ImageView(ImageScalling.this);
image.setImageResource(R.drawable.ic_launcher);
image.setLayoutParams(new LinearLayout.LayoutParams(200, 200));
image.setScaleType(ImageView.ScaleType.FIT_XY);
rR.addView(image);
}
I have set width and height is 200. If you want the show a small image then set less than 200.
For scaling imageview try this link
https://argillander.wordpress.com/2011/11/24/scale-image-into-imageview-then-resize-imageview-to-match-the-image/
You can set the size of Imageview according to screen size like 20% of height .
Display display = getWindowManager().getDefaultDisplay();
int height = display.getHeight();
int width = display.getWidth();
float mReqPercentages =20.0;
float mCalculatedWidth = (mReqPercentages / 100) * height;
image.getLayoutParams().height = (int) (mCalculatedWidth * 1.5);
image.getLayoutParams().width = (int) mCalculatedWidth;
I have an array of strings(dynamic). I have to show that strings in a textview. The size of the string may vary. The first string is "check", the second String is "my head is breaking" and so on. I want the textview to adjust itself. Say for ex : If the first textview occupied less than half of the screen, The second textview will be next to it. Else, The 2nd Textview will be placed at the next row.
Please check the following image.
How can i achieve like this?
I'm not native speaker.
In order to achieve your purpose you need to follow these steps:
1. Find the dimensions of your device: I did this some time ago, here you have my implementation.
protected void loadDeviceDimensions(){
Point size = new Point();//Calcular Width y Height.
WindowManager w = getWindowManager();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
{
w.getDefaultDisplay().getSize(size);
DeviceWidth = size.x;
DeviceHeight = size.y;
}
else
{
Display d = w.getDefaultDisplay();
DeviceWidth = d.getWidth();
DeviceHeight = d.getHeight();
}
}
Once you have the device dimensions the next step is create the text fields, the text, font, and all the other stuffs. Don't worry about the position, just create the fields
Once you have your fields created with their text and font set, its time to set their position. For these purpose you will need to use the TextView.getWidth() and TextView.getHeight(). The code should look like this.
for(int i=0, i < textView-1 , i++) {
if( textViews[i].getWidth <= deviceWidth/2 && textViews[i+1].getWidth <= deviceWidth/2){
//Put the two text fields on the same line
i++;//Don't forget this line.
}else{
//Put only one text field on the line.
}
}
You can check your Display size, and you can check your size the TextView will have. then it should not be a problem, otherwise let me know.
display size
textview size without to draw first
I make something like tab.
I have HorizontalScrollView with LinearLayout and few View in it.
I want to change width of my View depending on screen size (ScrollView or LinearLayout size)
to do this:
if (view1.size+view2.size + ... + viewn.size < scrollView.size)
{
view1.setSize(scrollView.size/n);
view2.setSize(scrollView.size/n);
...
viewn.setSize(scrollView.size/n);
}
I try set something in onMeasure, but all decisions were wrong.
ScrollView's size is not determinate and actually depends on the size of the children.
If you want to use the size of the screen and how many children you want to show on the screen which makes more sense, something like the following might work (not tested):
int children_on_screen = 3;
int size = getWidthOfTheScreen();
for(int i=0;i<yourLinearLayout.getChildCount();i++) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) yourLinearLayout.getChildAt(i).getLayoutParams();
params.width = size / children_on_screen;
}
yourLinearLayout.requestLayout();
I have the problem: when you try to add ImageView the LinearLayout, some images are not loaded and the program stops with a black screen. Picture JPG or PNG there is no difference. Tried to change the size of 100x100px to 1080x1920. If I replace the picture on another then everything is fine, but I need this picture. I put the exception to this code, but in the logcat nothing, i.e. the exception does not occur.
Please help me. Thank you.
for (int i = 0, lenI = anims.length; i < lenI; i++ ) {
try {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
//add image
ImageView imageView = new ImageView(this);
linearLayout.addView(imageView);
int resId = getResources().getIdentifier("ru.romston.testprog:drawable/"+pics[i], null, null);
imageView.setImageResource(resId);
imageView.setMaxHeight(100);
imageView.setMaxWidth(100);
imageView.setPadding(5,5,5,5);
imageView.setAdjustViewBounds(true);
//label
TextView textView = new TextView(this);
linearLayout.addView(textView);
textView.setText(anims[i]);
layout.addView(linearLayout);
} catch (Exception e) {
Log.e(TAG, "read data: error!" + e.getMessage(), e);
}
}
I think the image is of higher resolution/size that Android rendering system fails to scale it into height and width of 100. Try with same image after changing the image size/resolution.
Please correct if wrong.
ImageViews have a resolution limit based on OpenGL. If the bitmap or image passes this limit resolution, it shows a black screen and throws no error.
You can retrieve this limit by querying OpenGL:
int[] maxSize = new int[1];
GLES10.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxSize, 0);
See this answer for more details
I am programatically adding ImageView elements into a horizontal Linear Layout. Then I set the scaleX and scaleY properties to "2" on one of the ImageView resources. The image gets scaled properly, but it doesn't move the other ImageView elements, instead of that it overlaps them. I don't like the image to overlap with other images. How can I fix that? Here's my code:
int resources[] = {R.drawable.desert, R.drawable.koala, R.drawable.jellyfish,
R.drawable.lighthouse, R.drawable.desert};
for(int i=0; i<5; i++) {
ImageView logo = new ImageView(this);
logo.setLayoutParams(new LinearLayout.LayoutParams(100, 75));
logo.setImageResource(resources[i]);
logosContainer.addView(logo);
}
ImageView middleImage = (ImageView) logosContainer.getChildAt(2);
middleImage.setScaleX(middleImage.getScaleX() * 2);
middleImage.setScaleY(middleImage.getScaleY() * 2);
The result from the code looks like this:
http://imageshack.us/a/img15/2811/scaleal.jpg
You can clearly see that the scaled image overlaps with the other images.
Your ImageViews are already measured and placed in the container when you leave the loop. You would have to refresh your layout to make this work, which is tricky and often leads into "removing all views and adding them again". So why don't you just do the "scaling" in the loop?
for(int i=0; i<5; i++) {
ImageView logo = new ImageView(this);
if(i==middleImageIndex){
logo.setLayoutParams(new LinearLayout.LayoutParams(100*2, 75*2,1));
} else {
logo.setLayoutParams(new LinearLayout.LayoutParams(100, 75,1));
}
logo.setImageResource(resources[i]);
logosContainer.addView(logo);
}
The last parameter (100,75, 1) is the weight of the view, which makes sure that each ImageView is equally important and doesn't get overlapped.
Another note: setScale requires API level 11 or higher, which could cut a lot of users out there