0

I have a TextView in ClassA.java :

TextView txt1 = (TextView) findViewById(R.id.txt1);

I want to use the value of this TextView in ClassB.java How can i do this?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • You're going to need to give us more info. Can you post ClassA and ClassB (or the relevant parts)? one way is to put a getTextView() method in ClassA and call it from ClassB – Brian Pipa Dec 05 '16 at 20:09
  • you need to share strings between activities – ΦXocę 웃 Пepeúpa ツ Dec 05 '16 at 20:10
  • Possible duplicate of [Passing strings between activities in android](http://stackoverflow.com/questions/8748444/passing-strings-between-activities-in-android) – ΦXocę 웃 Пepeúpa ツ Dec 05 '16 at 20:13
  • 2
    Possible duplicate of [How do I pass data between activities on Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android) – baldguy Dec 05 '16 at 20:25

1 Answers1

0

To get String from TextView:

TextView txtView = (TextView) findViewById(R.id.your_id_here);
String text = txtView.getText().toString();

Option A If you want to pass data through activities you can use putExtra(). Syntax (ClassA):

 @Override onCreate(Bundle savedInstanceState){
     ...
     Intent intent = new Intent(this, ClassB.class);   
     intent.putExtra("value", "Your text from TextView" );

Then, you have to get it in ClassB. Syntax:

Intent source = getIntent();
String textFromClassA = source.getStringExtra("value");

Option B Initialize string as static and public.

public static String ...

Then you can easly access data from this variable.

h3wro
  • 62
  • 1
  • 13