vendredi 23 janvier 2015

How to random quiz in an Android Application without Database

I like to make a random quiz with 5 questions. So here's my code in the first question, it is the same with the other questions.



public class alphaquiz1 extends Activity implements OnClickListener{

MediaPlayer yourStereo, mp;
AssetFileDescriptor afd;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quizal1);




View button1 = this.findViewById(R.id.button1);
button1.setOnClickListener(this);

View button2 = this.findViewById(R.id.button2);
button2.setOnClickListener(this);


View back= this.findViewById(R.id.buttonback);
back.setOnClickListener(this);

ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.alphaquiz1);



MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.tiktok);
mediaPlayer.setVolume(8f, 8f);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}




public void onClick(View v) {

switch(v.getId()){


case R.id.button1:
mp = MediaPlayer.create(this, R.raw.correct);
mp.start();

View popupView = new View(alphaquiz1.this);
final PopupWindow popupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
Drawable drawable = getResources().getDrawable(R.drawable.correct);

popupWindow.setBackgroundDrawable(drawable);

popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setTouchInterceptor(new View.OnTouchListener() {



@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub

popupWindow.dismiss();
return false;

}
});

popupWindow.showAtLocation(findViewById(R.id.button1), Gravity.CENTER, 0, 0);

Intent button1 = new Intent(this, alphaquiz2.class);
startActivity(button1);




break;

case R.id.button2:
mp = MediaPlayer.create(this, R.raw.wrong);
mp.start();
break;

case R.id.buttonback:
Intent buttonback = new Intent(this, Alphabets.class);
startActivity(buttonback);
break;
}

}

@Override
public void onBackPressed(){

new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
.setMessage("Are you sure you want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
System.exit(0);
}
}).setNegativeButton("No", null).show();
}

}


And here it's xml lay out.



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1.01"
android:src="@drawable/abc_ab_bottom_transparent_dark_holo" />

<Button
android:id="@+id/button1"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="20dp"
android:background="@drawable/buttonyellow"
android:text="O"
android:textColor="#000000"
android:textSize="30sp" />

<Button
android:id="@+id/button2"
android:layout_width="246dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="23dp"
android:background="@drawable/buttongreen"
android:text="Z"
android:textColor="#000000"
android:textSize="30sp" />

<Button
android:id="@+id/buttonback"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginBottom="20dp"
android:layout_marginTop="23dp"
android:background="@drawable/backbutton"
android:text="Back"
android:textColor="#000000"
android:textSize="25sp" />

</LinearLayout>


Please help me to random questions, even it don't random answers or option. Thankyou





Aucun commentaire:

Enregistrer un commentaire