I'm working on an app that allows changing theme. To achieve that, I need to change background in java.
I created imageView and tried to set background as imageView.
I was using this code:
ImageView imgViewBackground =(ImageView) findViewById(R.id.imageViewBackground);
int ID = getResources().getIdentifier("imagebackground", "drawable", getPackageName());
imgViewBackground.setImageResource(ID);
but the app crashes after 20-30 seconds of usage.
I also tried this, but the app crashes on startup:
RelativeLayout layout =(RelativeLayout)findViewById(R.id.imageViewBackground);
layout.setBackgroundResource(R.drawable.imagebackground);
Is there an effective way to change background directly, and not through imageView in java?
Why not simply do this:
ImageView imgViewBackground =(ImageView) findViewById(R.id.imageViewBackground);
imgViewBackground.setImageResource(R.drawable.imagebackground);
P.S: An image by the name imagebackground must be present in the drawable folder.
Related
I'm working on an android app using java in AndroidStudio. I have an ImageView pic and I want to set its image as a .png that I have saved in my /drawable folder. I may try
pic.setImageDrawable(R.drawable.image_name);
However, setImageDrawable() requires a Drawable, while R.drawable.image_name returns an Integer (the ID of the image).
How may I resolve this?
You can use setImageResource() for this:
pic.setImageResource(R.drawable.image);
More about it here
Use this for java
ContextCompat.getDrawable(getActivity(), R.drawable.drawable_resource_name);
and then set this drawable to your image view
I have to add a fragment over an Imageview, but my ImageView goes on top and covers the other views. All this is done in a RelativeLayout.
things must be done dynamically (no xml). I want that my activity has an image, which can be set DYNAMICALLY, and my fragment (added dynamically) should have transparent background, and if this is not true, i can set it to transparent at run time.
This is the code to create the image:
ImageView img=new ImageView(this);
img.setImageResource(imgID);
img.setScaleType(ImageView.ScaleType.CENTER_CROP);
((ViewGroup)findViewById(android.R.id.content)).getRootView()).addView(img);
As you can see, the image works as background, i'm using a ImageView for its scaleType.
Is there a way to do so?
SOLUTION
solved, i added the image to the wrong container.
The function below, however, has been pretty useful. It has been taken from an other answer, and lets a view to get on the back of all the others.
public static void sendViewToBack(View child) {
final ViewGroup parent = (ViewGroup)child.getParent();
if (parent!=null) {
parent.removeView(child);
parent.addView(child, 0);
}
}
I have two activities. One that submits the data and one that shows the submitted data. I am able to add the images to the first activity fine and the data is then stored in a singleton data object with a ArrayList instance called "images".
The problem I have is that when trying to retrieve the images from the image array and display them it crashes the app. I have tried to debug the code and it runs perfectly until you get to the last line:
attachmentLayout.addView(image,imParams);
The Images are stored in the singleton correctly and when you debug it, it shows that they are in fact there.
I tried to figure it out for myself, but I can't find what it is. I am quite new to android programming so help would be much appreciated. I have added my code that tries to get the images below.
if(values.getImage()!=null){
int count = images.size()-1;
for (int x=0;x<=count;x++){
//declare attachments and get image from arraylist<ImageView>
LinearLayout attachmentLayout = (LinearLayout) findViewById(R.id.attachments);
ImageView image;
image = images.get(x);
//set parameters
LinearLayout.LayoutParams imParams =
new LinearLayout.LayoutParams(90,270);
image.setBottom(attachmentLayout.getBottom());
image.setVisibility(View.VISIBLE);
image.isShown();
//add to linear layout
attachmentLayout.addView(image,imParams);
}
}
Edit:
This is the logcat I get when the app restarts after is crashes. I don't believe this is helpful though.
08-24 12:34:18.522 4782-4820/com.example.jules_gribble.test I/Adreno﹕ QUALCOMM build : 065751b,
Build Date : 04/15/15
OpenGL ES Shader Compiler Version: E031.25.03.07
Local Branch :
Remote Branch : quic/LA.BF64.1.2.1_rb2.9
Remote Branch : NONE
Reconstruct Branch : AU_LINUX_ANDROID_LA.BF64.1.2.1_RB2.05.01.00.081.016 + 065751b + NOTHING
Managed to answer my question myself. Turns out it was crashing because I wa overlooking that in the submit activity it was declared
ImageView image = new ImageView(Test.this);
This meant it could not be used in a different activity (Test is the name of the class of the submit activity which will be changed).
To solve this I changed it to
ImageView image = new ImageView(Submitted.this);
where submitted is the name of the submitted activity. I then needed to get the bitmap of the image and set the bitmap to the new imageview.
Bitmap bitmap = ((BitmapDrawable)images.get(x).getDrawable()).getBitmap();
image.setImageBitmap(bitmap);
I found this from HERE.
Hope this will help someone in the future.
I am using this project universal-image-loader in order to display in a gridview several images. But I would like to modify these images with some html (for example add ads at the bottom of each images).
I saw the image are displayed by calling imageLoader.displayImage(..), in the method getView of the ImageAdapter.
I don't know what approach is better:
add a step in displayImage(...) in order to create a webview with my image and html and transform this webview in bitmap?
modify the getView of ImageAdapter which will create the webview and add an argument webview in displayeImage()?
...
...
I guess I don't have the choice, I have to use a webview.
Moreover in the method displayImage() of ImageLoader.java, i don't understand where the images are loaded, I guess this is somewhere in this line, but when I am looking in these methods I can't find/understand
ImageSize targetSize = getImageSizeScaleTo(imageView);
String memoryCacheKey = MemoryCacheUtil.generateKey(uri, targetSize);
cacheKeysForImageViews.put(imageView.hashCode(), memoryCacheKey);
Bitmap bmp = configuration.memoryCache.get(memoryCacheKey);
(How from a string built by an uri and ImageSize we can have a bitmap? where is the image?)
Images are loaded asynchronously. So your code piece is just a top of complex logic. Main work is done in LoadAndDisplayTask class but you don't need to touch it.
For your case you can create own BitmapDisplayer:
create BitmapDisplayer implementation
implement Bitmap display(Bitmap bitmap, ImageView imageView) method
in this method you can process incoming Bitmap as you want
set result Bitmap in ImageView and return result
Set your BitmapDisplayer into configuration.
My requirement is that i need to change all the images and colors at run time. But as far as i know the images needs to be in the drawable folders. So my question is, after i export the apk file will i be able to download the images from a server through the application and set them as the images in the app?
For an example, lets say i have a linear layout with a background image which is in my drawable folder. After installing the app in the device can i download an image from a server and set it as the background of the linear layout?
I hope i have made the question clear. Any help would be greatly appreciated.
Thanks in advance.
try this one (i posted my version for FrameLayout, Imageview should be similar):
String myJpgPath = "/sdcard/picture.jpg";
Bitmap image_b = BitmapFactory.decodeFile(myJpgPath);
final BitmapDrawable image_d = new BitmapDrawable(image_b);
final FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
main.setBackgroundDrawable(image_d);