user298519 user298519 - 2 months ago 16
Android Question

Android Studio: Making a random number generator app, random number doesn't display?

I have EditViews to prompt the user for a min and max. The data entered is passed into an Activity function and a random number is generated. That random number is passed onto another Activity to be displayed. When I enter a min number and a max number and then click the button that says generate, I get directed to a blank screen. However, I'm expecting randValue to be displayed.

MainActivity.java

public class MainActivity extends AppCompatActivity {
public final static String RAND_NUM = "com.example.random.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void generateNum(View view) {
Intent intent = new Intent(this, DisplayNumActivity.class);

EditText minInput = (EditText) findViewById(R.id.min_num);
EditText maxInput = (EditText) findViewById(R.id.max_num);

int minNum = Integer.parseInt(minInput.getText().toString());
int maxNum = Integer.parseInt(maxInput.getText().toString());
Random rand = new Random();
int randNum = rand.nextInt(maxNum - minNum) + minNum;
intent.putExtra(RAND_NUM, randNum);
startActivity(intent);

}
}


DisplayNumActivity.java

public class DisplayNumActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_num);

Intent intent = getIntent();
String randValue = intent.getStringExtra(MainActivity.RAND_NUM);

TextView textView = new TextView(this);
textView.setTextSize(30);
textView.setText(randValue);

ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_num);
layout.addView(textView);

}
}


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.random.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Min number"
android:id="@+id/textView"
android:layout_marginTop="40dp"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2" />

<EditText
android:id="@+id/min_num"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="min"
android:inputType="number"
android:layout_marginTop="32dp"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Max number"
android:id="@+id/textView2"
android:layout_marginTop="43dp"
android:layout_below="@+id/min_num"
android:layout_centerHorizontal="true" />

<EditText
android:id="@+id/max_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="max"
android:inputType="number"
android:minWidth="500dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:onClick="generateNum"
android:id="@+id/button"
android:layout_below="@+id/max_num"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp" />



Answer

OK, two things:

1) In DisplayNumActivity, change

String randValue = intent.getStringExtra(MainActivity.RAND_NUM);

to this:

int randValue = intent.getIntExtra(MainActivity.RAND_NUM, 0);

Here we are reading in an int value from the intent instead of a String. In your main activity you saved an int here, not a String, the String here is just the "key" in the key-value pair. The zero is just a default in case it can't find your int.

2) To display the number you have to first change that int back to a String in order to set it in a TextView and then we display it to your relative layout like this:

String stringRandValue = Integer.toString(randValue);
View relativeLayout =  findViewById(R.id.activity_display_num);;
TextView textView = new TextView(this);
textView.setTextSize(30);
textView.setText(stringRandValue);
((RelativeLayout)relativeLayout).addView(textView);

That should work.