Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

java - Change button color when a button in another activity is pressed

I'm new in Android programming and I still have sometimes small problems. My problem currently is the following:

I have 2 activities with just one button in each. The button of the first activity is opening the second activity. But when I press the button in the second activity, the Text in the Button of the first activity should change to "Hello" and the color should be red.

I have managed to change the text but not the color. Could someone help me please?

My Code:

First activity:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button placeHolder;
Intent intent;

public void button0(View v){

    intent = new Intent(getApplicationContext(), MainActivity2.class);
    startActivity(intent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    placeHolder = findViewById(R.id.button);

    placeHolder.setText(getIntent().getStringExtra("message"));
}
}

The code of second activity:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity2 extends AppCompatActivity {


public void buttonOnClick(View v){

    Intent intent=new Intent(MainActivity2.this, MainActivity.class);
    intent.putExtra("message", "Hello");
    startActivity(intent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
}
}
question from:https://stackoverflow.com/questions/65932729/change-button-color-when-a-button-in-another-activity-is-pressed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add this line of code in your first activity where you set your text to your button:

To change background color:

placeHolder.setBackgroundColor(Color.parseColor("#FF0000"));

To change text color:

placeHolder.setTextColor(Color.parseColor("#FF0000"));

Usage: MainActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button placeHolder;
Intent intent;

public static final int REQUEST_CODE = 101;

public void button0(View v){

    intent = new Intent(getApplicationContext(), MainActivity2.class);
    startActivityForResult(intent, REQUEST_CODE);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    placeHolder = findViewById(R.id.button);
}

    // Call Back method  to get the Message form other Activity  
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data){  
    super.onActivityResult(requestCode, resultCode, data);  

    // check if the request code is same as what is passed
  
    if(requestCode == REQUEST_CODE) {  
       placeHolder.setText(data.getStringExtra("message"));
       placeHolder.setTextColor(Color.parseColor("#FF0000"));
    }  

}  

MainActivity2:


package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity2 extends AppCompatActivity {


public void buttonOnClick(View v){

    Intent intent=new Intent();
    intent.putExtra("message", "Hello");
    setResult(MainActivity2.REQUEST_CODE, intent);
    finish();
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...