Let's say I have class MainActivity which has main_activity as its layout. I have a String in the class and I want to set my other layout's textview (secondactivity) with the value of that string. How can I do that?
Asked
Active
Viewed 29 times
-1
-
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) – PPartisan Aug 15 '17 at 11:54
-
I don't want to go into another Activity , the link you sent has no relevance – edgelord Aug 15 '17 at 12:05
-
Your question states you have another layout, and you put secondactivity in brackets. – PPartisan Aug 15 '17 at 12:23
-
Only one Activity is active at once, why do you need to change text you can't see? Intents/SharedPreferences/Database/Files are the correct answers to your question, regardless of how the second Activity is started – OneCricketeer Aug 15 '17 at 12:52
1 Answers
0
If you just want to use a String that's in another Java class, you have to create a static String in the first class.
In your MainActivity:
public class MainActivity {
public static String yourString = "Your string";
}
Then were you want to set the text of the TextView:
yourTextView.setText(MainActivity.yourString);

Jasper Barzilaij
- 11
- 3