Storing R.drawable IDs in XML array

You use a typed array in arrays.xml file within your /res/values folder that looks like this: <?xml version=”1.0″ encoding=”utf-8″?> <resources> <integer-array name=”random_imgs”> <item>@drawable/car_01</item> <item>@drawable/balloon_random_02</item> <item>@drawable/dog_03</item> </integer-array> </resources> Then in your activity, access them like so: TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs); // get resource ID by index, use 0 as default to set null resource imgs.getResourceId(i, 0) … Read more

Is it possible to have placeholders in strings.xml for runtime values?

Formatting and Styling Yes, see the following from String Resources: Formatting and Styling If you need to format your strings using String.format(String, Object…), then you can do so by putting your format arguments in the string resource. For example, with the following resource: <string name=”welcome_messages”>Hello, %1$s! You have %2$d new messages.</string> In this example, the … Read more

How can I assign an ID to a view programmatically?

Android id overview An Android id is an integer commonly used to identify views; this id can be assigned via XML (when possible) and via code (programmatically.) The id is most useful for getting references for XML-defined Views generated by an Inflater (such as by using setContentView.) Assign id via XML Add an attribute of … Read more

Passing image from one activity another activity

There are 3 Solutions to solve this issue. 1) First Convert Image into Byte Array and then pass into Intent and in next activity get byte array from Bundle and Convert into Image(Bitmap) and set into ImageView. Convert Bitmap to Byte Array:- Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); … Read more

How can I get a resource content from a static context?

Create a subclass of Application, for instance public class App extends Application { Set the android:name attribute of your <application> tag in the AndroidManifest.xml to point to your new class, e.g. android:name=”.App” In the onCreate() method of your app instance, save your context (e.g. this) to a static field named mContext and create a static … Read more

getColor(int id) deprecated on Android 6.0 Marshmallow (API 23)

Starting from Android Support Library 23, a new getColor() method has been added to ContextCompat. Its description from the official JavaDoc: Returns a color associated with a particular resource ID Starting in M, the returned color will be styled for the specified Context’s theme. So, just call: ContextCompat.getColor(context, R.color.your_color); You can check the ContextCompat.getColor() source … Read more