how to programmatically add an image to a activity android - android

Hi I am trying to programatically add an image to an activity for an android app
I have this :
for (int i = 0; i < num_devices; i++) {
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(vp);
try {
Class res = R.drawable.class;
Field field = res.getField(device_types.get(i));
int resId = field.getInt(null);
imageView = (ImageView) findViewById(resId);
}
catch (Exception e) {
Log.e("MyTag", "Failure to get drawable id.", e);
}
LinearLayout link_devices = (LinearLayout) findViewById(R.id.link_devices);
link_devices.addView(imageView);
however it doesnt let me get the location of the images ( i try and get 0 for getTop, getLeft etc..)
am i doing it wrong and was is the correct way to do it

You are not setting image on that imageview, and you have set WRAP_CONTENT as layout params, which means the size of the imageView is same as size of image you are setting on it.
Since no image is attached the imageView size is 0.
Try setting an image, using any one of the codes ;- imageView.setImageBitmap(Bitmap bm), setImageDrawable(Drawable drawable) or setImageResource(int ResID).
I don't get what you're doing the with this code & its not required:
Class res = R.drawable.class;
Field field = res.getField(device_types.get(i));
int resId = field.getInt(null);
imageView = (ImageView) findViewById(resId);

Related

Get SVG out of ImageView

I'm using AndroidSVG to dynamically place a signature rendered as an svg, for displaying to an ImageView.
Here is my code:
SVG svg = SVG.getFromString(singleEmployee.signature);
ImageView image_view = (ImageView) layout_signature.findViewById(R.id.toolboxtalk_signature_svg);
image_view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
image_view.setTag(String.format(Locale.US, "employee_signature%d", x));
Drawable drawable = new PictureDrawable(svg.renderToPicture());
image_view.setImageDrawable(drawable);
My question is, how do I get the SVG out of the ImageView later in my code execution.
Here is what I'm trying:
ViewGroup v1 = (ViewGroup) findViewById(R.id.job_ticket_form);
ImageView signatureImageView = (ImageView) v1.findViewWithTag("employee_signature1");
PictureDrawable pictureDrawable = (PictureDrawable) signatureImageView.getDrawable();
Picture picture = pictureDrawable.getPicture();
Context context = signatureImageView.getContext();
String employeeSigSVGString = null;
try {
SVG svg = SVG.getFromResource(signatureImageView.getContext(), R.id.toolboxtalk_signature_svg);
employeeSigSVGString = spinner_value + "|" + svg;
} catch (SVGParseException e) {
e.printStackTrace();
}
Any ideas how to get the SVG back out of the ImageView?

Why setVisibility() does work in for-statement?

There are a lot of questions on this topic but none whose answers solve my problem.
I have an array of ImageView and, through a for-statement, I would like to get them INVISIBLE. The code is the following.
final ImageView[] image = new ImageView[12];
image[0] = (ImageView) findViewById(R.id.imageView1);
image[1] = (ImageView) findViewById(R.id.imageView2);
image[2] = (ImageView) findViewById(R.id.imageView3);
image[3] = (ImageView) findViewById(R.id.imageView4);
for (int p = 0; p < 4; p++) {
image[p].setVisibility(View.INVISIBLE);
}
It seems that the problem is putting p as argument of image[], I think so because if I put a number instead of p it works.
try to use this it uses Varargs
public void hideViews(View... views)
{ //it will work with parent class but you can change it to ImageView
for (View view : views) {
view.setVisibility(View.INVISIBLE);
}
}
and the call:
hideViews(image1,image2,image3,image4);
good luck;

Adding Dynamically ImageViews to horizontal scrollview in android

I'm trying to show images dynamically by string as ID and want to display it in Horizontal scroll-view as i have 13 cards which can't be fit on screen
Here is my code its working fine but i want it in Horizontal Scroll View
int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
ImageView im = (ImageView) findViewById(resID);
Context context = im.getContext();
int id = context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(im.getLayoutParams());
lp.setMargins(counter * 43, 0, 0, 0);//left,right,top,bottom
im.setLayoutParams(lp);
im.setImageResource(id);
im.setOnClickListener(this);
counter++;
}
mp = MediaPlayer.create(getApplicationContext(), R.raw.clicksound);
}![enter image description here][2]
As per your requirement I will suggest you to RecyclerView which comes with library which you will have to add and use RecyclerView with horizontal orientation refer to link below.
https://developer.android.com/training/material/lists-cards.html
Or you can do it dynamically as below
here what you can do add Linear horizontal layout inside scrollview say id is imgcontainer then you can do is
create image view dynamically and add to linear layout.
for(int i=0;i<10;i++)
{
//create imageview here and setbg
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
((LinearLayout) findViewById(R.id.imgcontainer)).addView(
imageView, i);
}
int[] image_array = new int[]{R.drawable.image1, R.drawable.image2, ... R.drawable.image10};
LinearLayout ll = (LinearLayout) findViewById (R.id.ll_images);
for (int i=0 ; i<10; i++){
ImageView img = new ImageView (this);
img .setBackgroundResource (image_array [i]);
ll .addView(img );
}

Get coordinates of a programatically created imageview Android

So i am creating my views programatically like so:
for (int i = 0; i < num_devices; i++) {
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.
LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(vp);
try {
Class res = R.drawable.class;
Field field = res.getField(device_types.get(i));
int resId = field.getInt(null);
imageView.setImageResource(resId);
}
catch (Exception e) {
Log.e("MyTag", "Failure to get drawable id.", e);
}
LinearLayout link_devices = (LinearLayout) findViewById(R.id.link_devices);
link_devices.addView(imageView);
}
And i am trying to get the coordinates of the imageview:
I have tried the following methods, but am getting no where
imageView.getDrawable().getBounds().centerX() /centreY
imageView.getX() / getY
imageView.getLocationInWindow(); / onScreen();
Could someone please explain to me how to get the cooridinates of the image which is a drawable so i can draw a box around it or draw lines between two images (that is the planned functionality)
int top = imageView.getTop();
int left = imageView.getLeft();
From there you have the top-left point coordinates.

Change many imageView sources using for loop

I have 100 imageview in my layout :
iv1 = (ImageView) findViewById(R.id.ImageView1);
iv2 = (ImageView) findViewById(R.id.ImageView2);
iv3 = (ImageView) findViewById(R.id.ImageView3);
...
...
iv98 = (ImageView) findViewById(R.id.ImageView98);
iv99 = (ImageView) findViewById(R.id.ImageView99);
iv100 = (ImageView) findViewById(R.id.ImageView100);
Now, in my program I want change all image sources time to time, so now how i do this i want some thing like this
for (int F=1; F<101; i++) {
int resID = getResources().getIdentifier("a"+F, "drawable", getPackageName());
ivF.setImageResource(resID);
}
so, any suggestion ?
thanks.
Why dont you create it dynamically , set ids to them and use these ids as per your need to get image references .
I found solution by my own, this how I should declare ImageView
ImageView iv1,iv2,iv3,iv4,iv5,iv6;
ImageView[] image = {iv1,iv2,iv3,iv4,iv5,iv6}
and then when i need to set an image :
for (int F=1; F<7; F++) {
int resID = getResources().getIdentifier("a"+F, "drawable", getPackageName());
image[F].setImageResource(resID);
}

Categories

Resources