0

Hi I am very new to Android development. Here is my problem:

In have two activities as shown below.

  • Activity1.class XML--> has two buttons

    Button1 (Id:@+id/btn1 , onclick:doTHIS) and Button2 (Id:@+id/btn2 , onclick:doTHAT)

  • Activity2.class XML--> has a TextView with this parameters

    Textview (Id:@+id/texter , Text:@+string/text1 , background:#ffffff)

onClick of Button1, I want to go to Activity2 and change

  • the background of a TextView to #000000
  • the Text to @+sting/text2

onClick of Button2, I want go to Activity2 and

  • set the TextView to visible:gone (not visible)

I tried to do this with no results.

public void doTHIS(View view){
    TextView tv= (TextView)findViewById(R.id.texter);
    tv.setText(getResources().getString(R.string.text2));
    tv.setTextColor(Color.RED);
    tv.setVisibility(View.GONE);
    Intent intent = new Intent (this, Activity2.class);
    startActivity(intent);

Can anyone help me figure out how to solve my problem?

Melquiades
  • 8,316
  • 1
  • 27
  • 40
  • What is the problem you are facing? Is the Activity2 not launching? – Ajit Pratap Singh Jan 25 '14 at 17:26
  • Thanks for your reply. It crashes on Activity2. Actually I know how to load the activity2. but I am not able to change the parameters of my textview (Textstring,Background and visibility). – user3210337 Jan 25 '14 at 17:32
  • Add the code for Activity2. – Ajit Pratap Singh Jan 25 '14 at 17:33
  • Thats the xml – user3210337 Jan 25 '14 at 17:40
  • thats the code of activity 2 import android.app.Activity;import android.os.Bundle;public class Lettore extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity2); } }; – user3210337 Jan 25 '14 at 17:42
  • I tried to write this : public void doTHIS(View view) { final TextView tv1=(TextView)findViewById(R.id.texter); tv1.setVisibility(View.GONE); but I get an error FATAL EXEPTION:main java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick etc etc – user3210337 Jan 25 '14 at 18:08

1 Answers1

0

You can't change controls on another layout until it's loaded. In other words, when you're in Activity1, you can't change TextView that's in layout for Activity2.

I'm sure there are a couple of ways to achieve what you want though. One of them would be to pass a flag in an Intent to Activity2, indicating which button has been pressed, and act accordingly.

  1. In Activity1, let's pass integer 1 if Button1 was pressed, and 2 if Button2 was pressed, so change your click listeners as follows:

    public void doTHIS(View view) {
        Intent intent = new Intent(this, Activity2.class);
        intent.putExtra("button", 1);
        startActivity(intent);
    }
    

    and

    public void doTHAT(View view) {
        Intent intent = new Intent(this, Activity2.class);
        intent.putExtra("button", 2);
        startActivity(intent);
    }    
    
  2. In Activity2, in onCreate(), check which button called this activity, and do your changes (note: we're doing this AFTER setContentView()):

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity2);
    
        //now, let's check which button got us here
        int button = getIntent().getIntExtra("button", 1);
    
        TextView tv = (TextView) findViewById(R.id.texter);
    
        switch (button) {
            case 1:
                tv.setText(getResources().getString(R.string.text2));
                tv.setTextColor(Color.RED);
                //whatever else you want to do
                break;
            case 2:
                tv.setVisibility(View.GONE);
                break;    
        }
    
        //rest of your code
    }
    

Read more on passing data between activities here and here.

Community
  • 1
  • 1
Melquiades
  • 8,316
  • 1
  • 27
  • 40