Change button background with an action

41 views Asked by At

I have two activitys in the android application, the MainActivity and the SecondActicity. In the MainActivity i have one button which is lead to the second one. I have a background image on this button and i like to change that from the SecondActivity. There is a simple question in the SecondActivity and when the answer is good i like the first button to change the background. When i launch the app and my answer is good i got this message from the emulator:"Unfortunately,app has stopped." Any response thank you!

Here is the MainActivity.java

    public void onClick(View v) {
    if(v.getId() == R.id.button1){
        Intent i = new Intent();
        i.setClass(this, SecondActivity.class);
        startActivity(i);
    }
}   
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

}

The activity_main.xml

    <?xml version="1.0" encoding="UTF-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">

  <Button android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/otven"
        android:onClick="onClick"/>

 </RelativeLayout>

SecondActivity.java

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


final TextView textview = (TextView) findViewById(R.id.mennyi); 
final EditText etGuess = (EditText) findViewById(R.id.guess);
final Button btnNew = (Button) findViewById(R.id.btnok);
final Button btnNew2 = (Button) findViewById(R.id.button1);

btnNew.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        int guess = Integer.parseInt (etGuess.getText().toString());
        if(guess==2){
            btnNew2.setBackgroundResource(R.drawable.hetvenot);

        }
    }
});
}
}

And the second.xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <TextView 
    android:id="@+id/mennyi"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="1+1="/>
  <EditText 
    android:id="@+id/guess"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    />

  <Button
    android:id="@+id/btnok"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/guess"
    android:onClick="onClick"
    android:text="Ok" />

 </RelativeLayout>
1

There are 1 answers

0
Anton Kovalyov On BEST ANSWER

Use startActivityForResult(i) in FirstActivity and onActivityResult method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (resultCode == RESULT_OK)
       button1.setBackgroundResource(R.drawable.hetvenot);
}

in SecondActivity:

@Override
    public void onClick(View v) {
        int guess = Integer.parseInt (etGuess.getText().toString());
        if(guess==2){
            setResult(Activity.RESULT_OK);
            finish();
        }
    }
});