Cours Complet Android Studio (Java)
Ce document guide étape par étape la création d'une application Android en Java avec :
1. Base de données locale (SQLite)
2. ListView simple et avec images
3. Saisie et affichage de données utilisateur
4. Navigation entre pages (activités)
5. Layout XML complet
1. Création du projet Android
- Ouvre Android Studio > New Project > Empty Activity
- Nom du projet : MonApp
- Langage : Java
- Minimum SDK : API 21 ou supérieur
2. Layout XML (activity_main.xml)
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/nameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Entrez votre nom" />
<Button
android:id="@+id/submitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Soumettre" />
<TextView
android:id="@+id/resultText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
3. Layout pour ListView avec image (row_layout.xml)
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textSize="16sp" />
</LinearLayout>
4. Code Java pour affichage ([Link])
public class MainActivity extends AppCompatActivity {
EditText nameInput;
Button submitBtn;
TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
nameInput = findViewById([Link]);
submitBtn = findViewById([Link]);
resultText = findViewById([Link]);
[Link](v -> {
String name = [Link]().toString();
[Link]("Bienvenue " + name);
Intent i = new Intent([Link], [Link]);
[Link]("userName", name);
startActivity(i);
});
}
}
5. Deuxième page : [Link]
public class SecondActivity extends AppCompatActivity {
TextView welcomeText;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_second);
welcomeText = findViewById([Link]);
String name = getIntent().getStringExtra("userName");
[Link]("Hello " + name + ", vous êtes sur la 2ème page");
}
}
6. Layout de la deuxième activité (activity_second.xml)
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<TextView
android:id="@+id/welcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>