Android Studio TP
Android Studio TP
Android Studio TP
TP 1
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
1
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<TextView
android:id="@+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
tools:layout_editor_absoluteX="137dp"
tools:layout_editor_absoluteY="178dp" />
<Button
android:id="@+id/afficherB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="133dp"
tools:layout_editor_absoluteY="250dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
TP 2: MyApplication3
package com.example.myapplication3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
2
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button x=(Button) findViewById(R.id.button1);
x.setText("Bien Venue en " +x.getText());
TextView y=findViewById(R.id.textView1);
y.setText("salut cv ");
x.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast g=Toast.makeText(MainActivity.this.getBaseContext(),
"message envoye",Toast.LENGTH_LONG);
g.show();
}
});
}
public void afficher (View view){
// Toast g=Toast.makeText(MainActivity.this.getBaseContext(),
"message envoye",Toast.LENGTH_LONG);
//g.show();
}
}
<TextView
android:id="@+id/textView1"
android:layout_width="152dp"
android:layout_height="86dp"
android:text="TextView"
android:textIsSelectable="false"
android:textSize="34sp"
tools:layout_editor_absoluteX="92dp"
tools:layout_editor_absoluteY="364dp" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="151dp"
tools:layout_editor_absoluteY="291dp"
android:onClick="afficher"/>
3
</androidx.constraintlayout.widget.ConstraintLayout>
<Spinner
android:id="@+id/listespinner"
android:layout_width="609dp"
android:layout_height="wrap_content" />
</LinearLayout>
package com.example.myapplication4manipuler;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
4
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spl=(Spinner)findViewById(R.id.listespinner);
ArrayAdapter ad=new ArrayAdapter(this,
android.R.layout.simple_spinner_item,T);
spl.setAdapter(ad);
spl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
String valeuchoisi = MainActivity.this.T[position];
Toast.makeText(MainActivity.this,valeuchoisi,Toast.LENGTH_LONG).show();
});
}
}
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
5
android:text="Donnee la valuer de A"
android:textSize="24sp" />
<EditText
android:id="@+id/a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:textSize="24sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Donnee la valuer de B"
android:textSize="24sp" />
<EditText
android:id="@+id/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:textSize="24sp" />
<Button
android:id="@+id/calculer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:textSize="24sp" />
<TextView
android:id="@+id/solution"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Solution est : "
android:textSize="24sp" />
</LinearLayout>
package com.example.myapplication5calculsimple;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Recupere les view par id
Button calButton=(Button)findViewById(R.id.calculer);
6
EditText A=(EditText)findViewById(R.id.a);
EditText B=(EditText)findViewById(R.id.b);
TextView solution =(TextView) findViewById(R.id.solution);
MainActivity c=this;
calButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String va =A.getText().toString();
String vb=B.getText().toString();
Integer na=Integer.valueOf(va);
Integer nb=Integer.valueOf(vb);
Integer somme=na+nb;
solution.setText(somme.toString());
}
});
}
}
TP : Calcul
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="99dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="donner le npmbre 1"/>
<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="448dp"
7
android:layout_height="87dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="donner le npmbre 2"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="105dp"
android:orientation="horizontal">
<Button
android:id="@+id/some"
android:layout_width="99dp"
android:layout_height="97dp"
android:layout_weight="1"
android:text="+"
android:textSize="34sp" />
<Button
android:id="@+id/button2"
android:layout_width="113dp"
android:layout_height="96dp"
android:layout_weight="1"
android:text="-"
android:textSize="34sp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="97dp"
android:layout_weight="1"
android:text="*"
android:textSize="34sp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="97dp"
android:layout_weight="1"
android:text="/"
android:textSize="34sp" />
</LinearLayout>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="314dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="Solution :"
android:textSize="34sp" />
</LinearLayout>
package com.example.tp1calculatrice_buttonlistener;
import androidx.appcompat.app.AppCompatActivity;
8
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
9
} } } });
btndiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Boolean test=verifiezZone();
if (test==true)
{ try { Integer
s=Integer.valueOf(zone1.getText().toString())/
Integer.valueOf(zone2.getText().toString());
solution.setText(s.toString());
}
catch (Exception x)
{// Toast
} } } });
}
private boolean verifiezZone()
{
if(zone1.getText().toString()==null
||zone2.getText().toString()==null )
{
Toast.makeText(this," Remplir les
valeurs",Toast.LENGTH_LONG).show();
return false;
}
return true;
}
}
package
com.example.myapplication6manipulerradiogroupradiobuttonlinearlayoutcolor;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
10
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
Toast.makeText(MainActivity.this,rouge.getText().toString(),Toast.LENGTH_LO
NG).show();
L1.setBackgroundColor(Color.RED);
break;
case R.id.vert:
Toast.makeText(MainActivity.this,vert.getText().toString(),Toast.LENGTH_LON
G).show();
L1.setBackgroundColor(Color.GREEN);
break;
case R.id.bleu:
Toast.makeText(MainActivity.this,bleu.getText().toString(),Toast.LENGTH_LON
G).show();
L1.setBackgroundColor(Color.BLUE);
break;
}
}
});
button2 =(Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
}
});
}
}
11
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical"
android:id="@+id/L1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="34sp"
android:gravity="center"/>
<RadioGroup
android:id="@+id/group1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rouge"
android:layout_width="132dp"
android:layout_height="wrap_content"
android:text="Rouge"
android:textSize="20sp" />
<RadioButton
android:id="@+id/vert"
android:layout_width="98dp"
android:layout_height="wrap_content"
android:text="Vert"
android:textSize="20sp" />
<RadioButton
android:id="@+id/bleu"
android:layout_width="181dp"
android:layout_height="wrap_content"
android:text="Bleu"
android:textSize="20sp" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
12
</LinearLayout>
package
com.example.myapplication6manipulerradiogroupradiobuttonlinearlayoutcolor;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
@Override
public void onClick(View v) {
String s="valeur";
if(ch1.isChecked()==true)
{
s=s+" : "+ch1.getText().toString();
ch1.setText("TDI clicked");
}
else
{
ch1.setText("TDI");
}
if(ch2.isChecked()==true)
{
s=s+" : "+ch2.getText().toString();
ch2.setText("TRI clicked");
}
13
else
{
ch2.setText("TRI");
}
Toast.makeText(this,s,Toast.LENGTH_LONG).show();
}
}
<CheckBox
android:id="@+id/checkBox"
android:layout_width="228dp"
android:layout_height="53dp"
android:layout_marginStart="62dp"
android:layout_marginTop="56dp"
android:layout_marginEnd="121dp"
android:text="TRI"
android:textSize="34sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox2" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="228dp"
android:layout_height="53dp"
android:layout_marginStart="62dp"
android:layout_marginTop="45dp"
android:layout_marginEnd="121dp"
android:text="TDI"
android:textSize="34sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btngotomayactivity"
android:layout_width="263dp"
android:layout_height="75dp"
android:layout_marginStart="62dp"
android:layout_marginTop="121dp"
android:layout_marginEnd="86dp"
android:layout_marginBottom="328dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox" />
</androidx.constraintlayout.widget.ConstraintLayout>
14
15