Dynamically change the name of the resource file to be used?


It is possible to do something like:

if (colorScheme == 1) button.setBackgroundResource(R.drawable.button + "_1")

in order to use R.drawable.button_1 as the resource for this button in color scheme 1, if there are files named button_1.png, button_2.png, button_3.png in drawable folder. (dynamically use different resource file for the same UI element based on the color scheme being used?)



I've done something simular using getIdentifier():

int resId = context.getResources().getIdentifier("button_1","drawable",context.getPackageName()); button.setBackgroundResource(resId);


In order for it to be dynamic, there will be some code required. You can set up your layout in xml Like this:

<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" />

And then reference it in your code like this:

int resId = context.getResources().getIdentifier("button_1","drawable",context.getPackageName()); Button button = (Button view.findViewById(R.id.button1); button.setBackgroundResource(resId);

I haven't tested this, but this should give you the idea.


Put R.drawable.button_n in an array int res[] and then call them by button.setBackgroundResource(res[i])


  • SQL query to get count of rows with more than one row in another table
  • Error in dataframe *tmp* replacement has x data has y
  • Resizing bitmap is cropping instead of scaling in View
  • Exponential fitting with R
  • ImageSwitcher is not showing outAnimation
  • Android get resource / Buffer reader problems
  • GIF image works on emulator but not in real device?
  • Multiply textviews in a listview
  • How to dynamically setting relative layout background images?
  • Adding ProgressBar to DetailsOverviewRow on Android TV
  • Colour resource ID returning wrong value
  • problem with code to combine two images in android?
  • GCM registration works for debug APK but not release APK
  • write text on image and show it to a imageview
  • android Navigation Bar hiding and persantage of usable screen overlap
  • Why use database factory in asp.net mvc?
  • How do I configure context broker accept post requests from my remote sensor?
  • C: Incompatible pointer type initializing
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • copying resource to sdcard gives a damaged file in android
  • C++ Partial template specialization - design simplification
  • How to use an array of arrays with array_map(…) in PHP?
  • Paperclip, set path outside of rails root folder
  • Initializer list vs. initialization method
  • Change JButton Shape while respecting Look And Feel
  • Nant, Vault & Windows Integrated Authentication
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • Javascript Callbacks with Object constructor
  • Weird JavaScript statement, what does it mean?
  • jQuery tmpl and DataLink beta
  • How to get next/previous record number?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Python: how to group similar lists together in a list of lists?
  • Proper folder structure for lots of source files
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • Append folder name and increment by 1 using batch script
  • Django query for large number of relationships
  • Unable to use reactive element in my shiny app