1

I have a textview, the xml file is

<TextView
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:fontFamily="sans-serif"
        android:lineSpacingExtra="30sp"
        android:text="MyText"
        android:textColor="?android:attr/colorActivatedHighlight"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.22000003" />

There is no ID of this textview so I can't use it in MainActivity.java, I just started Android Studio(I worked with Python so far) what am I missing?

I have also a button and it has an id.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
GLHF
  • 3,835
  • 10
  • 38
  • 83
  • Do you need to define *id* to *Text View* ? Or you need to use it in *Activity* ? *Elaborate* more. – Jay Rathod Jun 22 '17 at 11:51
  • This textview was the default one with "Hello World!" So I thought it must have an ID by default. – GLHF Jun 22 '17 at 11:53
  • You need to define id manually as *android:id="@+id/tvTest"* for your *Text View*. *tvTest* is an identifier of your *Text View* which you will use in your *Activity* to bind some data to it. – Jay Rathod Jun 22 '17 at 11:55

2 Answers2

1

Just add:

<TextView
    android:id="@+id/myTextView"
    ... (rest of lines)
    app:layout_constraintVertical_bias="0.22000003" />

myTextView will be the identifier you choose

Burnie777
  • 395
  • 5
  • 18
  • 1
    But why it doesn't have an id by default anyways? – GLHF Jun 22 '17 at 11:53
  • 2
    In every item i ever added, I had to add the id field... Another option is to add it in the Design properties, which will add it to the xml. Other than that there are many cases a ID is not needed, since you would not reference it from anywhere in the Activity/Fragment – Burnie777 Jun 22 '17 at 11:56
0

Just keep in mind this:

When creating an XML layout file you must specify the Activity name value for the
tools:context like below:

tools:context=".views.QuestionActivity"

This will allow you to retrieve all the ids from the XML layout file in the specified activity.

Also if you have checked that everything is correct but still can't get the ids just restart the IDE.

Make also sure that in your "AndroidManifest.xml" file you have created the activity and assigned the correct name.

Sergio
  • 61
  • 1
  • 7