Android Build a Simple Android App with Java Improving Our Code Dynamically Changing the Background Color

HOW TO SET BACKGROUND COLOR

HOW TO SET GREEN COLOR BACKGROUND

MealActivity.java
import android.os.Bundle;
import android.widget.TextView;

public class MealActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meal);

        TextView foodLabel = (TextView) findViewById(R.id.foodTextView);
        TextView drinkLabel = (TextView) findViewById(R.id.drinkTextView);
        RelativeLayout mealLayout = (RelativeLayout) findViewById(R.id.mealLayout);
        RelativeLayout.setBackgroundColorColorGREEN;
    }
}

1 Answer

Ruan Lopes de Souza
Ruan Lopes de Souza
4,854 Points

You are not assigning the color to the relative layout properly.

You should do it as below

RelativeLayout mealLayout = (RelativeLayout) findViewById(R.id.mealLayout);
mealLayout.setBackgroundColor(Color.GREEN);