Hei, I have a hor scrollview which holds a LinearLayout (orientation: hor).
<HorizontalScrollView
android:id="#+id/adImageScroller"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/adImageViewContainer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="left"
android:background="#color/material_blue_grey_950">
</LinearLayout>
</HorizontalScrollView>
I am populating this LinearLayout dynamically with ImageViews (all images have random aspect ratios).
This is the onCreateView method which populates the LinearLayout:
for (int i = 0; i < reObjectPicUrls.size(); i++) {
String url = reObjectPicUrls.get(i).getContent();
ImageView imageView = new ImageView(getActivity());
imageView.setId(i);
imageView.setPadding(2, 2, 2, 2);
new DownloadImageTask(imageView)
.execute(url);
imageView.setScaleType(ImageView.ScaleType.FIT_START); //<-- WORKS IN XML
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT)); //<-- WORKS IN XML
imageViewContainer.addView(imageView);
}
What I need but don't get is all images to be scaled to the height of the grey area (horizontalScrollView height) while sustaining their aspect ratio.
The images are all scaled down with options.inSampleSize = 4;
Problem is in the image below. The images are not scaled to fit the height of the LinearLayout.
I have tried different scaleType but nothing seems to fix it.
Thanks for any tips on how to fix this.
Finally cracked it.
Didn't have to change the layout xml but did alter the onCreateView code and added setAdjustViewBounds(true) also used FIT_CENTER scale type.
for (int i = 0; i < reObjectPicUrls.size(); i++) {
String url = reObjectPicUrls.get(i).getContent();
ImageView imageView = new ImageView(getActivity());
imageView.setId(i);
imageView.setPadding(2, 2, 2, 2);
new DownloadImageTask(imageView)
.execute(url);
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));
imageView.setAdjustViewBounds(true); <--- THIS DID THE TRICK
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageViewContainer.addView(imageView);
}
Related
I am facing issue when setting the layout_margin programatically for the dynamically created ImageView inside a GridLayout.
I have tried the below code :
GridLayout gridLayout = (GridLayout) findViewById(R.id.idGridLayout);
gridLayout.setColumnCount(10);
gridLayout.setRowCount(10);
for (int i = 1; i <= 100; i++) {
ImageView tile1 = new ImageView();
LayoutParams lp = new LayoutParams(100, 100);
tile1.setLayoutParams(lp);
lp.setMargins(4, 4, 4, 4); ////not working some error
tile1.setBackgroundColor(Color.rgb(255, 255, 5));
//tile1.setBackgroundResource(R.drawable.aalien);
//tile1.setBackgroundColor();
I have found a method setMargins(4, 4, 4, 4), but by this I can set the margin only for a ViewGroups like LinearLayout/GridLayout/RelativeLayout etc.
But I want to set margin for the ImageView like I do in the xml by using the android:layout_margin="5dp"
Try doing this for each ImageView
ImageView imageView = new ImageView(context);
GridLayout.LayoutParams params = (GridLayout.LayoutParams) imageView.getLayoutParams();
params.setMargins(4, 4, 4, 4); // left, top, right, bottom
imageView.setLayoutParams(params);
I want to place a ImageView at top left corner when runtime, but it does not work.
This is my layout and code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:id="#+id/layout_main"
android:layout_height="fill_parent" tools:context=".Main">
</RelativeLayout>
This is code:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout_main);
ImageView imageView =new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.green_apple);
imageView.setScaleX(0.3f);
imageView.setScaleY(0.3f);
imageView.setLeft(0);
imageView.setTop(0);
layout.addView(imageView);
I also tried:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout_main);
ImageView imageView =new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.green_apple);
imageView.setScaleX(0.3f);
imageView.setScaleY(0.3f);
imageView.setX(0);
imageView.setY(0);
layout.addView(imageView);
And:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout_main);
ImageView imageView =new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.green_apple);
imageView.setScaleX(0.3f);
imageView.setScaleY(0.3f);
layout.addView(imageView);
set = new AnimatorSet();
ObjectAnimator aniX = ObjectAnimator.ofFloat(imageView, "x", 0);
aniX.setDuration(100);
ObjectAnimator aniY = ObjectAnimator.ofFloat(imageView, "y", 0);
aniY.setDuration(100);
set.playTogether(aniX, aniY);
set.start()
But same result, this is my result:
Always have large space to Top and Left screen, Although I set 0,0. I also try set height and width of screen, it fly out of screen. I don't know why.
Thank you very much for anyone can explain and how to help me fix it
That is because you use setScaleX and setScaleY, so try the code below:
final ImageView iv = new ImageView(context);
iv.setImageResource(R.drawable.green_apple);
rl.addView(iv);
iv.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
#Override
public boolean onPreDraw() {
iv.getViewTreeObserver().removeOnPreDrawListener(this);
int w = (int) (iv.getWidth() * 0.3);
int h = (int) (iv.getHeight() * 0.3);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(w, h);
iv.setLayoutParams(rlp);
return true;
}
});
marginBottom not working. It works but the margin is only a few space regardless of how much margin is applied. Why?
Java-code :
LinearLayout onepage= (LinearLayout) findViewById(R.id.onepage);
RelativeLayout bluegreen = new RelativeLayout(this);
p_ll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
bluegreen.setLayoutParams(p_ll);
//some other views that make up the whole page
//bottom most image
ImageButton migs = new ImageButton(this);
p_rl = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p_rl.addRule(RelativeLayout.CENTER_HORIZONTAL);
p_rl.setMargins(0, 0, 0, 20);
register.setLayoutParams(p_rl);
register.setImageResource(R.drawable.migs);
register.setPadding(0, 0, 0, 0);
bluegreen.addView(migs);
onepage.addView(bluegreen);
XML:
<LinearLayout
android:id="#+id/onepage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".PageActivity" >
</LinearLayout>
Have you notice that in setMargins method parameters are in pixel and not in dp? take a look at android doc page
So depending of your device screen size but 20 px can be very small.
Don't forget to call requestLayout() to take in account margin changes.
I'm using a TableLayout to create a square gameboard in Android. I want to set the dp of the table's width and height, and have its contents automatically expand and contract to fit the table.
I got this working with the entries in the tableRows as LinearLayouts with a single ImageView inside, but I need to change what images are displayed with click actions, so I thought I would use a ViewFlipper.
Now that I'm using the ViewFlipper, though, my image sizes have gone all wonky. Does anyone know if there's a setting I can set on ViewFlipper to get the images to auto adjust their size in a table?
Here's my xml:
<TableLayout
android:id="#+id/gameBoard"
android:layout_width="#dimen/boardWidth"
android:layout_height="#dimen/boardWidth"
android:layout_margin="0dp"
android:padding="0dp"
android:shrinkColumns="*"
android:stretchColumns="*" >
</TableLayout>
And here's the code I'm programmatically creating the table with:
TableLayout gameBoard = (TableLayout) findViewById(R.id.gameBoard);
TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
rowParams.setMargins(0, 0, 0, 0);
for (int y = settings.getBoardSide() - 1; y > -1; y--) {
TableRow row = new TableRow(this);
row.setPadding(0, 0, 0, 0);
for (int x = 0; x < settings.getBoardSide(); x++) {
ViewFlipper flipper = new ViewFlipper(this);
flipper.setOnClickListener(new TileClick());
//default image
ImageView cloud = new ImageView(this);
cloud.setImageResource(R.drawable.transparent_cloud);
cloud.setAdjustViewBounds(true);
flipper.addView(cloud, 0);
//second image
ImageView landscape = new ImageView(this);
landscape.setImageResource(R.drawable.land_scape);
landscape.setAdjustViewBounds(true);
flipper.addView(landscape, 1);
row.addView(flipper);
}
gameBoard.addView(row, rowParams);
}
It turned out that for my purpose a FrameLayout with its width and height set to a specific dp was best. That way I can control the view and ensure it has the right height and width. With the FrameLayout I can add and remove views at will, and layer different views on top of each other as well.
Here's the thing: I want to add some images programmatically. The images should have to have a topMargin of 5dip except for the first image, in a LinearLayout with a vertical orientation manner. below the code segment:
LinearLayout body = (LinearLayout) findViewById(R.id.body);
for (int i = 1; i <= 4; i++) {
ImageView img = new ImageView(this);
MarginLayoutParams lp = new MarginLayoutParams(-2, -2);
img.setImageResource(R.drawable.image);
if (i != 1) {
lp.setMargins(0, 5, 0, 0);
}
img.setLayoutParams(lp);
body.addView(img);
body.requestLayout();
}
By running the program I can see the the 4 images(here) vertically aligned one by one but there is no topMargin(as in the code,5dip). body is the id of the LinearLayout. here's the XML segment to:
<LinearLayout
android:id="#+id/body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#901b0e08"
android:orientation="vertical"
android:paddingLeft="6dp"
android:paddingRight="8dp" >
</LinearLayout>
I cant get what went wrong here.
Thanks.
Try changing your MarginLayoutParams to this:
LayoutParams lp = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
The reason for doing this, is that body is a LinearLayout and thus you would want to use the LinearLayout-specific LayoutParams.
Try to set padding but margin for ImageView. Sometimes it works fine. Your can directly set padding size to ImageView w/o declare LayoutParams.
You probably need to set LayoutParams' gravity property, for example:
lp.gravity = 0; //AXIS_X_SHIFT
That's what works fine for me.