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.
Related
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativer);
layout.setBackgroundResource(R.drawable.bgorange);
I want the bgorange to get retrieved from a spinner. So bgorange is a static value for now.
In general, for getting a resource programmatically you can use:
public static final Drawable getDrawable(Context context, int id) {
return ContextCompat.getDrawable(context, id);
}
So, in your exmaple you can use it like this:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativer);
Drawable drawable = getDrawable(context, YourSpinner.getYourResBackground());
layout.setBackgroundDrawable(drawable)
Note that you need the latest support lib in your build.grade
compile 'com.android.support:support-v4:xxxxxx
im trying to create a listview where if you press an item on that list, it displays an image in fullscreen. I have about 10 different items in the list and I want to display different images for each item.
I used this tutorial: http://www.androidhive.info/2011/10/android-listview-tutorial/
But instead of showing the name of my string item, i want it to show an image. How do i fix this?
I have been trying to check other questions, but they have been kinda hard to follow since im new to this.
First of all you need to have images in your drawable folder for all different items(in list view)
Change the single_list_item.xml to
single_list_item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
change your SingleListItem.java
package com.androidhive.androidlistview;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SingleListItem extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.single_list_item_view);
ImageView productimage = (ImageView) findViewById(R.id.image);
Intent i = getIntent();
// getting attached intent data
String product = i.getStringExtra("product");
// displaying selected product name
switch(product){
case "item1":
productimage.setImageDrawable(R.drawable.image1);
break;
case "item2":
productimage.setImageDrawable(R.drawable.image2);
break;
case "item3":
productimage.setImageDrawable(R.drawable.image3);
break;
.
.
.
.
.
//upto all 10 images
}
}
}
You should be able to do this with only a few changes to the tutorial code.
First, in single_list_item_view.xml, you'll need to use an ImageView instead of a TextView, since you want to show an image. You can find documentation for image views, including what attributes they support, on the Android developer site.
Second, you'll need to change the line that looks up the TextView to find your new ImageView instead. This will only require changing the following line:
TextView txtProduct = (TextView) findViewById(R.id.product_label);
to:
// Assuming you gave your ImageView the id "product_image" in single_list_item_view.xml
ImageView productImage = (ImageView) findViewById(R.id.product_image);
You'll need to add the images that you want to show into your project. The simplest place for these to go is in the res/drawable folder. The name of the image files will determine their "ids". For example, if you have res/drawable/photoshop.png, you can refer to it with the id R.drawable.photoshop.
Next you need to figure out which image to show in SingleListItem. The key part of how clicking on an item in the list shows something else is in SingleListItem:
Intent i = getIntent();
String product = i.getStringExtra("product");
Correspondingly, in AndroidListViewActivity:
Intent i = new Intent(getApplicationContext(), SingleListItem.class);
i.putExtra("product", product);
startActivity(i);
The Intent is how you send information from the list activity to the activity that shows the item. In this case, the information being sent is which item to show. You have a few options about how to do this part. One simple way is just to inspect the product string and choose the appropriate drawable:
int imageId = -1;
if (product.equals("Adobe After Effects")) {
imageId = R.drawable.after_effects;
} else if (product.equals("Adobe Bridge")) {
imageId = R.drawable.bridge;
} else if ...
... // All your other cases
}
This approach obviously doesn't scale very well, but it will work okay for your simple example. Another possible approach would be to pass the image id in the Intent, rather than the product name, for example:
// In AndroidListViewActivity:
i.putExtra("product", R.drawable.after_effects);
// In SingleListItem:
int imageId = i.getIntExtra("product", -1);
// -1 in the call above is the value to return if "product" is not in the Intent
The final thing you need to do is set the Drawable for the ImageView you found earlier:
// Make sure to check that the id is valid before using it
if (imageId != -1) {
Drawable d = getResources().getDrawable(imageId);
productImage.setDrawable(d);
}
getResources() fetches the Resources object for you application. You can use the Resources object to look up things like strings and drawables you've defined in your res folder. The tutorial you're following also uses it to look up an array of strings in the res folder. Again, you can see everything Resources supports on the Android developer site.
Instead of showing the item/text in SingleListItem class you could use the passed item to check which image should be displayed in an imageview. Something like that:
public class SingleListItem extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.single_list_item_view);
// TextView txtProduct = (TextView) findViewById(R.id.product_label);
ImageView image = (ImageView) findViewById(R.id.someImage);
Intent i = getIntent();
// getting attached intent data
String product = i.getStringExtra("product");
// displaying selected product name
if (product.equals("item1")) {
image.setDrawableResource(R.drawable.YOURIMAGE);
} else if (product.equals("item2"))
image.setDrawableResource(R.drawable.ANOTHERIMAGE);
}
}
I'am sorry for the dumb question but I'am still a beginner.
I'am using universal image loader : https://github.com/nostra13/Android-Universal-Image-Loader
(Android, Eclipse)
and I don't know how to make it load my image buttons(nearly 30 imagebuttons per layout), I don't want to change anything in the image I just want it to display it as it is in the xml file.
So if you could tell me what code should I type that would be great, Thanks
for example if I have this ImageButton set up in my java file:
ImageButton staff = (ImageButton) findViewById(R.id.staff);
and my java file is called MainActivity and this image is saved in my drawable folder what should I do.
a part of my code:
public class MainActivity extends Activity implements OnClickListener {
SoundPool sp;
int soundId;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
soundId = sp.load(kh2hd.this, R.raw.btnclick, 1);
Picasso.with(this).load(R.drawable.staff).into(staff);
ImageButton staff = (ImageButton) findViewById(R.id.staffbtn);
staff.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.staffbtn:
sp.play(soundId, 1, 1, 0, 0, 1);
startActivity(new Intent("android.intent.action.INFO"));
break;
Download Picasso library from here
Add it to your build path. Import this library to your code file.
For loading from web url:
String url = "http://example.com"
Picasso.with(getApplicationContext()).load(url).into(imageView);
For loading from drawable:
Picasso.with(getApplicationContext()).load(R.drawable.imageFile).into(imageView);
Here, imageView is the name of variable you are using to represent ImageView. Eg.
ImageView imageView = (ImageView) findViewById(R.id.ImageView1);
If you are using ImageButton, change imageView variable to imageButton variable
ImageButton imageButton = (ImageButton)findViewById(R.id.imageButton);
Note that I am passing variable name.
#user3896367 i think below code is use full for you. if you upload image from drawable of web url using this code.
download universal image loader jar.
put jar in lib folder and add jar.
create application class.
public static void initImageLoader(Context context) {
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(
new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.build();
ImageLoader.getInstance().init(config);
}
4.user imageloader like below syntext.
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.showImageOnLoading(
R.drawable.wallpaper_placeholder)
.showImageForEmptyUri(
R.drawable.wallpaper_placeholder)
.showImageOnFail(R.drawable.wallpaper_placeholder)
.displayer(new FadeInBitmapDisplayer(500))
.cacheInMemory(true).cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565).build();.
imageLoader.displayImage(
URL or ResourceID,
imageButton, options);
try this.
I am new for Android. I want to create an jar file if i call a method of that jar file in another android application it will show an image in imageview of the application. For example
a method in jar file as DisplayImg(URL); i will use this jar as library in another application and here i will create an imageview
ImageView imageView;
imageView = (ImageView) findViewById(R.id.img);
Then if i call imageView.DisplayImg(URL); then it will display specified url image on the imageview. I spend lot more time to get the solution but not work.plz help with the code thank
You don't need to create a library, in fact, there is a same library available for doing exactly what you want to!
It's called Picasso. You can find it here.
Also note that if you want imageView.DisplayImage(URL); you should create a new instance of ImageView class, because it doesn't contain this method. I recommend using DisplayImage(URL, imageView);.
But in case you REALLY WANT to do this, here you go:
1- For creating a library, create a New Android Application Project, and in the second window, check Mark this project as a library option. Also notice that we don't need Activity.
2- Create a new class, such as ImageUtils.java in the newly-created project. Here you need to put the DisplayImage() method:
public static void DisplayImage(String URL, ImageView imageView) {
new DownloadImageTask(imageView).execute(URL);
}
And this for the DownloadImageTask class :
public static class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView imageView;
public DownloadImageTask(ImageView bmImage) {
this.imageView = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap imageBitmap = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
imageBitmap = BitmapFactory.decodeStream(in);
} catch (IOException e) {
// Probably there's no Internet connection. Warn the user about checking his/her connection.
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return imageBitmap;
}
protected void onPostExecute(Bitmap result) {
if (imageView != null) imageView.setImageBitmap(result);
}
}
3- Execute your project as an android application.
4- There should be a jar file under the bin folder. It's your library. Import it to your other applications and use it. In that applications you can use it with this code:
ImageView imageView = (ImageView) findViewById(R.id.img);
ImageUtils.DisplayImage(URL, imageView);
5- Don't forget to add this permission to the apps that use this library:
<uses-permission android:name="android.permission.INTERNET" />
Hope it helps :)
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