Linearlayout background image in assets folder - android

I want to use a Image from the assets folder as my background image in android
but the only picture I can use is out oif the drawable folder
random = String.valueOf(util_random.random(1, 4));
String drawable = "/assets/images/img"+random ;
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundDrawable(drawable);
this.setContentView(ll);

You cannot pass a String in View.setBackgroundDrawable(Drawable).
Use BitmapFactory to create a Bitmap, then create a BitmapDrawable and use it as your background image.

Related

android change background of activity

I am trying to use the following to change the background to an image stored on the sd card:
String pathName = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ "/newbackground.jpg";
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
View view = findViewById(R.id.fred);
view.setBackgroundDrawable(bd);
But it keeps crashing.
I´m using setBackgroundDrawable as I want it to work below api 16 and it is deprecated.
I put in a check to see if pathname exists and it does.
Any ideas?
mark

Set ImageView Drawable programmatically

I am trying to set the image of a ImageView programmatically from a array of drawables but keep getting a NUllPointerException....does this method look correct for setting Image resource..
//get a random drawable
int[] imageSelection= {R.drawable.buddycheck, R.drawable.logdive3, R.drawable.sea, R.drawable.weather, R.drawable.logo1};
Random whichImage = new Random();
int theImage = whichImage.nextInt(imageSelection.length);
displayImage.setBackgroundResource(theImage);
You are setting the image resource to a random number.
You need to do it like this:
int theImage = imageSelection[whichImage.nextInt(imageSelection.length)];
displayImage.setBackgroundResource(theImage);
Try this : imageView.setImageResource(R.drawable.image);

can we pass a directory path to Drawable.createfrompath instead of a file path?

can we pass a directory path to Drawable.createfrompath instead of a file path?
Ex:
Drawable d = Drawable.createFromPath("aq/img/sample.png");
converts the path to a drawable "d". I want to pass a folder path to the same function, say "/aq/img/" and I want convert all images present in that folder into a drawable. Can this be done? and how?
EDIT:
Did this to display a single image in my imageView.
ImageView imgView01 = (ImageView) findViewById(R.id.imageView1);
File dir = new File("/sdcard/WallpapersHD/");
File file[]=dir.listFiles();
for (int i=0;i<file.length;i++) {
Drawable d = (Drawable) Drawable.createFromPath(file[i].toString());
imgView01.setImageDrawable(d);
Now how do I set a delay of say 5 seconds and then set a different image in the imageview?
After do this to start to count 5 seconds
long time = SystemClock.elapsedRealTime();
put this into a loop:
if(SystemClock.elapsedRealtime() > time + 5000){
ImageView img01 = (ImageView) findViewById(R.id.yourNewImg);
img01.setImageDrawable(d);
}
"d" is your Drawable.

How to show image using ImageView in Android

I am looking for the way to assign image src to image view control. I read few example and they says something src="#drawable\image" but didn't understand this, also I want to assign image src at runtime by java code also want to apply default image in XML.
If you want to display an image file on the phone, you can do this:
private ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.imageViewId);
mImageView.setImageBitmap(BitmapFactory.decodeFile("pathToImageFile"));
If you want to display an image from your drawable resources, do this:
private ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.imageViewId);
mImageView.setImageResource(R.drawable.imageFileId);
You'll find the drawable folder(s) in the project res folder. You can put your image files there.
You can set imageview in XML file like this :
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/imagep1" />
and you can define image view in android java file like :
ImageView imageView = (ImageView) findViewById(R.id.imageViewId);
and set Image with drawable like :
imageView.setImageResource(R.drawable.imageFileId);
and set image with your memory folder like :
File file = new File(SupportedClass.getString("pbg"));
if (file.exists()) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap selectDrawable = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
imageView.setImageBitmap(selectDrawable);
}
else
{
Toast.makeText(getApplicationContext(), "File not Exist", Toast.LENGTH_SHORT).show();
}
In res folder select the XML file in which you want to view your images,
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/imagep1" />
shoud be #drawable/image where image could have any extension like: image.png, image.xml, image.gif. Android will automatically create a reference in R class with its name, so you cannot have in any drawable folder image.png and image.gif.
Drag image from your hard drive to Drawable folder in your project and in code use it like this:
ImageView image;
image = (ImageView) findViewById(R.id.yourimageviewid);
image.setImageResource(R.drawable.imagename);

How to use relativelayout.setBackgroundDrawable() with a bitmap?

I have a RelativeLayout object and want to dynamically change the background image with a dynamically created Bitmap object (it changes its color dynamically).
I saw that when I wanted to update the background image of the RelativeLayout object that I can only choose setBackgroundDrawable() which requires a Drawable object as a parameter.
My question is, how can I convert the dynamically created Bitmap object into a Drawable object?
BitmapDrawable(obj) convert Bitmap object into drawable object.
Try:
RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative1);
Drawable dr = new BitmapDrawable(bit);
(view).setBackgroundDrawable(drawable);
I hope this will help you.
You can do it by this way
Drawable drawable = new BitmapDrawable(bitmap);
RelativeLayout r;
r = (RelativeLayout) findViewById(R.id.relativelayout1);
ll.setBackgroundDrawable(drawable);
Try this,
Drawable drawable = new BitmapDrawable(bitmap);
RelativeLayout rl=(RelativeLayout) findViewById(R.id.main1);
Bitmap myImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Drawable dr = new BitmapDrawable(myImage);
rl.setBackgroundDrawable(dr);
Drawable d = new BitmapDrawable(bitmap);
//Create Reference for RelativeLayout
RelativeLayout layout = (RelativeLayout) findViewById(R.id.rl);
//set Background to layout reference using the following method
Drawable drawable = getResources().getDrawable(R.drawable.bg);
layout.setBackground(drawable);
Note: R.id.rl is id of RelativeLayout
R.drawable.bg id the Image in drawable folder

Categories

Resources