1

For the app I am building it will randomly select a restaurant (randomResturantName) from my array and print it with toast.

How would I make it so that the user can click on the popup to go to another java page where I will have the chosen restaurant name and other info?

What my toast prints out

My code:

    private void pickRestaurant(int checkedRadioButtonId) {
    Random r = new Random();
    int randomNumber = -1;

    switch (checkedRadioButtonId) {
        case R.id.Asian:
            randomNumber = r.nextInt(6-1)+1 ;
            //1-6

            break;
        case R.id.middle_eastern:
            randomNumber = r.nextInt(9-7) +7  ;
            break;

        case R.id.Pizza:
            randomNumber = r.nextInt(11) + 6;
            break;

    }

    if (DEBUG) Log.i(TAG, "Random number to pick restaurant is: " + randomNumber);

    String randomRestaurantName = resArray.get(randomNumber).getName();
    Toast toast = null;

    if (randomNumber < 0) {
        toast = Toast.makeText(myContext, "Select from one of the following:",
      Toast.LENGTH_SHORT);

    } else {
        toast = Toast.makeText(myContext, "Today you will eat at "
                + randomRestaurantName , Toast.LENGTH_LONG);
    }

            toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();

    Intent intent = new Intent(MainActivity.this,chaCha.class);
    intent.putExtra("KEY",randomRestaurantName);
    startActivity(intent);
}

chaCha.java

public class chaCha {

private Intent getIntent;
Bundle bundle = getIntent.getExtras();
String randomRestaurantName = bundle.getString("KEY");

}

Miki
  • 33
  • 4
  • if you would like user to interact with the message, then you should not show the message using toast, but instead of a dialog (e.g.alertdialog). – CodePlay Mar 10 '17 at 06:09
  • From your discussion below, I recommend you to take a free course that will help you start Android App Development from [New Boston](https://thenewboston.com/videos.php?cat=6) and if you are really interested in developing Android apps, you can follow [Udacity's Android Beginner's course designed by Google.](https://cn.udacity.com/course/android-development-for-beginners--ud837) (also free). – Talha Mar 10 '17 at 06:56

3 Answers3

0

make intent and pass the data along it like:

Intent intent = new Intent(thisClass.this,NextClass.class);
intent.putExtra("KEY",RestaurentName);
startActivity(intent);

in next class recieve intent and get extras like:

Bundle bundle = getIntent.getExtras();
String RestaurentName = bundle.getString("KEY");
AwaisMajeed
  • 2,254
  • 2
  • 10
  • 25
  • it just says "Food selector has stopped" – Miki Mar 10 '17 at 06:27
  • currently my main is already extended "public class MainActivity extends Activity {" – Miki Mar 10 '17 at 06:28
  • check this Intent intent = new Intent(MainActivity.this,SignupActivity.class); intent.putExtra("KEY","slkjf"); startActivity(intent); now in next activity Bundle b = getIntent().getExtras(); String s = b.getString("KEY"); – AwaisMajeed Mar 10 '17 at 06:30
  • android.content.ActivityNotFoundException: Unable to find explicit activity class {edu.temple.tuyen.templefoodselector/edu.temple.tuyen.templefoodselector.chaCha}; have you declared this activity in your AndroidManifest.xml? – Miki Mar 10 '17 at 06:33
  • Sorry, I am very new to Android Studios – Miki Mar 10 '17 at 06:34
  • have you declared your next activity in the AndroidManifest.xml???? like: – AwaisMajeed Mar 10 '17 at 06:36
0

Use Notifications, you can not interact with Toast. Then set Intent (wrapped in PendingIntent) to that Notification object, and pass Restaurant name and other info in Intent extras.

Building a notification is as simple as (from Google Dev Site):

NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon) // set icon
    .setContentTitle("Random Restaurant")
    .setContentText(randomRestaurantName);

NotificationManager mNotifyMgr =
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());

How to handle click on notification, here SO has already an answer.

You can also use Dialog but you will have to write more code and handle more stuffs like, listeners to Dialog button, or in case of custom Dialog, layouts etc. Here is official tutorial doc for Dialogs.

Reposting my comment to emphasize the advice:

From your discussion above, I recommend you to take a free course that will help you start Android App Development from New Boston and if you are really interested in developing Android apps, you can follow Udacity's Android Beginner's course designed by Google. (also free).

Community
  • 1
  • 1
Talha
  • 903
  • 8
  • 31
0

You cannot click on Toast message. Better use POPUP on click of radio button instead of TOAST.

MUKUND U
  • 44
  • 6