1

i have been stuck on this problem for far too long. i think it's simple but i'm a newbie android developer so your help would be greatly appreciated.

i have two activities. my main activity layout has a textview and a button. What i am trying to do is to implement the button to change the text of the textview from another activity class (Btn class).

this is my Btn class

public class Btn extends Activity implements OnClickListener {
    Button btn;
    TextView textBox;

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        btn = (Button) findViewById(R.id.btn);  
        textBox = (TextView)findViewById(R.id.address);
        btn.setOnClickListener(this);           
    }

    @Override
    public void onClick(View v) {
        textBox.setText("Test Test");
    }

}

now this compiles and works fine but when i press the button nothing happens. i would like to know how change the text of the textview when i press the button. i know how to do this in my main activity class but i would like to do it from another class.

user2293062
  • 171
  • 1
  • 2
  • 6
  • possible duplicate of [How to pass object from one activity to another in Android](http://stackoverflow.com/questions/2736389/how-to-pass-object-from-one-activity-to-another-in-android) – Pratik Butani Dec 25 '14 at 10:56
  • Your question is not clear to me, did you want to change the text of textview in MainActivity class after getting some data from Second Activity class on Btn Click Event??? – A.R. Dec 25 '14 at 11:23

2 Answers2

1

Please try implementing the below code,

MainActivity.java

    public class MainActivity extends Activity implements OnClickListener {

    Button btn;
    TextView textBox;

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        btn = (Button) findViewById(R.id.btn);  
        textBox = (TextView)findViewById(R.id.address);
        btn.setOnClickListener(this);           
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn:
            String strTemp = "Test Test";
            textBox.setText(strTemp);

            Intent intent = new Intent(MainActivity.this, Second.class);
            intent.putExtra("tempstring", strTemp);
            startActivity(intent);
            break;

        default:
            break;
        }

    }
}

Second.java

 public class Second extends Activity {

    private TextView txtTemp;

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.second); 

        txtTemp = (TextView) findViewById(R.id.txt_temp);

        Intent intent = getIntent();
        String strTemp = intent.getStringExtra("tempstring");

        txtTemp.setText(strTemp);
    }
}

Hope it works for you.

Parth Bhayani
  • 1,894
  • 3
  • 17
  • 35
  • 1
    Maybe an explanation of what this does would be useful rather than "here's some code". – Ivan Wooll Dec 25 '14 at 10:23
  • In your main activity define edittext or textview like txtUname and txtEmail and define one button, on click of this button put the above code from MainActivity.java and now create second layout in which you can fetch the data using intent which is shown in the Second.java and set the text which is required from your end. I hope this will work for you. – Parth Bhayani Dec 25 '14 at 10:31
1

Even if you are really a complete newbie, the second thing to learn after Activity is Intent. There's no need for one Activity to retrieve objects' data and properties from another and send changes straight to them, it's not safe for the runtime because you can't be 100% sure the background Activity isn't killed by system. So Intents are invented for calling Activities and passing data to them. The code provided by Android Coders works in this way. First, you create an Intent object, put extra data (such as text you want to set to another Activity's TextView) and finally start an Activity responsible for this kind of Intent. In this code, Intent is explicit, which means that a certain Activity of certain package should be called. In this Activity, there's a code that checks up if there was an Intent called, reads extra data (your text) from it and finally calls setText(). There are also implicit Intents, based on the type of data and action. It's up to the system to give you a choise between all Activities from all packages present on the device, which can handle the type of data you provide to the Intent. Nothing personal, but if you won't be able to understand Intents, you won't understand Android. Try this very hard.

Tor_Gash
  • 164
  • 1
  • 9