How to display a textView of an Activity in another Activity

-1

    

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1.0">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Valor do Alcool"
            android:layout_weight=".5"
            android:textColor="@android:color/black"
            android:textSize="12sp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight=".5"
            android:text="Valor da Gasolina"
            android:textSize="12sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1.0">
        <EditText
            android:id="@+id/txtAlcool"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:inputType="numberDecimal"
            android:maxLength="7"
            android:textSize="20sp" />
        <EditText
            android:id="@+id/txtGasolina"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:inputType="numberDecimal"
            android:maxLength="4"
            android:textSize="20sp" />
    </LinearLayout>

    <Button
        android:id="@+id/btnCalcular"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:text="Enviar" />

    <TextView
        android:id="@+id/tvResultado"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />
</LinearLayout>

    

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1.0">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Valor do Alcool"
            android:layout_weight=".5"
            android:textColor="@android:color/black"
            android:textSize="12sp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight=".5"
            android:text="Valor da Gasolina"
            android:textSize="12sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1.0">
        <EditText
            android:id="@+id/txtAlcool"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:inputType="numberDecimal"
            android:maxLength="7"
            android:textSize="20sp" />
        <EditText
            android:id="@+id/txtGasolina"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:inputType="numberDecimal"
            android:maxLength="4"
            android:textSize="20sp" />
    </LinearLayout>

    <Button
        android:id="@+id/btnCalcular"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:text="Enviar" />

    <TextView
        android:id="@+id/tvResultado"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />
</LinearLayout>

I would like the result of tvResultado to show in another actvity.

    
asked by anonymous 21.06.2018 / 16:11

1 answer

1

There are some ways to pass information between two Activities.

One of these ways is to use the Intent itself that invokes the second Activity :

Intent intent = new Intent(this, SegundaActivity.class);
intent.putExtra("chave", "texto");
startActivity(it);

To recover on your second Activity would be:

@Override
protected void onCreate(Bundle saveInstancestate) {
    super.onCreate(saveInstanceState);
    setContentView(R.layout.main_activity);

    //Pega a intent que disparou esta Activity
    Intent intent = getIntent();

    //Recuperei o texto
    String texto = intent.getStringExtra("chave");
}

Another way would be to use SharedPreferences . You will need to define the name of the file to be shared:

public static final String PREFS_NAME = "shareData";

In your first Activity you save a value like this:

SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
sharedPreferences.edit().putString("chave", texto).commit();

In the second Activity retrieve the information like this:

SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
String texto = sharedPreferences.getString("chave");

PS : It does not matter how you want to store the value of PREFS_NAME, but it has to be the same in both Activities.

    
21.06.2018 / 16:21