Dynamic Branding on Android - java

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);

Related

How do I access an image as a Drawable in AndroidStudio?

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

Why does my ImageView work on Windows but not on Mac

Here is the simple code for javafx in intellij:
Image image = new Image(url);
ImageView imageView = new ImageView(image);
movieBox.getChildren().add(imageView);
//checking
System.out.println(imageView.getImage().getUrl());
Textfield tx = new Textfield();
tx.setText("hi");
movieBox.getChildren().add(tx);
I copied the project over to intellij on my mac and everything works accept the images won't show. Here are the things I tried:
Checking that the URL is valid by printing out the imageview image url. It prints out a valid URL of an image i can open in my browswer
Adding a random textfield to the movieBox (a flowpane object) to make sure it works. And it does.
There shouldn't be any error in the code because it works in my windows intellij. It seems like a problem with the environment, but I haven't even got a clue how should i approach this problem. Any sort of advice will be nice, thanks!
Try downloading the image and using the path to the image on your disk, if it works then the problem will be with the url, otherwise its with the ImageView. If the problem is with the URL, post the one you are trying to use so the community can test it, and try another URL.
P.S. If you aren't manipulating the image you can just pass the path/url directly top the ImageView constructor.
public ImageView(String url)
Allocates a new ImageView object with image loaded from the specified URL.
The new ImageView(url) has the same effect as new ImageView(new Image(url)).

How to view an Image View to a linear layout in android dynamically on andorid

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.

How to change background in Java?

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.

Display Bitmap resource in MainScreen banner for BlackBerry

Hey guys,
I'm having trouble displaying my icon in the banner of my MainScreen. The Icon is located in the res/ directory. Here's my MainScreen code:
HorizontalFieldManager mngr = new HorizontalFieldManager(USE_ALL_WIDTH);
LabelField label = new LabelField("AACD");
BitmapField pic=new BitmapField(Bitmap.getBitmapResource("res/aacdIconSmall.PNG"),Field.FOCUSABLE);
mngr.add(pic);
mngr.add(label);
this.setBanner(mngr);
BrowserField browserField = new BrowserField();
add(browserField);
The AACD Label shows up above my BrowserField but my pic bitmap never shows up. Any ideas?
Your code is OK.
However I do suspect you did not add the image to your JDE project, so the image is not included in the resulting .cod file during building the project.
No need to use res folder
try Bitmap.getBitmapResource("img/aacdIconSmall.png")
the folder should be like this
res
- img
- aacdIconSmall.png
(i dunno is accdIconSmall.PNG is case-sensitive or not, have not tested yet, just make sure you are using the same upper and lower case)

Categories

Resources