Reputation: 65
I am having some issues trying to make slight modifications to the tutorial from the android website. I had no issues following the tutorial but when trying to make some slight modifications to further acquaint myself with the constraints, view, layouts, etc., I find myself at a wall.
So far I have almost the exact same work as the tutorial except for trying to send a color to a new activity that changes the background color.
My issue so far is finding the ID to target said window. With the original tutorial the "R.id.textView" locates a component of the constraints tree, but, having a background color already, I assumed that I could just use the windows/constraint ID to change the color.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_color);
Intent intent = getIntent();
String hexValue = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
RelativeLayout layout = (RelativeLayout) findViewById(R.layout.activity_main);
layout.setBackgroundColor(parseColor("#" + hexValue));
}
So far I have gone through the docs a fair amount, but they are vast and detailed, so I may be looking for the wrong search terms.
Any help appreciated! Thank you!
=============================================================
Updated MainActivity code..
package com.example.color;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import static android.graphics.Color.parseColor;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void changeColor(View view) {
Intent intent = new Intent(this, MainActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String hexValue = editText.getText().toString();
ConstraintLayout conlay = (ConstraintLayout) findViewById(R.id.screen);
conlay.setBackgroundColor(parseColor("#" + hexValue));
startActivity(intent);
}
}
This is starting to work, but I decided to delete the second activity because it wasn't responding well. Now I have the color changing but it doesn't change the background color of the main screen, and/or it doesn't persist. But the color does change now! So thank you for what I have so far!
Upvotes: 1
Views: 1145
Reputation: 622
To find the ID of your layout, look for the below line in your xml.
android:id="@+id/your_layout_id"
Then, you can change the background color of a layout like below.
//In your example your_layout_id = screen
RelativeLayout mRelativeLayout = (RelativeLayout)findViewById(R.id.your_layout_id);
mRelativeLayout.setBackgroundColor(Color.RED);
Also, I can see that your layout is a constraint layout and not a relative layout.
So your code to change the background should be like below.
ConstraintLayout mConstraintLayout = (ConstraintLayout)findViewById(R.id.screen);
mConstraintLayout.setBackgroundColor(Color.RED);
Upvotes: 1
Reputation: 36
Instead of sending the new color, save the button with the new color in your folder with custom buttons, and when changing Acitivity, just change bottom background ID in your new activity function
But I believe the mistake is that you save the hex value as a string...
Upvotes: 0