0

I'm playing with Android SDK 10 (I want to support devices from sdk 10)

I can't figure how to auto center the text in a Imageview.

The only way I can think of to center the text is to use padding, but I don't like it, it must be a better solution.

  1. Resizing Image from asset is not working

    InputStream ims = context.getAssets().open("myimage.png");
    Drawable d = Drawable.createFromStream(ims, null);
    d.setBounds(0, 0, (int)(d.getIntrinsicWidth()*0.5),
    (int)(d.getIntrinsicHeight()*0.5));
    
    ScaleDrawable sd = new ScaleDrawable(d,0,px,py);
    ims.close();
    Image = new ImageView(context);
    Image.setImageDrawable(sd);
    Image.setAdjustViewBounds(true);
    layout.addView(Image);
    
    TextView t = new TextView(this.context);
    t.setText(r.toString());
    t.setTextSize(80);
    t.setGravity(Gravity.CENTER);
    layout.addView(t);
    

Is there a way to add the text to image asset (ImageView) ?

Right now I added the TextView and ImageView in a RelativeLayout.

What is the best practice to create a group of asset image and text so that when I move the image the text is following (centered )

Yurets
  • 3,999
  • 17
  • 54
  • 74
Sodasi Web
  • 11
  • 4

3 Answers3

1

You can always add the CENTER_HORIZONTAL rule to the RelativeLayout, so all of its children remain centered horizontally.

Juan Martinez
  • 770
  • 5
  • 16
1

Try this. Keep Framelayout as the parent layout where you are adding the views. Now use LayoutParams to set the ImageView's gravity, height and width.

  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  layoutParams.gravity = Gravity.CENTER;
  imageView.setLayoutParams(layoutParams);

Now in the FrameLayout first add the Imageview and then the textview. Do not change anything for the textview.

 frameLayout.addView(imageView);
 frameLayout.addView(text);
  • it doesn't work, I will update the code with part of my class – Sodasi Web Jan 20 '17 at 15:53
  • Actually it is working, bu is not centered because of the first problem. The ScaleDrawable is not resizing the image. Do you have any ideea why ? – Sodasi Web Jan 20 '17 at 15:59
  • Instead of using the Scalable Drawable you can Add calculateInSampleSize() and decodeSampledBitmapFromResource() from [link](https://developer.android.com/training/displaying-bitmaps/load-bitmap.html) Calculate the required Width and required Height image you want and pass those values to calculateInSampleSize(). Also set the Scaletype of the ImageView according to your needs – Dhananjay Sarsonia Jan 20 '17 at 17:14
  • I will take a look and if is what I need I will pick this answer. Thank you – Sodasi Web Jan 22 '17 at 13:53
0

This one worked https://stackoverflow.com/questions/8471226/how-to-resize-image-bitmap-to-a-given-sizehttps://stackoverflow.com/questions/8471226/how-to-resize-image-bitmap-to-a-given-size

combined with the answer of @Dhananjay Sarsonia

Community
  • 1
  • 1
Sodasi Web
  • 11
  • 4