Tous les articles

Personnalise ton AlertDialog

Le code Java :

  • MainActivity.java
Spoiler

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void commencerAvant(View v){
        AlertDialog.Builder a = new AlertDialog.Builder(MainActivity.this);
        a.setTitle("Règles du jeu");
        a.setMessage("Voici les règles du jeu dans une Alertdialog non personnalisée");
        a.setPositiveButton("Commencer!", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        a.setNegativeButton("Fermer", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        a.show();
    }

    public void commencerApres(View v){
        DialogPerso.showDialog(this, "Voici les règles du jeu dans une Alertdialog personnalisée");
    }

}

[collapse]
  • DialogPerso.java
Spoiler

import android.app.Dialog;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Formation on 25/03/2018.
 */

public class DialogPerso {

    public static void showDialog(MainActivity mainactivity, String texte){
        Dialog dialog = new Dialog(mainactivity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.alertdialog);

        TextView tx = (TextView) dialog.findViewById(R.id.texte);
        tx.setText(texte);

        Button bt1 = (Button) dialog.findViewById(R.id.button1);
        Button bt2 = (Button) dialog.findViewById(R.id.button2);

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Quitter
            }
        });

        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Commencer
            }
        });

        dialog.show();
    }
}

[collapse]

Le code XML :

  • activity_main.xml
Spoiler

<?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"
    
    android:orientation="vertical"
    android:gravity="center">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Règles du jeu avant"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:onClick="commencerAvant"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Règles du jeu après"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:onClick="commencerApres"/>

</LinearLayout>

[collapse]
  • alertdialog.xml
Spoiler

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
    android:orientation="vertical">


    <TextView
        android:id="@+id/titre"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="Règles du jeu"
        android:background="#4ec2e7"
        android:textColor="#FFF"
        android:gravity="center"
        android:textSize="18sp"
        android:drawableLeft="@drawable/regles"
        android:drawablePadding="0dp"
        android:paddingLeft="30dp"
        />

    <TextView
        android:id="@+id/texte"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Texte"
        android:textSize="18sp"
        android:layout_below="@id/titre"
        android:gravity="center_horizontal"
        android:layout_marginBottom="40sp"
        android:layout_marginTop="40sp"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@id/texte">

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#CBD0D8"
            android:text="Quitter"
            android:textColor="#FFF"/>

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#5fbf9a"
            android:text="Commencer"
            android:textColor="#FFF"/>


    </LinearLayout>

</RelativeLayout>

[collapse]

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *