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 ツ
  • 43,054
  • 16
  • 58
  • 83

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
  • 52
  • 1
  • 12