0

I want to put some images in an area at the application start. As the number of images is not a fixed amount, so I have to create images dynamically. I want to set the position/margin of every images while they're created for good balance.

I have tried follows, but there is no efficacy. ・After Created, use imageview.layout(). ・Use LayoutParams.margins(). ・Use AsyncTask to set margins. ・activity.runOnUiThread().

This is the code:

    // After access server, if needed then add a imageview to the 'c_box'
    // c_box: the parent that imageview to add.
    FrameLayout c_box = (FrameLayout) findViewById(R.id.c_box);

    MarginLayoutParams mlp = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    ImageView img1 = new ImageView(this);
    img1.setImageResource(R.drawable.c_file);
    img1.setLayoutParams(params);
    img1.setLongClickable(true);
    img1.setId(img_id);
    c_box.addView(img1);

    img1.setOnTouchListener(listener);


    **// it's possible to set the position by layout or margin?
    img1.layout(100, 100, 200, 200);**

I don't know where to call the invalidate() method.


// After access server, if needed then add a imageview to the 'c_box'
    // c_box: the parent that imageview to add.
    FrameLayout c_box = (FrameLayout) findViewById(R.id.c_box);

    MarginLayoutParams mlp = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    mlp.setMargins(100, 100, 200, 200);
    ImageView img1 = new ImageView(this);
    img1.setImageResource(R.drawable.c_file);
    img1.setLayoutParams(mlp);
    img1.setLongClickable(true);
    img1.setId(img_id);
    c_box.addView(img1);

    img1.setOnTouchListener(listener);
kinglomei
  • 126
  • 1
  • 1
  • 7

2 Answers2

5

Finally I found the point:

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
        Gravity.NO_GRAVITY);

the 3rd param(Gravity.NO_GRAVITY) that I missed. use it you can position views at anywhere with setting width/height.

kinglomei
  • 126
  • 1
  • 1
  • 7
2

You could use MarginLayoutParams

MarginLayoutParams mlp = (MarginLayoutParams) yourImageViewHere.getLayoutParams();
mlp.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);//all in pixels
yourImageViewHere.setLayoutParams(mlp);

To set your margins dynamically.

thomas.cloud
  • 881
  • 13
  • 30
  • Thanks a lot. It is possible after a imageview is created and add to the parent? – kinglomei May 08 '12 at 09:11
  • Well, I think you would have to redraw your layout if you are setting these margins after onDraw is called. To force a redraw of your view you could say yourImageViewHere.invalidate(); Please see http://developer.android.com/reference/android/view/View.html#invalidate(). Please accept my answer if this works for you. – thomas.cloud May 08 '12 at 14:34
  • Very thanks. I am glad to accept you answer if this problem is solved.I have edited my question that added some code. any advice? – kinglomei May 09 '12 at 02:25
  • Is this in your onCreate method? You just need to use MarginLayoutParams. So it would be MarginLayoutParams mlp = (MarginLayoutParams) img1.getLayoutParams(); mlp.setMargins(20, 40, 20, 40); img1.setLayoutParams(mlp); – thomas.cloud May 09 '12 at 04:20
  • Yes, this is in my onCreate method. and I have tried MarginLayoutParams but nothing is changed. See the question i have edited. – kinglomei May 09 '12 at 05:23
  • You didn't add what I wrote. You added a modified version, that's why it's not working. Add what I wrote in my pervious comment. – thomas.cloud May 09 '12 at 13:45
  • Hope I wasn't mean in my previous comment. I should say you could try adding what I wrote and maybe it will work, good luck. – thomas.cloud May 11 '12 at 02:51
  • Unfortunately, in my situation, I can't set the init position/margin:  ①the parent of the ImageView is FrameLayout.  ②the imageview can't be defined at the xml before the fact.  Can you give some test code? Thanks a lot. – kinglomei May 14 '12 at 08:26
  • Thanks a lot to <> for a long-time reply and patience. – kinglomei Jun 15 '12 at 02:15
  • works great this code to "move" an ImageView in a RelativeLayout using margins, thanks. – Pelanes Apr 11 '13 at 09:43