The Newbie The Newbie - 1 year ago 322
Android Question

Android - Expected Resource of type ID

I have this code

final static int TITLE_ID = 1;
final static int REVIEW_ID = 2;

Now, I want to create a new layout in my main class

public View createContent() {
// create linear layout for the entire view
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,

// create TextView for the title
TextView titleView = new TextView(this);

StarView sv = new StarView(this);

return layout;

But when ever I call TITLE_ID and REVIEW_ID, it gives me an error

Supplying the wrong type of resource identifier.

For example, when calling Resources.getString(int id), you should be passing R.string.something, not R.drawable.something.

Passing the wrong constant to a method which expects one of a specific set of constants. For example, when calling View#setLayoutDirection, the parameter must be android.view.View.LAYOUT_DIRECTION_LTR or android.view.View.LAYOUT_DIRECTION_RTL.

I don't have any problem running this code. I'm just wondering why it gives me an error. Any idea?

Answer Source

This is not a compiler error. It is just editor validation error(lint warning) as this is not a common way to deal with Ids.

So if your app supporting API 17 and higher,

you can call View.generateViewId as




and for API<17

  1. open your project's res/values/ folder
  2. create an xml file called ids.xml

with the following content:

<?xml version="1.0" encoding="utf-8"?>
    <item name="titleId" type="id" />
    <item name="svId" type="id" />

then in your code,




And to disable this warning (If you want)

In Android Studio click on light bulb on line with this 'error'. And select Disable inspection in first submenu.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download