2

Im a little stuck, how can i get a variable from main activity to be displayed on a second activity?

a code example would be great.

Also a second Problem:

How can i create a function in mainactivty when a button is pressed in a second activity?

This is what i have so far, but when i press the button in the second activity, the app crashes.

the button's function needs to be able to change a variable's value in MainActivity and Run a toast saying it was selected.

Main Activity

//SETTING THE DRINK SIZE BASED ON POPUP BUTTONS
 public int DrinkSize;

public void SetDrinkSize_Small(View view) {

    DrinkSize = 1;
    Toast Small = Toast.makeText(getApplicationContext(),
            "Drink Size Set To Small",
            Toast.LENGTH_SHORT);

    Small.show();
}

public void SetDrinkSize_Medium(View view) {

    DrinkSize = 2;
    Toast Medium = Toast.makeText(getApplicationContext(),
            "Drink Size Set To Medium",
            Toast.LENGTH_SHORT);

    Medium.show();
}


public void SetDrinkSize_Large(View view) {

    DrinkSize = 3;
    Toast Large = Toast.makeText(getApplicationContext(),
            "Drink Size Set To Large",
            Toast.LENGTH_SHORT);

    Large.show();
}

CustomPopUp.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="120dp"
        android:layout_weight="1"
        android:background="@color/Orange"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:onClick="SetDrinkSize_Small">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Small"
            android:textColor="@color/White"
            android:textSize="18dp"
            android:textStyle="bold" />


        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/drop" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="120dp"
        android:layout_weight="1"
        android:background="@color/Green"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:onClick="SetDrinkSize_Medium">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium"
            android:textColor="@color/White"
            android:textSize="18dp"
            android:textStyle="bold" />


        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/drop" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="120dp"
        android:layout_weight="1"
        android:background="@color/Orange"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:onClick="SetDrinkSize_Large">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large"
            android:textColor="@color/White"
            android:textSize="18dp"
            android:textStyle="bold" />


        <ImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/drop" />


    </LinearLayout>
  • Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – medyas Feb 11 '19 at 18:18
  • calling a function in mainactivity when a button is clicked from the second will cause your app to crash as the mainactivity is not visible (in the background and the second in forground). do a research on fragment and see if they can help you achieve your goal – medyas Feb 11 '19 at 18:29

1 Answers1

0

You can easily send and receive data using intents, as you can see in this Android tutorial: Intent tutorial. And in case you want to send some data back to the first one, you can use this post: Sending info back.

P. Vera
  • 315
  • 4
  • 11