I want to show an image, depending on the value of the int imgch
This is in my onCreate:
ImageView image = (ImageView)findViewById(R.id.gimage);
Resources res = getResources();
Drawable gameImage = res.getDrawable(images[imgch]);
And this in my XML:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/gimage" />
How do I get it to show the correct image in the activity?
Yes, but with java-side code
ImageView img = (ImageView) findViewById(R.id.gimage);
img.setImageDrawable(getDrawable(R.drawable.my_image_name));
If you wanna load from an remote url you may be use this
https://github.com/nostra13/Android-Universal-Image-Loader
Check this ImageView#setImageDrawable. So you should have image.setImageDrawable(gameImage)
Create an Array
int[] images;
on
protected void onCreate(Bundle savedInstanceState) {
add this
images = new int[3];
images[0] = R.drawable.first_image;
images[1] = R.drawable.second_image;
images[2] = R.drawable.third_image;
and then your code (where imgch is an integer)
...
Drawable gameImage = res.getDrawable(images[imgch]);
...
use
Resources resources = getResources();
image.setImageDrawable(resources.getDrawable(R.drawable.myfirstimage));
Related
I want to set an Image Source in Android,
XML :
<ImageView
android:layout_width="200dp"
android:layout_height="300dp"
android:id="#+id/main"
android:src="#drawable/malayali"
android:layout_marginTop="100dp"
android:layout_marginLeft="80dp"
/>
Java :
public class MainActivity extends ActionBarActivity {
public SharedPreferences exactPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
exactPreferences = getSharedPreferences("details",MODE_PRIVATE);
getSupportActionBar().hide();
new RetriveIcon().execute(); //Api Calling & Storing value in SharedPreference
String value = exactPreferences.getString("main_url",null);
Log.i("from exactpreference",value); // Working fine !!! (http://www.exampple.com/storage/images/filename.jpeg)
ImageView banner = (ImageView)findViewById(R.id.main);
banner.setImageDrawable(getResources().getDrawable(R.drawable.value));
setContentView(R.layout.activity_main);
}
}
androidStudio showing error in the below line of value as red.
banner.setImageDrawable(getResources().getDrawable(R.drawable.value));
ERROR:
Cannot resolve symbol 'value'
How can I solve this Error ?
If you are dealing with the drawable name in your Resources folder, then Your problem is one of those options:-
1)You don't have the drawable value in your Resources.
Make sure it exists in your resources folder.
2)You are importing the wrong R in your activity
Make sure you are importing your application R not the android one.
If your are using a Direct URL for the image. I recommend you to use Universal Lazy Loader third party for that. All you have to do is to pass the image direct URL and the ImageView and it will do the job for you.
In your Gradle file "Module:app"
dependencies {
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
....
}
Are you trying to load an image from your drawable folder or download one from the Internet?
This:
banner.setImageDrawable(getResources().getDrawable(R.drawable.value));
Will work if you have a value.png file in one of your drawable folders. If you are trying to load an image from the net (i.e. something like this: http://www.exampple.com/storage/images/filename.jpeg), you'll need to download it first and set the bitmap as an ImageView source. There are many 3rd party libraries doing this. Take a look at Picasso:
Picasso.with(this).load(value).into(banner);
EDIT:
In order to add Picasso you need to update the build.gradle file (the one which refers to a module not project). You should have a section like this:
dependencies {
...
}
You need to add this line inside it:
compile 'com.squareup.picasso:picasso:2.5.2'
Try this Simple code
ImageView banner = (ImageView)findViewById(R.id.main);
banner.setBackgroundResource(R.drawable.value);
But image name must be value and store in your drawable folder.
Try this :
String uri = "#drawable/myresource.png";
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);
Note : This is sample code.
You want to set your imageview's background with an online image file. You can use this example on your project. Usage of this is very easy.
Also in your codes, you should change some lines after adding classes from the above link:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exactPreferences = getSharedPreferences("details",MODE_PRIVATE);
getSupportActionBar().hide();
new RetriveIcon().execute(); //Api Calling & Storing value in SharedPreference
String value = exactPreferences.getString("main_url",null);
Log.i("from exactpreference",value); // Working fine !!! (http://www.exampple.com/storage/images/filename.jpeg)
// ImageLoader class instance
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
ImageView banner = (ImageView)findViewById(R.id.main);
imgLoader.DisplayImage(value, 0, banner);
}
Good luck.
I made a GIF from 2 JPGs photos, using the spin_animation.xml, according to the relative Android Developers tutorial. It works fine. But I want to change the GIF inside the ImageView in my layout. Basically, I want to change it to 50 different GIFs.
Is there a better and less silliest way to do that, instead of creating 50 spin_animation.xml, one for each GIF ?
For example, is there a way to use the id that I gave to the animation-list inside it ?
In my .java file, in the onCreate method, I call the spin_animation.xml in order to make a gif the 2 jpg pictures that I have in my drawables.
My .java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress);
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setBackgroundResource(R.drawable.spin_animation);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
// Start the animation (looped playback by default).
frameAnimation.start();
}
My spin_animation.xml file in my drawable contains:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/selected" android:oneshot="false">
<item android:drawable="#drawable/barbellcurl1" android:duration="1000" />
<item android:drawable="#drawable/barbellcurl2" android:duration="1000" />
</animation-list>
And at last my ImageView in my .xml layout:
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="106dp" />
You can create or modify animation drawables at runtime if you like:
AnimationDrawable createFrom(Context context, int id1, int id2) {
AnimationDrawable ad = new AnimationDrawable();
ad.addFrame(context.getResources().getDrawable(id1), 1000);
ad.addFrame(context.getResources().getDrawable(id2), 1000);
return ad;
}
Using above, you could do:
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setBackground(createFrom(this, R.drawable.barbellcurl1, R.drawable.barbellcurl2));
and you should still have the same result.
You can also define those drawables in a xml array: http://developer.android.com/guide/topics/resources/more-resources.html#TypedArray - the advantage is that you have to write less code and you can now define all your "gif"s in a single xml file.
Using a typed array should work like this:
AnimationDrawable createFromArray(Context context, int arrayId) {
TypedArray array = context.getResources().obtainTypedArray(arrayId);
AnimationDrawable ad = new AnimationDrawable();
for (int i = 0; i < array.length(); i++) {
ad.addFrame(array.getDrawable(i), 1000);
}
array.recycle();
return ad;
}
The content of the XML file would look like res/values/gifs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="gif1">
<item>#drawable/barbellcurl1</item>
<item>#drawable/barbellcurl2</item>
</array>
<array name="gif2">
<item>#drawable/cat</item>
<item>#drawable/dog</item>
</array>
</resources>
And to get a drawable in code you would do createFromArray(this, R.array.gif1) etc.
The code in onCreate in the end would be
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress);
ImageView img = (ImageView)findViewById(R.id.imageView1);
AnimationDrawable ad = createFrom(this, R.drawable.barbellcurl1, R.drawable.barbellcurl2);
// OR
AnimationDrawable ad = createFromArray(this, R.array.gif1);
ad.setOneShot(false);
img.setBackground(ad);
ad.start();
}
I have written this code that loads an image to in ImageView widget:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
i = (ImageView)findViewById(R.id.imageView1);
new get_image("https://www.google.com/images/srpr/logo4w.png") {
ImageView imageView1 = new ImageView(GalleryActivity.this);
ProgressDialog dialog = ProgressDialog.show(GalleryActivity.this, "", "Loading. Please wait...", true);
protected void onPreExecute(){
super.onPreExecute();
}
protected void onPostExecute(Boolean result) {
i.setImageBitmap(bitmap);
dialog.dismiss();
}
}.execute();
}
bu now, I want to load several images. for this I need create image views dynamically but i don't know how...
I want run my code inside a for loop:
for(int i;i<range;i++){
//LOAD SEVERAL IMAGES. READ URL FROM AN ARRAY
}
my main problem is creating several ImageViews inside a loop dynamically
you can modify the layout , image resource and no of images (may be dynamic as well) according to your requirement...
LinearLayout layout = (LinearLayout)findViewById(R.id.imageLayout);
for(int i=0;i<10;i++)
{
ImageView image = new ImageView(this);
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
image.setMaxHeight(20);
image.setMaxWidth(20);
// Adds the view to the layout
layout.addView(image);
}
You can use this code to create ImageViews
ImageView image = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
image.setLayoutParams(vp);
image.setMaxHeight(50);
image.setMaxWidth(50);
// other image settings
image.setImageDrawable(drawable);
theLayout.addView(image);
where theLayout is the layout you want to add your image views to.
For more customization check out the dev page, where all the possible options are listed.
If your requirement is show in List or Gridview then you should go for Lazy Loading List or grid.
Please go through the below link.
https://github.com/thest1/LazyList
https://github.com/abscondment/lazy-gallery
Try this
rootLayout = (LinearLayout) view1.findViewById(R.id.linearLayout1);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_VERTICAL;
params.setMargins(0, 0, 60, 0);
for(int x=0;x<2;x++) {
ImageView image = new ImageView(getActivity());
image.setBackgroundResource(R.drawable.ic_swimming);
rootLayout.addView(image);
}
I'm trying to follow this this tutorial to add image into imageView from URL.
public class AndroidLoadImageFromURLActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.image);
String image_url = "http://api.androidhive.info/images/sample.jpg";
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(image_url, image);
}
}
However, I have the image urls stored in my database table and would like to grab and display dynamically in java. please kindly show me how to do this in java? Thank you so much!
try this link
here there is a function named showImage() which accepts a url as parameter and returns bitmap which you can use to set to the image view.
and dont forget to give the internet permission when dealing with images to be displayed from a url
Here is sample code:
WallpaperManager wallpaperManager1 = WallpaperManager
.getInstance(getApplicationContext());
final Drawable wallpaperDrawable1 = wallpaperManager1.getDrawable();
getWindow().setBackgroundDrawable(wallpaperDrawable1);
if (wallpaperDrawable1==null)
{
Resources res = getResources();
Drawable drawable1=res.getDrawable(R.drawable.bg1);
getWindow().setBackgroundDrawable(drawable1);
}
I wanted to get the system background in my app, If its not there or if user removes it, then I wanted to set a default image from my app to set as app background. Hope Its all clear....
To see if current system has wallpaper, you use peekDrawable():
final Drawable wallpaperDrawable1 = wallpaperManager1.peekDrawable();
Also, you missed else:
if (wallpaperDrawable1==null)
{
Resources res = getResources();
Drawable drawable1=res.getDrawable(R.drawable.bg1);
getWindow().setBackgroundDrawable(drawable1);
}
else
{
getWindow().setBackgroundDrawable(wallpaperDrawable1);
}