How to display a textView of an Activity in another Activity

Asked

Viewed 548 times

-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 tvResultate to show in other actvity.

  • Please edit the question by placing the code used to call the second Acitivy and to retrieve the text.

1 answer

1

There are some ways to pass information between two Activities.

One of these ways is to use one’s own Intent invoking the second Activity:

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

To recover on your Monday 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 set the file name to be shared:

public static final String PREFS_NAME = "shareData";

In your first Activity you keep a value like this:

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

In the second Activity recover the information thus:

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

PS: No matter how you want to store the PREFS_NAME value but it has to be the same on both Activitys.

  • I’ve done it, it didn’t work.

  • Edit the question and put the code used to call the second Acitivy and to retrieve the text.

  • I noticed an error when using Sharedpreferences, I was missing using the .commit() in the method of putString()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.