IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

comment mettre une image avec les question de mon application ?


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Septembre 2014
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Technicien maintenance

    Informations forums :
    Inscription : Septembre 2014
    Messages : 11
    Par défaut comment mettre une image avec les question de mon application ?
    Bonjour,
    Je suis entrain de faire une application qui pose des questions et qui a tribu un score a la fin.
    Je suis bloqué sur un point :
    Je cherche à mettre une image pour chaque questions mais sa ne fonctionne pas, quelqu’un peut-il m’aider ?
    Volontairement je n’est pas mis tous le programme a fin que le message ne soit pas trop grand
    Si dessous en rouge ce que j’ai fait pour mettre l’image qui ne fonctionne pas :

    Activity-Game.xml :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.tu.tutu.essaie2.controller.GameActivity">
        <ImageView
            android:id="@+id/activity_game_question_img"
            android:layout_width="match_parent"
            android:layout_height=" match_parent "
           android:adjusVewsBounds=”true”
        <TextView
            android:id="@+id/activity_game_question_text"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:textColor="@color/colorPrimary"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:textSize="18sp"
            android:textStyle="bold"
            android:gravity="center"
            android:text="Question?"/>
        <Button
            android:id="@+id/activity_game_answer1_btn"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:textColor="@color/colorAccent"
            android:background="@android:color/white"
            android:textSize="20sp"
            android:text="Answer1"/>
        <Button
            android:id="@+id/activity_game_answer2_btn"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:textColor="@color/colorAccent"
            android:background="@android:color/white"
            android:textSize="20sp"
            android:text="Answer2"/>
        <Button
            android:id="@+id/activity_game_answer3_btn"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:textColor="@color/colorAccent"
            android:background="@android:color/white"
            android:textSize="20sp"
            android:text="Answer3"/>
        <Button
            android:id="@+id/activity_game_answer4_btn"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:textColor="@color/colorAccent"
            android:background="@android:color/white"
            android:textSize="20sp"
            android:text="Answer4"/>
    </LinearLayout>
    GameAtivity.java :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    package com.ponroy.florian.topquiz.controller;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.Handler;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    import com.ponroy.florian.topquiz.R;
    import com.ponroy.florian.topquiz.model.Question;
    import com.ponroy.florian.topquiz.model.QuestionBank;
    import java.util.Arrays;
    public class GameActivity extends AppCompatActivity implements View.OnClickListener {
       private Image mImageView;
        private TextView mQuestionTextView;
        private Button mAnswerButton1;
        private Button mAnswerButton2;
        private Button mAnswerButton3;
        private Button mAnswerButton4;
        private QuestionBank mQuestionBank;
        private Question mCurrentQuestion;
        private int mScore;
        private int mNumberOfQuestions;
        public static final String BUNDLE_EXTRA_SCORE = "BUNDLE_EXTRA_SCORE";
        public static final String BUNDLE_STATE_SCORE = "currentScore";
        public static final String BUNDLE_STATE_QUESTION = "currentQuestion";
        private boolean mEnableTouchEvents;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_game);
            System.out.println("GameActivity::onCreate()");
            mQuestionBank = this.generateQuestions();
            if (savedInstanceState != null) {
                mScore = savedInstanceState.getInt(BUNDLE_STATE_SCORE);
                mNumberOfQuestions = savedInstanceState.getInt(BUNDLE_STATE_QUESTION);
            } else {
                mScore = 0;
                mNumberOfQuestions = 10;
            }
            mEnableTouchEvents = true;
            // Wire widgets
           mImageView = (image) findViewById(@+id/activity_game_question_img);
            mQuestionTextView = (TextView) findViewById(R.id.activity_game_question_text);
            mAnswerButton1 = (Button) findViewById(R.id.activity_game_answer1_btn);
            mAnswerButton2 = (Button) findViewById(R.id.activity_game_answer2_btn);
            mAnswerButton3 = (Button) findViewById(R.id.activity_game_answer3_btn);
            mAnswerButton4 = (Button) findViewById(R.id.activity_game_answer4_btn);
            // Use the tag property to 'name' the buttons
            mAnswerButton1.setTag(0);
            mAnswerButton2.setTag(1);
            mAnswerButton3.setTag(2);
            mAnswerButton4.setTag(3);
            mAnswerButton1.setOnClickListener(this);
            mAnswerButton2.setOnClickListener(this);
            mAnswerButton3.setOnClickListener(this);
            mAnswerButton4.setOnClickListener(this);
            mCurrentQuestion = mQuestionBank.getQuestion();
            this.displayQuestion(mCurrentQuestion);
        }
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            outState.putInt(BUNDLE_STATE_SCORE, mScore);
            outState.putInt(BUNDLE_STATE_QUESTION, mNumberOfQuestions);
            super.onSaveInstanceState(outState);
        }
        @Override
        public void onClick(View v) {
            int responseIndex = (int) v.getTag();
            if (responseIndex == mCurrentQuestion.getAnswerIndex()) {
                // Good answer
                Toast.makeText(this, "Bonne réponse", Toast.LENGTH_SHORT).show();
                mScore=mscore + 5;
            } else {
                // Wrong answer
                Toast.makeText(this, "Mauvaise réponse!", Toast.LENGTH_SHORT).show();
               mScore--;
            }
            mEnableTouchEvents = false;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mEnableTouchEvents = true;
                    // If this is the last question, ends the game.
                    // Else, display the next question.
                    if (--mNumberOfQuestions == 0) {
                        // End the game
                        endGame();
                    } else {
                        mCurrentQuestion = mQuestionBank.getQuestion();
                        displayQuestion(mCurrentQuestion);
                    }
                }
            }, 2000); // LENGTH_SHORT is usually 2 second long
        }
        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            return mEnableTouchEvents && super.dispatchTouchEvent(ev);
        }
        private void endGame() {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Bien joué!")
                    .setMessage("Ton score est" + mScore)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // End the activity
                            Intent intent = new Intent();
                            intent.putExtra(BUNDLE_EXTRA_SCORE, mScore);
                            setResult(RESULT_OK, intent);
                            finish();
                        }
                    })
                    .setCancelable(false)
                    .create()
                    .show();
        }
        private void displayQuestion(final Question question) {
            mImageView.setImage(question.getQuestion());
            mQuestionTextView.setText(question.getQuestion());
            mAnswerButton1.setText(question.getChoiceList().get(0));
            mAnswerButton2.setText(question.getChoiceList().get(1));
            mAnswerButton3.setText(question.getChoiceList().get(2));
            mAnswerButton4.setText(question.getChoiceList().get(3));
        }
        private QuestionBank generateQuestions() {
            Question question1 = new Question("scr="@drawable/image_question1", "Quel est le nom du président français actuel?",
                                              Arrays.asList("François Hollande", "Emmanuel Macron", "Jacques Chirac", "François Mitterand"),
                                              1);
            Question question2 = new Question("Combien de pays y a-t-il dans l'Union européenne?",
                                              Arrays.asList("15", "24", "28", "32"),
                                              2);
    
            Question question3 = new Question("Qui est le créateur du système d'exploitation Android?",
                                              Arrays.asList("Andy Rubin", "Steve Wozniak", "Jake Wharton", "Paul Smith"),
                                              0);
            return new QuestionBank(Arrays.asList(question1,
                                                  question2,
                                                    question3));
        }
        @Override
        protected void onStart() {
            super.onStart();
            System.out.println("GameActivity::onStart()");
        }
        @Override
        protected void onResume() {
            super.onResume();
            System.out.println("GameActivity::onResume()");
        }
        @Override
        protected void onPause() {
            super.onPause();
            System.out.println("GameActivity::onPause()");
        }
        @Override
        protected void onStop() {
            super.onStop();
            System.out.println("GameActivity::onStop()");
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            System.out.println("GameActivity::onDestroy()");
        }
    }
    merci d'avance

  2. #2
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2018
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2018
    Messages : 4
    Par défaut
    Salut,

    Je me pose exactement la même question :'(

    J'ai fait le même tuto que toi mais je n'ai pas réussis à intégrer une image selon la question..

    Si quelqu'un peut nous éclairer !

    Merci

  3. #3
    Membre chevronné
    Profil pro
    Inscrit en
    Juillet 2012
    Messages
    476
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2012
    Messages : 476
    Par défaut
    Et moi j'ai une question : qu'est ce qui ne marche pas ? Est ce que rien ne s'affiche ? Est ce que ca s'affiche mal.

    Déjà, vu ton layout, comme ton image a une hauteur "match parent", tu ne risques pas de voir le reste des éléments (textes, boutons...)

    Que renvoie question.getQuestion() ?

Discussions similaires

  1. [Flex4] Comment mettre une image sur les bordures d'un BorderContainer
    Par souf1987 dans le forum Flex
    Réponses: 0
    Dernier message: 22/04/2011, 15h49
  2. comment mettre une image dans une liste avec les values ?
    Par Ekimasu dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 15/05/2007, 17h51
  3. [JBouton] Comment mettre une image sur un bouton ?
    Par Kyti dans le forum Composants
    Réponses: 6
    Dernier message: 11/03/2005, 16h08
  4. PL/SQL - Comment afficher une image avec HTP ?
    Par patmaba dans le forum PL/SQL
    Réponses: 2
    Dernier message: 08/07/2004, 09h28
  5. Comment mettre une image en fond de JFrame
    Par marc26 dans le forum Débuter
    Réponses: 3
    Dernier message: 19/01/2004, 17h57

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo