1

When I first ran the sample HelloWorld app, it displays the hello world text on the emulator. I decided then to delete that and make a button. What I wanted is that when I click the button, it will show a text "This is the second activity". I made another XML file and another class to handle the second activity to display the text. But when I ran again, I cannot see the changes on the UI for the emulator. The text "This is the second activity" does not show after I clicked the button. I saved everything. How would I automatically update the UI of the emulator after some of the changes made on the design? I am new to android development. Please help me. Btw I cannot post images so it requires 10 reputation that's why I used online image viewing. Sorry for that.

Here is my Graphical layout on eclipse: activity_main.xml

http://s16.postimg.org/wusm4qrp1/image.png

second.xml

http://s29.postimg.org/qft17p5on/image.png

Running the emulator:

http://s28.postimg.org/f9dn32ku5/image.png

After clicking the button (in which case the text I edit does not show):

http://s16.postimg.org/75r62dodx/image.png

Cœur
  • 37,241
  • 25
  • 195
  • 267
Miss Lc
  • 69
  • 1
  • 9
  • 1
    Share your work please. – yildirimyigit Jul 15 '14 at 13:53
  • Hello how would I share it? Sorry I'm just new to this. – Miss Lc Jul 15 '14 at 13:55
  • It's ok. Edit your question by copying & pasting your code. Look at this question, for example. http://stackoverflow.com/questions/24759796/android-error-no-such-file-or-directory Now we can just guess but if you post your code, then we will be able to see what's wrong. – yildirimyigit Jul 15 '14 at 13:59
  • And to ask better questions, please read http://stackoverflow.com/editing-help and http://stackoverflow.com/help/how-to-ask – yildirimyigit Jul 15 '14 at 14:03

1 Answers1

0

Because you didn't post your code, I'll try to explain it from the beginning.

I don't know if you can do it in an other way, but the following answer assumes we aim the good programming practice.

Both of the activities have their distinct layouts set in the following way, right?

public class MyFirstActivity extends Activity{
...
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
    ...


public class MySecondActivity extends Activity{
...
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    ...

Then, in your first activity, define onClickListener of your button

...
setContentView(R.layout.activity_first);
Button myButton = (Button)findViewById(R.id.my_button);

myButton.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View view) {
    Intent intent = new Intent(MyFirstActivity.this, MySecondActivity.class);
    startActivity(intent);
  }

});

That being said, I don't recommend you to have two activities for this functionality. You should have different activities when you need different layouts. Putting the button and the textview in the same layout and updating textview inside the button's onclicklistener is a better solution.

yildirimyigit
  • 3,013
  • 4
  • 25
  • 35