Nooby Coder Nooby Coder - 2 months ago 7
Java Question

TextView attribute in android for displaying intergers?

public void onClick(View v) {
if (v == button1){
counter++;
textView2.setText(Integer.toString(counter));
textView2.setText(counter);
}

}


I have tried to create a program that counts the number of clicks on a button, but for some reason it is not displaying it. Here is my textview component.

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="180dp"
android:layout_alignParentTop="true"
android:layout_above="@+id/button1"
android:layout_toRightOf="@+id/textView1"
android:layout_centerHorizontal="true"
android:textColor="#000000"/>


I can't seem to find an attribute for display the number of clicks. Is this because it interprets it as an interger instead of a string?

Answer
textView2.setText(String.valueOf(counter));

This takes your Integer and converts it to its String value, and sets it to your TextView.