0

Excuse me if this is an easy problem. I have been unable to find an answer so far.I have followed this tutorial( https://www.youtube.com/watch?v=fn5OlqQuOCk ) to create a pop up window. While it works as intended I want to change the text when the button is pressed from the java code.

To be more specific, I have a string array and whenever the button is pressed I want it to show a random element of the array. How can I go about doing those things?

My onCreate method is(mundaneLoot is the id of the button):

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button b = (Button)findViewById(R.id.mundaneLoot);


    b.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, Pop.class ));
        }

    });

}

My xml code for the pop up window (which essentially is a new activity) is:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Pop up window"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

I want to edit the android:text="Pop up window" from the java code.

5
  • (TextView)findViewById(R.id.textView).setText("what ever"); Commented Apr 7, 2016 at 13:04
  • pop up window is a dialog ?? Commented Apr 7, 2016 at 13:07
  • @SagarNayak as I've stated in my question I followed a tutorial where I create a pop up as a new activity. Commented Apr 7, 2016 at 13:17
  • So the activity in which you are showing popup is ??? and the activity from which you want to change its text is ???? (please give names of the activity ) Commented Apr 7, 2016 at 13:38
  • 1
    The Activity he is opening in his Intent IS a popup. Commented Apr 7, 2016 at 13:45

3 Answers 3

2

If your pop-up dialog is an Activity, you can pass in a value to that Activity through your Intent. You can do so like this.

Intent intent = new Intent(MainActivity.this, Pop.class);
intent.putExtra("value", randomValueFromArray);
startActivity(intent);

You can then get the value from the Intent in the onCreate() of your Pop class.

Bundle bundle = getIntent().getExtras();
editText.setText(bundle.getString("value"));

If you want to know how to get a random value from an ArrayList, you could use something like this.

String text = list.get((new Random()).nextInt(list.size()));
Sign up to request clarification or add additional context in comments.

Comments

1

Go on the onCreate method of the Pop class and then

TextView popUpMessage = (TextView) findViewById(R.id.textView);
popUpMessage.setText("Example");

Comments

-1

Use editText Like this:

EditText editText;     

In on create bind the editText like this

editText = (EditText) findViewById(R.id.editText);
editText.setText("YOUR_TEXT");

Comments

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.