0% found this document useful (0 votes)
9 views7 pages

Exercice android

The document outlines the implementation of an Android application with two main activities. The first activity allows users to change images and navigate to a second activity where they can apply animations to an image. Additionally, the second part of the document describes an exercise to create an interface for course selection using checkboxes and radio buttons, with validation and confirmation dialogs implemented in the main activity.

Uploaded by

sabr ine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Exercice android

The document outlines the implementation of an Android application with two main activities. The first activity allows users to change images and navigate to a second activity where they can apply animations to an image. Additionally, the second part of the document describes an exercise to create an interface for course selection using checkboxes and radio buttons, with validation and confirmation dialogs implemented in the main activity.

Uploaded by

sabr ine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Exercice TP5 :

Activity_main.xml :
<ImageView
android:id="@+id/imageView"
android:layout_width="320dp"
android:layout_height="536dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.184"
app:srcCompat="@drawable/img2" />

<Button
android:id="@+id/button1"
android:layout_width="130sp"
android:layout_height="wrap_content"
android:text="changer "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="0.644"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.881" />

<Button
android:id="@+id/button2"
android:layout_width="130sp"
android:layout_height="wrap_content"
android:layout_marginEnd="44dp"
android:layout_marginRight="44dp"
android:text="animer"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.881" />

MainActivity.java :
package com.example.tp5_1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.view.animation.*;

public class MainActivity extends AppCompatActivity {


int currentImageIndex = 1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = findViewById(R.id.imageView);
Button change = findViewById(R.id.button1);
Button animationButton = findViewById(R.id.button2);
// Définir l'image initiale
imageView.setImageResource(R.drawable.img1);
change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentImageIndex == 1) {
imageView.setImageResource(R.drawable.img2);
currentImageIndex = 2;}
else if (currentImageIndex == 2) {
imageView.setImageResource(R.drawable.img3);
currentImageIndex = 3;}
else {
imageView.setImageResource(R.drawable.img1);
currentImageIndex = 1;}
}});
animationButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// Naviguer vers AnimationActivity lorsque le bouton
est cliqué
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
startActivity(intent);

}});
}}

SecondActivity.xml :
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="zoom"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="Fade"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="Move"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="124dp"
android:text="Rotate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="172dp"
android:layout_height="316dp"
app:layout_constraintBottom_toTopOf="@+id/button1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/img1" />

SecondActivity.xml :
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.*;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button B1, B2, B3, B4;
ImageView image = findViewById(R.id.imageView);
B1 = findViewById(R.id.button1);
B2 = findViewById(R.id.button2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button4);
// Applique une animation à l'image lors du clic sur le deuxième
bouton
B1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Animation animation1 =
AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoom);
image.startAnimation(animation1);
}});
B2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Animation animation2 =
AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade);
image.startAnimation(animation2);
}});
B3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Animation animation3 =
AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move);
image.startAnimation(animation3);
}});
B4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Animation animation4 =
AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
image.startAnimation(animation4);
}});
}}

Exercice :
Créer une application qui affiche une interface permettant à l’utilisateur de sélectionner les
cours qu’il souhaite acheter (checkbox) en choisissant la langue (RadioButton avec français
ou anglais). Un message Toast s’affiche si la sélection n’est pas validée si non une alertDialog
pour la confirmation s'affiche
Activity_main.xml :

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="76dp"
android:text="Choix du cours"
android:textSize="20sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.283"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="Java"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.111"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="C++"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.11"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Python"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.116"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox2" />

<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="HTML"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.114"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox3" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Choix du langue"
android:textSize="20sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.293"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox4" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="52dp"
tools:layout_editor_absoluteY="472dp" />

<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="RadioButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.129"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="48dp"
android:text="RadioButton"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.129"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:text="Acheter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.466"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintVertical_bias="0.921" />

MainActivity.java :
package com.example.exercice2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.*;
import android.view.*;
import android.content.DialogInterface;
import androidx.appcompat.app.AlertDialog;

public class MainActivity extends AppCompatActivity {


CheckBox cb1, cb2, cb3, cb4;
RadioGroup radioGroup;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1 = (CheckBox) findViewById(R.id.checkBox);
cb2 = (CheckBox) findViewById(R.id.checkBox2);
cb3 = (CheckBox) findViewById(R.id.checkBox3);
cb4 = (CheckBox) findViewById(R.id.checkBox4);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View V) {
validation();
}});
}
public void validation() {
//checkbox validation
if (!cb1.isChecked() && !cb2.isChecked() && !cb3.isChecked() &&
!cb4.isChecked()) {
Toast.makeText(getApplicationContext(), "Veuillez choisissez le
cours que vous souhaitez acheter", Toast.LENGTH_LONG).show();
return;}
//getCheckedRadioButton() -> returns -1 if no radio button is checked
int isSelected = radioGroup.getCheckedRadioButtonId();
if (isSelected == -1) {
Toast.makeText(getApplicationContext(), "vous n'avez pas
sélectionner la langue!", Toast.LENGTH_LONG).show();
return;}
// Afficher une boîte de dialogue pour confirmer la sélection
showConfirmationDialog();}
private void showConfirmationDialog() {
new AlertDialog.Builder(this)
.setTitle("Confirmation de la sélection")
.setMessage("Voulez-vous confirmer la sélection des
cours ?")
.setPositiveButton("Confirmer", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int
which) {
Toast.makeText(getApplicationContext(),
"Sélection Confirmée", Toast.LENGTH_LONG).show();
// Vous pouvez ajouter le code pour passer à
une autre activité ici
}})
.setNegativeButton("Annuler", null).show();
}
}

You might also like