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);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…