Reputation: 9
I am Creating an App of Math for kids in android
.. In my app there is activity for asking questions (Image
of Any Number like 1,2,3,etc.) and Three options in Image Button
([ONE], [TWO], [THREE] and so on) with each pressed button there is a sound also. For each question I created an activity.
Is this a good approach to create an activity for each question? I want the random question what i should do please help me.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:layout_centerHorizontal="true"
tools:context="com.example.app.kids.kidsmaths.ExerciseSeven"
android:background="@drawable/bgn">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ivsz"
android:src="@drawable/seven"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:layout_marginTop="49dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="153dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ib71"
android:background="@drawable/six2"
android:src="@drawable/answer_click_wrong"
android:layout_below="@+id/ib73"
android:layout_alignStart="@+id/ib73" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ib72"
android:src="@drawable/answer_click_wrong"
android:background="@drawable/two2"
android:layout_below="@+id/ivsz"
android:layout_centerHorizontal="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ib73"
android:background="@drawable/seven2"
android:src="@drawable/answer_click_correct"
android:layout_below="@+id/ib72"
android:layout_alignStart="@+id/ib72" />
</RelativeLayout>
activity
package com.example.app.kids.kidsmaths;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ImageView;
public class ExerciseSeven extends AppCompatActivity {
MediaPlayer music71, music72, music73, music74;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exercise_seven);
final ImageView zoom = (ImageView) findViewById(R.id.ivsz);
final Animation zoomAnimation = AnimationUtils.loadAnimation(this, R.anim.zoomseven);
zoom.startAnimation(zoomAnimation);
ImageButton im1 = (ImageButton) findViewById(R.id.ib71);
ImageButton im2 = (ImageButton) findViewById(R.id.ib72);
ImageButton im3 = (ImageButton) findViewById(R.id.ib73);
music71= MediaPlayer.create(ExerciseSeven.this,R.raw.which_number);
music72= MediaPlayer.create(ExerciseSeven.this,R.raw.wrong);
music73= MediaPlayer.create(ExerciseSeven.this,R.raw.correct);
music74= MediaPlayer.create(ExerciseSeven.this,R.raw.wrong2);
music71.start();
im1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
music74.start();
}
});
im2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
music72.start();
}
});
im3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread timer = new Thread(){
public void run(){
try {
sleep(2000);
}catch (InterruptedException e){
e.printStackTrace();
}finally {
Intent i = new Intent(ExerciseSeven.this, ExerciseFour.class);
startActivity(i);
}
}
};
timer.start();
music73.start();
}
});
}
@Override
protected void onPause() {
super.onPause();
music71.release();
}
}
Upvotes: 0
Views: 82
Reputation: 45080
Activities are heavy object you definitely should not use activities for questions as loading lots of activities will make your app run out of memory. Instead you can use ViewPager to display questions. How to use viewpager for slide can found here
https://developer.android.com/training/animation/screen-slide.html
You can find solution best fit for your need here
http://code.tutsplus.com/tutorials/android-ui-workshop-build-an-interactive-quiz-app--mobile-14208
Upvotes: 0
Reputation: 1282
Actually this is a very bad approach.. You just need to create one activity and pass your variables to it through intent like this:
Intent intent = new Intent(this, QuestionActivity.class);
// question is a custom object that implements Serializable
// if you don't wanna create custom class you can just pass your variables as Strings or so..
intent.putExtra("question", question);
startActivity(intent);
and get that in your activity's onCreate:
Question question = (Question) getIntent().getExtras().get("question");
then set your views to have the values of your question
Upvotes: 2