I was trying to change background by clicking a button, but I can't identify Constraint Layout in java code.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
tools:context="frekzok.trafficlight.MainActivity">
//here are some button options
</android.support.constraint.ConstraintLayout>
MainActivity.java:
package frekzok.trafficlight;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private ConstraintLayout mConstraintLayout;
private TextView mInfoTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mConstraintLayout =
(ConstraintLayout)findViewById(R.id.constraintLayout);
mInfoTextView = (TextView)findViewById(R.id.textView2);}
public void onRedButtonClick(View view) {
mInfoTextView.setText(R.string.red);
mConstraintLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
}
public void onYellowButtonClick(View view) {
mInfoTextView.setText(R.string.yellow);
mConstraintLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.yellow));
}
public void onGreenButtonClick(View view) {
mInfoTextView.setText(R.string.green);
mConstraintLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.green));
}
}
In Main Activity, on line 18, there is a error on "constraintLayout": Cannot resolve symbol 'constraintLayout' How can I change background color?
Creating a background in the editor To create a visual background for a group of views, we must first create a simple View object to which we apply the background colour or drawable in the android:background attribute (adding this before the other views will cause it to be drawn behind the others).
More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.
A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread). As such, we are planning on enriching its API and capabilities over time.
You didn't add the ID to your constraint layout. Try modifying your xml file to include it:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
tools:context="frekzok.trafficlight.MainActivity">
</android.support.constraint.ConstraintLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With