changing he background image of the application - android

Hi I am beginner in android and I am trying to learn it, I wrote a small code for changing the background image of the application when clicking on some button:
public void processClicks(View display)
{
display.setBackgroundResource(#drawable/fgd4);
}
but it got me an error message that :
drawable cannot be resolved to a variable
fgd4 cannot be resolved to a variable
but I have created a folder named drawable in the res folder and I put the fgd4.jpg image in it.
Please help me to solve this small problem

You must reference your drawable in code differently than you do in your XML layouts.
display.setBackgroundResource( R.drawable.fgd4 );

Try Like This:
display.setBackgroundResource(R.drawable.fgd4);

Related

Texture2D is working in Editor but not in Android device

I am trying to change the texture of my object with this code:
Texture2D baileyburlwood = Instantiate(Resources.Load("bailey burlwood") as Texture2D);
myObject.GetComponent<Renderer>().material.mainTexture = baileyburlwood;
It is working perfectly fine in the editor, the texture changes but when I tried to run it in my android device, my object just goes black. There is also no error or any warning. Pls help! Thanks!
I am using Unity 5.5.1f btw
From the screenshot in your updated question, the image you want to load is called bailey burlwood.jpg which is already in the Resources folder..
Herein lies the problem:
Instantiate(Resources.Load("bailey burlwood") as Texture2D);
You instantiate prefabs, scripts and components not normal classes like Texture2D.
Your code would have worked if bailey burlwood.jpg is bailey burlwood.prefab and you load it with GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject; but that's not the case here.
Since the "bailey burlwood" file is a JPG file, you should load like this:
Texture2D baileyburlwood = Resources.Load("bailey burlwood") as Texture2D;
myObject.GetComponent<Renderer>().material.mainTexture = baileyburlwood;
Note that there is no Instantiate function involved. See this post for how to load other image files with different image settings when using the Resources folder.

QLabel pixmap works in Designer but not in the actual program

I've set a company logo above the main menu. This is what it looks like in QtDesigner:
I distorted the logo on purpose. The point is that the image is there.
This is the resource folder in QtCreator project view:
This is what it looks like on my android phone:
So, well, what's going on here? I see no file errors in console (and loading CSS files from resources works form me). One thing that is possibly relevant is that the image is a SVG file.
SVG was indeed an issue. While QtDesigner displays SVG without problems, the actual Qt library cannot handle that. I used this code to test it:
QPixmap pixmap;
if (!pixmap.load( ":/logo.svg" )) {
QDebug("Failed to load main svg logo.");
if (!pixmap.load( ":/logo.png" )) {
QDebug("Failed to load main png logo.");
}
else {
QDebug("PNG logo loaded.");
}
}
else {
QDebug("SVG logo loaded.");
}

ClassCastException in Android After Changing Drawable Name?

I am writing an Android app and have run into a very strange situation. I have an animation-list xml file that I have created for an animated loading graphic named 'loading.xml' and I am setting it as the background for an ImageView in my code. It is something like this:
public void startLoading() {
if(!isLoading) {
loading.setBackgroundResource(R.drawable.loading);
loading.post(new Runnable() {
public void run() {
anim = (AnimationDrawable) loading.getBackground();
anim.start();
}
});
flMask.setBackgroundResource(R.drawable.mask_loading);
flMask.setVisibility(View.VISIBLE);
flMask.bringToFront();
isLoading = true;
}
}
This works wonderfully with no problems whatsoever. However, if I rename 'loading.xml' to 'anim_loading.xml' (to keep it inline with my usual naming scheme) I get a force close with
java.lang.ClassCastException: android.graphics.drawable.ColorDrawable
at the line where
anim = (AnimationDrawable)loading.getBackground();
After changing the name, it force closes, and if I change it back to just 'loading.xml' it works fine again.
I am really stumped on this one. I have tried creating a completely new file with the correct name, various clean/builds, closing/reopening Eclipse, closing/reopening the AVD, wiping the AVD and reinstalling fresh, I have tried renaming the xml file to something else entirely, and there are no other resources with the same names. So far, nothing has worked outside of leaving the file named 'loading.xml', which at least it works, but it is annoying. I haven't been able to find anyone with the same type of problem, which as vast as SO and the internet are, usually means that it's something stupid that I have missed somewhere along the line.
Does anyone have any ideas on why it will not let change the name on this file?
After giving up and just leaving it alone for a little while, I just stumbled upon the answer to this question!
Apparently, there is some sort of bug with Android that causes the first file in the res/drawable directory to be viewed as a ColorDrawable regardless of what it actually is. Before, when I was trying to change the name of the file, the new name put it at the top of the list in the directory and that was enabling this bug to show itself. Without the name change, the top item in my drawable directory just happened to be a ColorDrawable so I never noticed this weird bug until after I tried changing that name.
To solve the problem (or rather, to work around the problem) I created an empty dummy file in the res/drawable directory named a.xml. As odd as it may sound, it fixed everything!
You should save in drawable res/drawable/rocket.xml
It will fix the question.

getting Eclipse to see recently added Drawables (png artwork)

I'm having trouble getting Eclipse to see that I've just put a new image into my project when referencing that image using R.drawable.this_text
So I drag my png into myProject/res/drawable-hdpi in Eclipse's Project Explorer. The name of the image is this_text and it's a png!
So I go into my application and I want to put this on the screen; So here's the code for that...
private void setTitle()
{
ImageView title = new ImageView(this); //this extends activity
title.setBackgroundResource(R.drawable.this_text);
...
}
This I have done before and each time I do it I experience the same thing:
if i just dragged my this_text.png into the Project Explorer in eclipse, the word "this_text" will be underlined in red, and not allow me to compile or proceed. I try to F5 (refresh) the project. I try refreshing the image foldr, the src, the actual java...etc! I try closing and opening eclipse but nothing really works. So I quit for afew minutes and work elsewhere in my application, and eventually this error goes away!
Well I'm impatient today-- And I'd rather know how to solve this incase I need to do any quick programming! So does anyone know what to do?
I'm using eclipse Version: Indigo Service Release 1.
Thanks- Ethan
R files for android are generated. Rather than refreshing you should be able to CLEAN the project which should fix your issue. You could even just delete the error from the problems list which would allow for compilation to start and for the generated R file to be created. Or you could make a change to an xml which would also allow for the file to be regened. As always make sure you are up to date on your tool chain.

Android: Any idea why cannot load drawable object?

There are several custom-made graphic objects (.png files) included in the project inside res/drawable map.
All elements are normally loaded and displayed in the user interface except two icons and so far haven't figured out what causes the problem.
The code which doesn't affect the user interface as it should is the following:
if (settings.isMute()) {
muteIcon.setIconImage(R.drawable.ic_volume_off_small);
} else {
muteIcon.setIconImage(R.drawable.ic_volume_small);
}
In both cases there is only ic_volume_small displayed on the screen and the Variables window in the IDE displays the following:
R.drawable.ic_volume_small = Class not
loaded : net.client.android.R$drawable
R.drawable.ic_volume_of_small = Class
not loaded :
net.client.android.R$drawable
The method (member of IconImage class) which should change the icon image is the following:
public void setIconImage(int imageFromResources) {
iconImage = BitmapFactory.decodeResource(getResources(), imageFromResources);
iconWidth = iconImage.getWidth();
iconHeight = iconImage.getHeight();
invalidate();
}
Does anyone know what could cause the described problem?
Thanks!
Assuming what you want is the drawable from the Android framework and not your own, you'd want to use:
android.R.drawable.ic_volumne_off_small
rather than
R.drawable.ic_volume_off_small
(notice the android prefix).
If you recently added file, please refresh (press F5) the drawable directory and R is going to be generated again. Once R is regenerated, you can use the newly added resource.

Categories

Resources