Set an image in imageView - android

I'm pretty new in Android and I am trying to make a Mancala (it's kind of an African game) app. I am looking for a way to update an Image View from one image to another programmatically (for those who are familiar with the game- I want the images of the empty holes to update to images with balls every time the player plays).
I tried to look for an answer but I didn't find one. Here is what I've tried:
imgHome2=(ImageView)findViewById(R.drawable.home0);

You can do same by using Drawable:
private ImageView imageView;
on onCreate()
imageView = (ImageView) findViewById(imageView);
Create new method for set drawable on ImageView.
private void setDrawableOnImageView(Drawable drawable){
imageView.setImageDrawable(drawable);
}
How to call above method:
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
setDrawableOnImageView(drawable);
You can change drawable as your requirement.

in findViewById you should pass an id not a drawable
imgHome2=(ImageView)findViewById(R.id.myimageviewid)
and than you can change the image by
imgHome2.setBackground(R.drawable.home0);

Related

how to play .svg animation file android activity

I have an animated *.svg file. I cannot find a way to play it in my android activity. I also need to know when the loop is over.
how to achieve this?
You could use https://shapeshifter.design/ in order to create an AnimatedVectorDrawable from plain SVG (see: https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable)
After creating the drawable and including it to the Assets Folder you can load the drawable and assing it to an ImageView, which is defined in your xml Layout.
See the following code:
final AnimatedVectorDrawableCompat animatedVectorDrawableCompat =
AnimatedVectorDrawableCompat.create(getApplicationContext(),
R.drawable.animatedvector);
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageDrawable(animatedVectorDrawableCompat);
final Animatable animatable = (Animatable) imageView.getDrawable();
animatable.start();
R.drawable.animatedvector is the AnimatedVectorDrawable you want to load, while R.id.imageView is the ImageView, which should show the AnimatedVectorDrawable.
Hope this helps ;-)

Displaying bitmaps

I need help with displaying a bitmap. I am using a class which is extending Activity. Where would I put this?
Mybitmap = BitMapfactory.decode resource(getresource,R.drawable.bit);
Also how would I draw it on the screen?
you should use ImageView...
put ImageView in you layout with "myImage" id and in activity do like this :
ImageView mImg = (ImageView) findViewById(R.id.myImage);
mImg.setImageBitmap(Mybitmap);

Converting bitmap to drawable in Android

I'm downloading the image from server and storing it in bitmap object. I want to set this image as background for button. But the button doesn't have the property setImageBitmap. So is there anyway I can set the background of button with the downloaded bitmap image? Like by converting the bitmap to drawable? Sorry, I'm new to Android, please bear with my mistakes (if any).
P.S : I want to use button control only. Because I want some text at the bottom of each buttons and I'm creating these buttons dynamically.
The best way to convert a Bitmap to drawable in android is as follows,
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
where bitmap is the name the Bitmap. Then set the drawable as your Button background as follows,
btn.setBackground(drawable);
N.B: Without specifying getResources() as the first argument, you may experience inconsistent image sizing across different screen densities.
for more info refer this
http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html
Simply use BitmapDrawable.
Drawable drawable=new BitmapDrawable(contact_pic);
Convert Bitmap to BitmapDrawable like this
Drawable drawable = new BitmapDrawable(getResources(),your_bitmap);
and then set it as background to button as ,
button.setBackgroundDrawable(drawable);
P.S. You can also do the same inline as ,
button.setBackgroundDrawable(new BitmapDrawable(getResources(),your_bitmap));
This is how to do it, I've used several times:
Drawable drawable = (Drawable)new BitmapDrawable(Bitmap);
Button button= new Button(this);
button.setBackground(drawable);

Android: Placing a bitmap or drawable image into an XML displayed screen

Well after days of looking I come to the source.
I have main screen that is drawn and created in Eclipse Graphical layout.
It consists of buttons across the top of the screen. Below the buttons is image.png area.
On startup of app it displays correctly. Buttons at top and graphic image below the buttons.
On pushing each button I want the image to be changed. I can supply either a drawable or bitmap image (figured that out).
I need help. What I don't understand is how I get an external image to display with the XML generate screen buttons.
I have currently many test trials but none give me what I am looking for.
I want to place either a drawable or bitmap image into R.id.imgVwMain and have it display with the buttons. I know I've got extra code in what is provided.
R.id.imgVwMain as a default has an image in it at start up (ie splash screen) but I want to replace it dynamically on button push.
Code snippets:
ImageView img=new ImageView(this);
View image;
Drawable drawable= getImg(0); // GETS NEW IMAGE as drawable
img.setImageDrawable(drawable);
//setContentView(img); // when uncommented displays just the image no buttons, not what needed
//R.id.imgVwMain // the target ID to place the new image into
image = findViewById(R.id.imgVwMain);
ImageView imageView=(ImageView)findViewById(R.id.imgVwMain);
imageView.setImageBitmap(drawableToBitmap(drawable));
image.setImageResource(imageView);
setContentView(R.layout.main); //of course this just displays default of the XML
Set onClickListeners onto your buttons like so:
Button mButton = (Button) findViewById(R.id.my_button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ImageView ivImage = (ImageView) findViewById(R.id.imgVwMain);
ivImage.setImageDrawable(getImg(0));
}
});
Now, whenever the button is clicked it set's the image of your imageview to whatever you'd like.
You can probably use addView to add an imageView dynamically and likewise call removeView to get rid of an imageView. At some point you'll need to set the layout parameters as well. Hopefully this will get you started:
myParentViewOrLayout.removeView(oldImage);
ImageView newImage = (ImageView) findViewById(R.id.imgToAdd);
//Set your layoutParams
myParentViewOrLayout.addView(newImage);

setImageResource for an ImageView from another ImageView

Strange request here, but is there any way you set an imageview to hold the same drawable as another imageview by getting it from the original imageview. what i mean is:
ImageView image1 = new ImageView(this);
image1.setimageResource(R.drawable.blah1);
ImageView image2 = new ImageView(this);
//now i want something like this, which doesn't work. i want to get it from image1
image2.setImageResource(image1.getDrawable());
so please how can i achieve this from image1?.. Thank you :)
Use setImageDrawable instead of setImageResource
For setting Image Resources from another image view
You need to use two methods of ImagView
setImageDrawable()
getDrawable()
Your code is all perfect except one error
You need to use setImageDrawable() in place of setImageResource()
following is your code implemention using above methods with correction
ImageView image1 = new ImageView(this);
image1.setimageResource(R.drawable.blah1);
ImageView image2 = new ImageView(this);
// just change this to setImageDrawable
image2.setImageDrawable(image1.getDrawable());

Categories

Resources