Mr. Awellstein
Mr. Awellstein

Reputation: 157

Add new ID on Android Studio

Okay so I'm having issues adding a new ID on Android Studio. On a different IDE I didn't have to do anything and it automatically added a new ID for what ever item or view. When I look around on the internet for a solution I cant find anything. I think the reason why I cant find anything when I search for an answer, is because I'm not using the correct terminology.

If I'm not making any sense, then here are some pictures that should explain what I want
What isn't working and what I am trying to make work on Android Studio:

enter image description here

If the picture isn't explaining my problem, then sorry, I'm new to programming on Android.

Upvotes: 2

Views: 14332

Answers (3)

Boma Halliday
Boma Halliday

Reputation: 1

I just deleted the end slash for that element, "/>", and put it back and the problem was solved.

Upvotes: -1

Slim Sim
Slim Sim

Reputation: 1183

No no no!

Short answer: Every id-declaration (which is what you do in the XML-file) must start with "@+id/"

Longer answer:

You must not add a new class, and you must not eddit R.class either. (I've read somewhere that you should "never" edit that place...)

What you need to do, and what the error message is trying to tell you, is this. In your XML, add id to a element by writing

...
android:id="@+id/<name_of_id>
...

So in your case you could write

    <TextView
        android:text="apples"
        android:id="@+id/main_textview1
        .... >
</RelativeLayout>

Hope this helps!

Upvotes: 3

Mr. Awellstein
Mr. Awellstein

Reputation: 157

Okay guys, I figured it out... So first, you have to go into the R.class and add something like:

public static final class <New ID>{ 
    public static final int <ID Name> = <ID Value>;
}

Image: https://i.sstatic.net/qiGxj.jpg

Then you go to your Layout.xml and do this:

<TextView
    . . .
    android:id="@+<New ID>/<ID Name>"
/>

Image: https://i.sstatic.net/qi3Zx.jpg

Note:
Your IDE will most likely say that the New ID will error when you use it in a view, and will underline it with red, but doing this will not throw any errors at all. Just make sure that you don't have multiple variables with the same ID so things do not get mixed up.

Upvotes: -2

Related Questions