Replace drawable in res/drawable

All your problems with Audio, Video and Images.

Replace drawable in res/drawable

Postby sterdroids » Thu Aug 05, 2010 7:41 am

I have an drawable in my res/drawable folder named drawableOne.
I use the following to return a drawable:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Drawable returnedDrawable = getPackageManager().getInstalledPackages(0).applicationInfo.loadIcon(getPackageManager())
  2.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

This part works fine. I get the returned drawable (which is the icon of an app). Now i want to replace "drawableOne" with the "returnedDrawable" but i don't know how. Thanks for you help!
sterdroids
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri Jul 23, 2010 6:17 am

Top

Re: Replace drawable in res/drawable

Postby blundell » Thu Aug 05, 2010 1:35 pm

Use your drawable ID
Code: Select all
getResources().getDrawable(R.id.drawableOne);

getDrawable
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Replace drawable in res/drawable

Postby sterdroids » Thu Aug 05, 2010 5:40 pm

Thanks for the response but thats not exactly what i'm after. I know how to get the Drawables. So i have two drawable objects in my code. I want to make one replace the other one in the res/drawable dir.

Do i have to convert the returnedDrawablt to a bitmap and save it over the original file in the res/drawable dir? Or something like that. Or is there an easy function i'm missing that will copy a drawable to a different one and save it correctly to res/drawable. Thanks.
sterdroids
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri Jul 23, 2010 6:17 am

Re: Replace drawable in res/drawable

Postby blundell » Thu Aug 05, 2010 7:29 pm

Why would you want to do this?

I don't think it's possible as your drawables are referenced by your R file that is created at deployment time.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Replace drawable in res/drawable

Postby sterdroids » Thu Aug 05, 2010 7:51 pm

Yeah after looking around i doubt its gonna work. I'm kind of new to android so let me explain what i'm trying to do:

I have a widget w/ a few buttons and those buttons have backgrounds set to res/drawable (android:background="@drawable/i1") in the XML that defines the widget layout.

I want the user to be able to change these images (aka button backgrounds). So my hope was to keep the reference in the XML to android:background="@drawable/i1" and just change the picture i1 to the picture they selected but keep the name i1.

I really appreciate the help.
sterdroids
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri Jul 23, 2010 6:17 am

Re: Replace drawable in res/drawable

Postby blundell » Thu Aug 05, 2010 7:56 pm

You would merely change the reference, the XML will be the default when your app is laoded and then this can be overwritten by the user preference:
Code: Select all
if(userHasSelectedADiffBackground = true) {
  setBackground(userBAckgroundIdFromPreferences);
}


Code: Select all
private void setBackground(int backgroundId) {
                View counterView = findViewById(R.id.mainCounter);             
                counterView.setBackgroundResource(backgroundId);
        }
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 8 guests