-2

Ill try and be specific as I can. This is my first post/question.

  1. No this is not for a school or work related project.
  2. I have researched and tried a number of things I saw with little results.

I am trying to create, on the Main activity, a button that generates a random number and sends the results to a Textview which is adjacent to the button on the same activity. The only thing I want to change is the contents of the TextView.

Here is my Java/Main Activity code:

package com.example.dannykennedyjr.diceroller;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void d4Results(View view) {
    TextView tv1 = (TextView) findViewById(R.id.d4TextView);
    final Random random = new Random();
    int d4Roll = random.nextInt(4)+1;
    tv1.setText(d4Roll);
}

}

This of course doesn't work, and anything I get to work opens up a new TextView and clears the current layout.

And my XML:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:background="@android:color/black"
    android:onClick="d4Results"
    android:text="@string/roll_d4"
    android:textAlignment="center"
    android:textColor="@android:color/holo_red_dark"
    android:textSize="24sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:ems="10"
    android:hint="@string/roll_result"
    android:inputType="textPersonName"
    android:textAlignment="center"
    android:textSize="24sp"
    app:layout_constraintBaseline_toBaselineOf="@+id/button"
    app:layout_constraintHorizontal_bias="0.508"
    app:layout_constraintLeft_toRightOf="@+id/button"
    app:layout_constraintRight_toRightOf="parent" />

Any Help would be greatly appreciated. Thank You!

  • try `tv1.setText(d4Roll + "")` The `setText` *needs* a string and can't take an integer. My suggestion will concatenate `d4Roll` with the empty string, making itself a string. – Yonah Karp Apr 04 '17 at 19:22
  • @ImmersionULTD *"needs a string"* is a bit misleading. [See my answer here](http://stackoverflow.com/questions/19883815/settext-fails-to-show-a-number-as-text-in-a-textview/19883845#19883845) but, yes, needs a string for this purpose – codeMagic Apr 04 '17 at 19:26
  • I just noticed that my R.id.d4TextView didn't match the android:id="@+id/textview"I updated that, it runs in the emulator and then crashes when I click on the D4 button. – James Kennedy Jr Apr 04 '17 at 19:31
  • Thank you for the comments Ill try some toString tweaks – James Kennedy Jr Apr 04 '17 at 19:32
  • And a little explanation, why you'd need a string here: Because the TextView.setText(int) method, that also exists, is for passing string ids to the TextView (e.g. R.string.app_name). So if you pass it an int, that is not the id of a string resource it gets confused - perhaps even crashes. – Ridcully Apr 04 '17 at 19:35
  • I tried some tweaks and all of the following work: tv1.setText(d4Roll + "");... tv1.setText(String.valueOf(d4Roll));... and tv.setText(Integer.toString(d4Roll)); – James Kennedy Jr Apr 04 '17 at 19:42

3 Answers3

2

I recently build an Android guess answer game.... here how I placed random numbers in TextView.

int random1 = rand.nextInt(100) + 1; //generate random numbers from 1-100

n1.setText(Integer.toString(random1)); //n1 is a TextView

Hope this helps you.

Omore
  • 614
  • 6
  • 18
1

This link will help you as you want :

Generate Random Number

set int in TextView like this :

tv1.setText(String.valueOf(d4Roll));
Community
  • 1
  • 1
Narendra Sorathiya
  • 3,770
  • 2
  • 34
  • 37
1

You are trying to set an integer as a string. You need to convert it first. As per your example:

public void d4Results(View view) {
   TextView tv1 = (TextView) findViewById(R.id.d4TextView);
   final Random random = new Random();
   int d4Roll = random.nextInt(4)+1;
   tv1.setText(Integer.ToString(d4Roll));
}
ruibar
  • 21
  • 1