[go: up one dir, main page]

0% found this document useful (0 votes)
86 views2 pages

Codes Sources Session Normale

The document contains code for an Android app that changes the background color of a constraint layout when a button is clicked. The MainActivity class initializes the button and constraint layout, and sets an onClick listener for the button that changes the background color between blue, red, and back to blue by incrementing and decrementing an integer counter. The activity_main XML layout defines the constraint layout and places a text view and button within it. When the button is clicked, it will call the onClick method in MainActivity to change the background color.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views2 pages

Codes Sources Session Normale

The document contains code for an Android app that changes the background color of a constraint layout when a button is clicked. The MainActivity class initializes the button and constraint layout, and sets an onClick listener for the button that changes the background color between blue, red, and back to blue by incrementing and decrementing an integer counter. The activity_main XML layout defines the constraint layout and places a text view and button within it. When the button is clicked, it will call the onClick method in MainActivity to change the background color.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

CODE SOURCE APK SESSION NORMALE

MAINACTIVITY.JAVA
package com.example.ivan.sessionnormale;
import android.graphics.Color;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.QuickContactBadge;
import static android.graphics.Color.YELLOW;
public class MainActivity extends AppCompatActivity {
int k = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.mon_button);
final ConstraintLayout constraintLayout = (ConstraintLayout)
findViewById(R.id.root);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (k==0){
k++;
constraintLayout.setBackgroundColor(Color.BLUE);
}
else {
if (k==1){
k++;
constraintLayout.setBackgroundColor(Color.RED);
}
else {
k--;
constraintLayout.setBackgroundColor(Color.BLUE);
}
}
}
});
}
}

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:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:text="CHANGE DE COULEUR"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="66dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/cl"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.68"
tools:text="CHANGE DE COULEUR" />
</android.support.constraint.ConstraintLayout>
}

You might also like