6

I'm trying to use Java (not XML) to add system drawable to a layout and I am able to do this in xml part but can't figured out how do I add system drawable in java ?

in Xml

android:Drawable="@android:drawable/cursor"

how to program in java

Resources res = getResources();
Drawable img = res.getDrawable(R.drawable.cursor);
Konrad Krakowiak
  • 11,733
  • 10
  • 53
  • 44
Ankita Bansal
  • 315
  • 1
  • 5
  • 10

2 Answers2

20

Just do it

Drawable img = res.getDrawable(android.R.drawable.cursor);

For API 22 and above have a look at: https://stackoverflow.com/a/29146895/1306012

Bruno Bieri
  • 7,754
  • 10
  • 55
  • 79
M D
  • 46,860
  • 8
  • 87
  • 108
0

In your code, replace R.drawable.cursor with android.R.drawable.cursor.

For API 22 and above:

ContextCompat.getDrawable(context, android.R.drawable.cursor)
James
  • 2,316
  • 19
  • 26