0

This my activity_main.xml

<EditText
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:hint="@string/str3"
    android:id="@+id/line2"
    android:layout_below="@id/line1" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_toRightOf="@id/line2"
    android:layout_below="@id/line1"
    android:text="@string/str4"
    android:onClick="method2"/>

I want to display the text entered in another activity but not in the way as done on the Activity tutorial (where another TextView is created in second activity and text set there only).

This is my display2.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display2);

    Intent intent2 = getIntent();

This is in my activity_display2.xml

<TextView
    android:id="@+id/pwdisplay"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textStyle="bold|italic"
    android:padding="30dp"
    />

and this is what I am trying to do (in main_activity.java)

public void method2(View view) {
    Intent intent2 = new Intent(this, display2.class);
    EditText enteredpw = (EditText) findViewById(R.id.line2);
    String pw = enteredpw.getText().toString();
    findViewById(R.id.pwdisplay).setText(pw);  <--Error here
    startActivity(intent2);
}

I am trying to set the text for pwdisplay TextView in main_activity.java itself, but got an error "cannot resolve method setText(java.lang.String)".

OR

is there any other way in which the text entered in EditText @id/line2 by the user, can be displayed by activity_display2.xml?

11
  • Error here does not say anything. Please post exact stack-trace. Commented Jan 27, 2017 at 15:16
  • put pw in intent object and fetch pw form intent2 and initialize text view there and set data using setText Commented Jan 27, 2017 at 15:17
  • Please describe what the user will do and what happens in your app. In particular why don't you want to do what the link you gave describes? How does your objective differ? Commented Jan 27, 2017 at 15:25
  • @Code-Apprentice My objective is similar to that but the approach is different ( I was just trying to do all layout/views stuff in xml and not in java ) Commented Jan 27, 2017 at 16:01
  • @Akhil how is your objective different? What do you want your app to do? Commented Jan 27, 2017 at 16:03

7 Answers 7

4

findViewById(R.id.pwdisplay) returns View. View does not have setText method. You need to cast:

((TextView)findViewById(R.id.pwdisplay)).setText(pw);
Sign up to request clarification or add additional context in comments.

3 Comments

pwdisplay belongs to activity display2 not to main_activity
@PavneetSingh you cannot set the text in another activity without starting the other activity first
What you are saying is right but it works only when i do it along with what Micha , Adan or Bram have suggested .
1

You can't manipulate widgets in one Activity from another Activity. To do what I think you're trying to do, you have to pass the text entered via the Intent. E.g.:

public void method2(View view) {
    Intent intent2 = new Intent(this, display2.class);
    EditText enteredpw = (EditText) findViewById(R.id.line2);
    String pw = enteredpw.getText().toString();
    intent2.putExtra("KEY", pw);
    startActivity(intent2);
}

//display2.java
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display2);

    Intent intent2 = getIntent();
    String pw = intent2.getStringExtra("KEY");
    findViewById(R.id.pwdisplay).setText(pw);

2 Comments

Oh yeah, what @bram-jongebloet says is necessary too, you'll need to cast findViewById(R.id.pwdisplay) to TextView.
Thanks for the help . This worked perfect for me when findViewById(R.id.pwdisplay) was cast to TextView :D
0

Your being inconsistent with your code. This solves your problem because u have to cast your findViewById to a TextView otherways the compiler won't know your dealing with a TextView.

    Intent intent2 = new Intent(this, display2.class);
    EditText enteredpw = (EditText) findViewById(R.id.line2);
    String pw = enteredpw.getText().toString();
    ((TextView)findViewById(R.id.text)).setText(pw);  
    startActivity(intent2);

But then again your doing everything like this:

    EditText enteredpw = (EditText) findViewById(R.id.line2);

You have to choose to use the short version like this:

    ((TextView)findViewById(R.id.text)).setText(pw);

or

    TextView textView = (TextView)findViewById(R.id.text);
    textView.setText(pw);

But now I read u want to sent the text the user entered over to the second activity. This can be done by providing it to the intent with the call:

  intent2.putExtra("text",pws);

and getting it in the new activity with

    Bundle extras = getIntent().getExtras(); 
    String text;

    if (extras != null) {
      text = extras.getString("text");
      // set the text to the textview.
      ((TextView)findViewById(R.id.text)).setText(text);
    }

Comments

0

Welcome to Android Development

You can't set the Text of a TextView from another activity like that, there´s a few things you can do but im gonna explain to you the most easy for you.

In MainActivity put this:

public void method2(View view) {
    Intent intent2 = new Intent(this, display2.class);
    EditText enteredpw = (EditText) findViewById(R.id.line2);
    String pw = enteredpw.getText().toString();
    intent2.putExtra("message", pw);
    startActivity(intent2);
}

In your display2 class

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display2);
    String message; 
    message = getIntent().getExtras().getString("message");
    TextView pwdisplay = (TextView) findViewById(R.id.pwdisplay);
    pwdisplay.setText(message);
}

Here's what is happening, imagine the Intent in your MainActivity like a letter, you can write in the letter some things you need in the display2 activity, in this case we have intent2.putExtra("message", pw); there's where we send the String, and in the display2 class we receive this in the line message = getIntent().getExtras().getString("message"); and later we put this string in the TextView pwdisplay.setText(message);.

Comments

0

you cant find the textview with id "pwdisplay" because its not inside the main_activity, so modify your code like below :

// MainActivity code 

public void method2(View view) {
    Intent intent2 = new Intent(this, display2.class);
    EditText enteredpw = (EditText) findViewById(R.id.line2);
    String pw = enteredpw.getText().toString();
    Intent intent2 = new Intent(this, MainActivity.class);
    // passed your entered text to the next activity using the line below:
    // KEY= text_to_display, his value equal the entered text 

   intent2.putExtra("text_to_display","some text here");

    startActivity(intent2);
}

// on display2.java "Activity" do modify it like below code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display2);

    Intent intent2 = getIntent();
    // get the passed text from the given intent
    String pw = intent2.getExtras().getString("text_to_display",null);
    TextView myTxView = (TextView) findViewById(R.id.pwdisplay);
    if(pw != null){
       myTxView.setText(pw);
    }

}

Enjoy!

3 Comments

This is what is done on developer.android.com ( making a new text view in the display2.java But i wanted to make layout in activity_display2.xml and set text through activity_main.java But my mistake was to not do it from activity_display2.java and i didnt cast view to textview
@Akhil the real problem is that you cannot set the text of a TextView from another activity like you are attempting to do.
Yes i understood :)
0
package com.kibjob50.provaspeec;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.speech.RecognizerIntent;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private final int REQ_CODE = 100;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.text);
    ImageView speak = findViewById(R.id.speak);
    speak.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Need to speak");
            try {
                startActivityForResult(intent, REQ_CODE);
            } catch (ActivityNotFoundException a) {
                Toast.makeText(getApplicationContext(),
                        "Sorry your device not supported",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });
}
   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case REQ_CODE: {
            if (resultCode == RESULT_OK && null != data) {
                ArrayList result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                textView.setText(result.get(0));
            }
            break;
        }
    }
}

}

The above example, found on the web, give the same error at statement "textView.setText(result.get(0)); Changing to "textView.setText((CharSequence) result.get(0));" works! I hope this can help you.

Comments

-1

Use butterknife to bind the view:

@Bind(R.id.text_view) TextView myTextView;

and then in your method:

myTextView.setText("Hello!");

1 Comment

Butter knife judo complicates things for an obviously new programmer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.