Issue
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);
}
}
Solution
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);
}
}
Answered By - WebDiva
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.