Android: integer from xml resource

Yes it is possible, it would look like this: Create an xml resources file in the folder /res/values/ called integers.xml. You are free to give it any name as you want, but choose one that is obvious. In that resources file, create your integer values. Your file then looks something like that: <?xml version=”1.0″ encoding=”utf-8″?> … Read more

How to fix: android.app.RemoteServiceException: Bad notification posted from package *: Couldn’t create icon: StatusBarIcon

What was happening was, I was including the integer reference to the icon in the PendingIntent bundle, and that integer was later being referenced while being posted to the NotificationManager. In between getting the integer reference and the pending intent going off, the app was updated and all of the drawable references changed. The integer … Read more

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

format statement in a string resource file

You do not need to use formatted=”false” in your XML. You just need to use fully qualified string format markers – %[POSITION]$[TYPE] (where [POSITION] is the attribute position and [TYPE] is the variable type), rather than the short versions, for example %s or %d. Quote from Android Docs: String Formatting and Styling: <string name=”welcome_messages”>Hello, %1$s! … Read more

Android, getting resource ID from string?

@EboMike: I didn’t know that Resources.getIdentifier() existed. In my projects I used the following code to do that: public static int getResId(String resName, Class<?> c) { try { Field idField = c.getDeclaredField(resName); return idField.getInt(idField); } catch (Exception e) { e.printStackTrace(); return -1; } } It would be used like this for getting the value of … 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

Get color value programmatically when it’s a reference (theme)

This should do the job: TypedValue typedValue = new TypedValue(); Theme theme = context.getTheme(); theme.resolveAttribute(R.attr.theme_color, typedValue, true); @ColorInt int color = typedValue.data; Also make sure to apply the theme to your Activity before calling this code. Either use: android:theme=”@style/Theme.BlueTheme” in your manifest or call (before you call setContentView(int)): setTheme(R.style.Theme_BlueTheme) in onCreate(). I’ve tested it with … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)