How to set text to textview?

I ran into the problem of setting text on a TextView in android, which is my code:

 public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.button1); final TextView text = (TextView) findViewById(R.id.textView1); final EditText input = (EditText) findViewById(R.id.editText1); final String string = input.getText(); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { text.setText(string); } }); } } 

if i write

  final Editable string = input.getText(); 

then it works ..... !!!!

Now I want to send the EditText data to the following Activity as follows:

 public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.button1); final TextView text = (TextView) findViewById(R.id.textView1); final EditText input = (EditText) findViewById(R.id.editText1); final Editable string = input.getText(); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(Main.this, Second.class); intent.putExtra("thetext", string); startActivity(intent); } }); } } 

and in the Second.java class Second.java I get a StringExtra as follows:

 public class Second extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); TextView text = (TextView) findViewById(R.id.textView2); String string = getIntent().getExtras().getString("thetext", "not found"); text.setText(string); /// Here the text is not shown but the default message "not found" is set to `TextView` } } 

Please give me a way to get started developing.

+6
source share
6 answers

I think the problem is that you are actually putting “Editable” in the intention, not in String. Although they are close, they are not the same thing. If you are toString (), your Editable to get a String object and put this in the intent, you can get it back using getString, as you do.

+3
source

The problem should be, you are sending Editable , not String . Try the following:

 final String string = input.getText().toString(); 
+5
source
  private TextView mTextView; private String mString; mTextView = (TextView) findViewById(R.id.tv); mString = "hello everyone ! how ru?"; mTextView.setText(mString); 
+2
source

Try something like this:

 public class Main extends Activity { EditText input; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView text = (TextView) findViewById(R.id.textView1); input = (EditText) findViewById(R.id.editText1); Button button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(Main.this, Second.class); intent.putExtra("thetext", input.getText().toString()); startActivity(intent); } }); } } 

(Hint: The easiest way to submit code is to insert your code, select it, and use crtl + k to indent / format it.)

+1
source

According to android docs, the name of the line placed in optional components should contain the package prefix ... that is, som.arshay.dev.thetext Secondly, getExtras () returns a package that is not what you added. You need getStringExtra( name )

0
source

In this line use

 final String string = input.getText().toString(); 

instead

 final String string = input.getText(); 
0
source

Source: https://habr.com/ru/post/914636/


All Articles