• Développez une application pour android étape 13 (Ajouter des ingredients avec une boite de dialogue personnalisée)

    Publié le 02 juin 2009 par Guy (GuyTouch)


    Dans cette étape 13, je reviens sur l’enregistrement de la recette, pour rajouter une fonctionnalité permettant d’ajouter les ingredients sur celle-ci avec une boite de dialogue personnalisée.

    Dans ma fenêtre d’enregistrement de la recette je rajoute le bouton Ajouter un condiment c’est lui qui vas ouvrir ma boite de dialogue

    ?Download download.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    
    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent" 
                  android:layout_height="fill_parent" 
                  android:orientation="vertical" 
                  android:layoutAnimation="@anim/layout_bottom_to_top_slide">
     
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent" 
                  android:layout_height="fill_parent" 
                  android:orientation="vertical" >             
     
     
        <TextView android:id="@+id/text"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:text="Nom de la recette"  />
     
         <EditText android:id="@+id/entry" 
                  android:layout_width="fill_parent" 
                  android:layout_height="wrap_content" 
                  android:background="@android:drawable/editbox_background"
                   />
     
         <Button android:id="@+id/ajcondiment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Ajouter un condiment" />           
     
         <EditText android:id="@+id/entry2" 
                  android:layout_width="fill_parent" 
                  android:layout_height="90px" 
                  android:background="@android:drawable/editbox_background" />
     
        <TextView android:id="@+id/text3"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:text="La recette"  />    
     
         <EditText android:id="@+id/entry3" 
                  android:layout_width="fill_parent" 
                  android:layout_height="200px" 
                  android:background="@android:drawable/editbox_background"
                   />
     
        <Button android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Valider" />
     </LinearLayout>           
     
    </ScrollView>

    android131 189x300 Développez une application pour android étape 13 (Ajouter des ingredients avec une boite de dialogue personnalisée) Android France

    Pour ma boite de dialogue personnalisé je crée une class Myclassdialog qui est une extension de la class Dialog

    ?Download download.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    public class Myclassdialog extends Dialog {
     
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		Log.d("TestApp", "Dialog created");
    			setContentView(R.layout.choixingredient);			
    			okButton = (Button) findViewById(R.id.btvalingre);
    			cancelButton = (Button) findViewById(R.id.btquitter);			
    			okButton.setOnClickListener(this);
    			cancelButton.setOnClickListener(this);
     
    	}   
     
     
    }

    Dans mon repertoire res/layout je crée un xml choixingredient.xml qui est appele par ma class Myclassdialog. J’ai dans cette fenêtre un champ pour l’unité de mesure, un champ pour la quantité et un autre pour le nom de l’ingredient

    ?Download download.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent" 
                  android:layout_height="fill_parent" 
                  android:orientation="vertical" >
     
     <TextView android:id="@+id/unitme"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:text="unite de mesure"  />  
      <EditText android:id="@+id/valunite" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:drawable/editbox_background" />
     
      <TextView android:id="@+id/unitme"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:text="quantite"  />  
      <EditText android:id="@+id/valquante" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:drawable/editbox_background"   
    	android:textSize="18sp"/>
     
      <TextView android:id="@+id/ingredient"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:text="Ingredient"  />  
     
      <EditText android:id="@+id/valingredient" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:drawable/editbox_background" /> 
      <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="2">
     
        <TableRow>
     
       <Button android:id="@+id/btvalingre"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Valider" />  
     
         <Button android:id="@+id/btquitter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="quitter" />  
          </TableRow>
           </TableLayout>                              
     
    </LinearLayout>

    il ne reste plus que dans la classe principal à rajouter une action sur mon bouton ajouter un condiment

    ?Download download.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    Button btEnregajcon = (Button) findViewById(R.id.ajcondiment);
    btEnregajcon.setOnClickListener(new Button.OnClickListener() {
                    public void onClick(View v) {
                    	//creation d'une instance de ma classe
                    	Myclassdialog dialog = new Myclassdialog(v.getContext());
                    	//titre de ma boite de dialogue
                            dialog.setTitle("Enregistrement ingredient");
                           //ouverture de ma boite de dialogue
                    	dialog.show();                	
                    }
            	});        	
            	return true;
    }

    android132 191x300 Développez une application pour android étape 13 (Ajouter des ingredients avec une boite de dialogue personnalisée) Android France

    A suivre sauvegarder temporairement les info saisies dans la boite de dialogue.

    les sources ici

    Il est temps de mettre à profit les tutoriels d’Android France:
    Passez à la vitesse supérieure et investissez quelques dizaines d’euros pour acquérir les connaissances qui vous feront gagner de l’argent avec vos applications rendez-vous sur notre boutique Android-france pour ces formations en vidéo


    Guy

    Co-fondateur du site Android france, senior lead developper, passionné de bière et de cigare cubain

    Twitter Google+ 

  • Les Brèves