I have a radio button activity with 5 choices for the game. It's a quiz game, so the user can choose to play until he or she makes one error, two errors, tree, four and five errors. My question is....is it better to make 5 activities and 5 classes, so I can call intent of each activity when user check that radio button, or, is better to make one activity, for all five choices and depending on what user chose, to count until 1,2,3,4 or 5 errors? I know how do the first option, but I don't know how to do the second one. Here my choice activity:
public class Izbor extends Activity implements OnClickListener, OnCheckedChangeListener{
MediaPlayer buttonBack;
RadioButton rbDeset,rbDvadeset,rbNeogranicenoTriGreske,rbNeogranicenoJednaGreska,rbNeogranicenoPetGresaka;
Button dNazad, dStart;
RadioGroup rGrupa;
TextView tv1;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.izbor);
addListenerOnButton();
}
private void addListenerOnButton() {
Typeface dugmad = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
Typeface pitanjeVrh = Typeface.createFromAsset(getAssets(), "Lobster.ttf");
rbDeset = (RadioButton) findViewById(R.id.radio1);
rbDvadeset = (RadioButton) findViewById(R.id.radio2);
rbNeogranicenoJednaGreska = (RadioButton) findViewById(R.id.radio3);
rbNeogranicenoTriGreske = (RadioButton) findViewById(R.id.radio4);
rbNeogranicenoPetGresaka = (RadioButton) findViewById(R.id.radio5);
dNazad = (Button) findViewById(R.id.bNazad);
dStart = (Button) findViewById(R.id.bStart);
rGrupa = (RadioGroup) findViewById(R.id.radioGroup1);
buttonBack = MediaPlayer.create(Izbor.this, R.raw.back);
tv1 = (TextView) findViewById(R.id.tv1);
dNazad.setTypeface(dugmad);
dStart.setTypeface(dugmad);
rbDeset.setTypeface(dugmad);
rbDvadeset.setTypeface(dugmad);
rbNeogranicenoPetGresaka.setTypeface(dugmad);
rbNeogranicenoJednaGreska.setTypeface(dugmad);
rbNeogranicenoTriGreske.setTypeface(dugmad);
tv1.setTypeface(pitanjeVrh);
rGrupa.setOnCheckedChangeListener(this);
rbDeset.setOnClickListener(this);
rbDvadeset.setOnClickListener(this);
rbNeogranicenoJednaGreska.setOnClickListener(this);
rbNeogranicenoTriGreske.setOnClickListener(this);
rbNeogranicenoPetGresaka.setOnClickListener(this);
dStart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(rbDeset.isChecked()){
startActivity(new Intent("rs.androidaplikacijekvizopstekulture.KVIZ"));
}else if(rbDvadeset.isChecked()){
startActivity(new Intent("rs.androidaplikacijekvizopstekulture.DVADESETPITANJA"));
}else if(rbNeogranicenoJednaGreska.isChecked()){
startActivity(new Intent("rs.androidaplikacijekvizopstekulture.TRIDESETPITANJA"));
}else if(rbNeogranicenoPetGresaka.isChecked()){
startActivity(new Intent("rs.androidaplikacijekvizopstekulture.NEOGRANICENOPETGRESAKA"));
}
}
});
dNazad.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttonBack.start();
finish();
}
});
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
And here is one activity where a user can play until 5 mistakes:
public class NeogranicenoPetGresaka extends Activity implements OnClickListener{
Button bIzlazIzKviza, bOdgovor1, bOdgovor2, bOdgovor3, bOdgovor4;
TextView question, netacniOdg, score;
int counter = 0;
int brojacPogresnihOdgovora = 0;
int brojacTacnihOdgovora = 0;
Runnable mLaunchTask = new Runnable() {
public void run() {
nextQuestion();
}
};
Runnable mLaunchTaskFinish = new Runnable() {
public void run() {
finish();
}
};
private class Answer {
public Answer(String opt, boolean correct) {
option = opt;
isCorrect = correct;
}
String option;
boolean isCorrect;
}
Handler mHandler = new Handler();
final OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
Answer ans = (Answer) v.getTag();
if (ans.isCorrect) {
brojacTacnihOdgovora++;
Intent i = new Intent("rs.androidaplikacijekvizopstekulture.TACANODGOVOR");
startActivity(i);
mHandler.postDelayed(mLaunchTask,1200);
}
else{
brojacPogresnihOdgovora++;
Intent i = new Intent("rs.androidaplikacijekvizopstekulture.POGRESANODGOVOR");
startActivity(i);
mHandler.postDelayed(mLaunchTask,2200);
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.neograniceno5gresaka);
Typeface dugmad = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
Typeface pitanje = Typeface.createFromAsset(getAssets(), "Lobster.ttf");
bIzlazIzKviza = (Button) findViewById(R.id.bIzlazIzKvizaN);
netacniOdg = (TextView) findViewById(R.id.tvBrojPitanjaN);
question = (TextView) findViewById(R.id.tvPitanjeN);
bOdgovor1 = (Button) findViewById(R.id.bOdgovorN1);
bOdgovor2 = (Button) findViewById(R.id.bOdgovorN2);
bOdgovor3 = (Button) findViewById(R.id.bOdgovorN3);
bOdgovor4 = (Button) findViewById(R.id.bOdgovorN4);
score = (TextView) findViewById(R.id.tvSkor2N);
bOdgovor1.setTypeface(dugmad);
bOdgovor2.setTypeface(dugmad);
bOdgovor3.setTypeface(dugmad);
bOdgovor4.setTypeface(dugmad);
bIzlazIzKviza.setTypeface(dugmad);
netacniOdg.setTypeface(dugmad);
question.setTypeface(pitanje);
score.setTypeface(dugmad);
nextQuestion(); //startuje prvo pitanje!
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
public void nextQuestion() {
TestAdapter mDbHelper = new TestAdapter(this);
mDbHelper.createDatabase();
try{ //Pokusava da otvori db
mDbHelper.open(); //baza otvorena
Cursor c = mDbHelper.getTestData();
List<Answer> labels = new ArrayList<Answer>();
labels.add(new Answer(c.getString(2), true));
labels.add(new Answer(c.getString(3), false));
labels.add(new Answer(c.getString(4), false));
labels.add(new Answer(c.getString(5), false));
Collections.shuffle(labels);
if(brojacPogresnihOdgovora < 5){
question.setText(c.getString(1));
bOdgovor1.setText(labels.get(0).option);
bOdgovor1.setTag(labels.get(0));
bOdgovor1.setOnClickListener(clickListener);
bOdgovor2.setText(labels.get(1).option);
bOdgovor2.setTag(labels.get(1));
bOdgovor2.setOnClickListener(clickListener);
bOdgovor3.setText(labels.get(2).option);
bOdgovor3.setTag(labels.get(2));
bOdgovor3.setOnClickListener(clickListener);
bOdgovor4.setText(labels.get(3).option);
bOdgovor4.setTag(labels.get(3));
bOdgovor4.setOnClickListener(clickListener);
//reset your next question and all four options here
netacniOdg.setText("" + brojacPogresnihOdgovora);
score.setText("Score: " + brojacTacnihOdgovora);
}
else{
Intent i = new Intent("rs.androidaplikacijekvizopstekulture.REZULTAT");
startActivity(i);
mHandler.postDelayed(mLaunchTaskFinish,4000);
}
}
finally{ // kada zavrsi sa koriscenjem baze podataka, zatvara db
mDbHelper.close();
}
bIzlazIzKviza.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
Considering that the choice is just a small setting to the same game I see absolutely no reason to create one class/activity for each choice, it would be sheer madness.
As for the settings part, just create a numeric only input field that accepts any amount of "errors" ( why not ? ).
OnClick get the input from this field, save the value to the intent extras and start the game with the intent. In the onCreate of the game read the intent extras and get the number of "tries"/"errors".
OK, I tried with onNewIntent, but I can only call one method, and I need at least 3. Here's the code in my choice activity with radio buttons(group):
public void onClick(View v) {
if(rbDeset.isChecked()){
Intent intent = new Intent(Izbor.this, Kviz.class);
intent.putExtra("myMethod", "nextQuestion");
startActivity(intent);
And here's my quiz activity:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if(intent.getStringExtra("methodName").equals("nextQuestion")){
nextQuestion();
}
}
I tried with ELSE IF but it didn't work. My methods are named: nextQuestion(), nextQuestion2() and nextQuestion3(). These are pretty much same methods only with different counters (one for 10 questions, one for 20 and one for 30). Maybe I don't need 3 methods to do this but honestly I don't know other way to do that.
Related
i am in a pickle at the moment to how to approach making 3 buttons within a single dialog box make the option selected change the text of the button used to show the dialog box, so far only managed to get option3 to work as i suppose its the last intent so the only button that works would be the button for option3
here is the code im trying to pass to the main button from the dialog
public class LabelDialog extends AppCompatDialogFragment {
private Button option1, option2, option3;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.label_dialog, null);
builder.setView(view);
//option 1 and 2
option3 = view.findViewById(R.id.label_lecturebtn);
option3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String option3 = "Lecture";
Intent intent3 = new Intent(getContext(), AddTask.class);
intent3.putExtra("lecture", option3);
startActivity(intent3);
dismiss();
}
});
and the main code
protected void onCreate(Bundle savedInstanceState) {
//...
label_button = findViewById(R.id.button_labels);
label_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDialog();
}
});
checkLabelResult();
}
public void openDialog() { //Labels dialog
//code to open dialog
}
public void checkLabelResult() {
//intents 1 and 2, same format as 3 but keys changed accordingly
Intent intent3 = getIntent();
String option3 = intent3.getStringExtra("lecture");
label_button.setText(option3);
}
Code for Option 1
option1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String option1 = "Important";
Intent intent = new Intent(getContext(), AddTask.class);
intent.putExtra("important", option1);
startActivity(intent);
dismiss();
}
Code for Option 2
option2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String option2 = "To Do";
Intent intent2 = new Intent(getContext(), AddTask.class);
intent2.putExtra("to do", option2);
startActivity(intent2);
dismiss();
}
I'm new to android. I'm trying to make a quiz app which has categories (history, sports...).
There is only one table with 60 questions in database. First 10 question are from category one(history) and 10 to 20 from category second(sports) and so on. I want that on click of First category only first 10 questions will be fetched and on click on second category only 10 to 20 questions will be fetched. Can we solve this problem passing some value through intent in onclick() and use that value in if else for bounding the questions?
QuestionActivity.java
public class QuestionActivity extends Activity {
List<Question> quesList;
int score = 0;
Random r = new Random();
int qid= (r.nextInt(60) + 0);
Question currentQ;
TextView txtQuestion, times, scored;
Button button1, button2, button3;
CounterClass timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
QuizHelper db = new QuizHelper(this); // my question bank class
quesList = db.getAllQuestions(); // this will fetch all quetonall
questions
currentQ = quesList.get(qid); // the current question
txtQuestion = (TextView) findViewById(R.id.txtQuestion);
// the textview in which the question will be displayed
// the three buttons,
// the idea is to set the text of three buttons with the options from
question bank
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
// the textview in which score will be displayed
scored = (TextView) findViewById(R.id.score);
// the timer
times = (TextView) findViewById(R.id.timers);
// method which will set the things up for our game
setQuestionView();
times.setText("00:00:30");
// A timer of 30 seconds to play for, with an interval of 1 second
(1000 milliseconds)
timer = new CounterClass(30000, 1000);
timer.start();
// button click listeners
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// passing the button text to other method
// to check whether the anser is correct or not
// same for all three buttons
getAnswer(button1.getText().toString());
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getAnswer(button2.getText().toString());
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getAnswer(button3.getText().toString());
}
});
}
public void getAnswer(String AnswerString) {
if (currentQ.getANSWER().equals(AnswerString)) {
// if conditions matches increase the int (score) by 1
// and set the text of the score view
score++;
scored.setText("Score : "+ score);
}
else {
// if unlucky start activity and finish the game
timer.cancel();
Intent intent = new Intent(QuestionActivity.this,
ResultActivity.class);
// passing the int value
Bundle b = new Bundle();
b.putInt("score", score); // Your score
intent.putExtras(b); // Put your score to your next
startActivity(intent);
finish();
}
if (qid < 60) {
// if questions are not over then do this
currentQ = quesList.get(qid);
setQuestionView();
} else {
// if over do this
timer.cancel();
Intent intent = new Intent(QuestionActivity.this,
ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); // Your score
intent.putExtras(b); // Put your score to your next
startActivity(intent);
finish();
}
}
#TargetApi(Build.VERSION_CODES.GINGERBREAD)
#SuppressLint("NewApi")
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
#Override
public void onFinish() {
times.setText("Time is up");
Intent intent = new Intent(QuestionActivity.this,
ResultActivity.class);
timer.cancel();
Bundle b = new Bundle();
b.putInt("score", score); // Your score
intent.putExtras(b); // Put your score to your next
startActivity(intent);
finish();
}
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
long millis = millisUntilFinished;
String hms = String.format(
"%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis)
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis)
-
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
.toMinutes(millis)));
System.out.println(hms);
times.setText(hms);
}
}
private void setQuestionView() {
// the method which will put all things together
txtQuestion.setText(currentQ.getQUESTION());
button1.setText(currentQ.getOPTA());
button2.setText(currentQ.getOPTB());
button3.setText(currentQ.getOPTC());
qid= (r.nextInt(60) + 0);
}
}
Home.java
public class Home extends Activity {
Button play,about,help,quit;
ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
play=(Button)findViewById(R.id.play);
about=(Button)findViewById(R.id.about);
quit=(Button)findViewById(R.id.quit);
help=(Button)findViewById(R.id.help);
play.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(getApplicationContext(),QuestionActivity.class);
startActivity(intent);
finish();
}
});
quit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
about.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),about.class);
startActivity(intent);
}
});
help.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),help.class);
startActivity(intent);
}
});
}
}
After you create an Intent object but before you call startActivity(intent); use: intent.putExtra("name",123); When you want to get the integer in the other activity simply use: int foo = getIntent().getIntExtra("name");
In the activity where you calculated that int value and you would like to go to the other Activity:
int magicVariable = 100;
Intent intent = new Intent(this, OtherClassToGo.class);
intent.putExtra("EXTRA_VALUE_KEY", magicVariable);
startActivity(intent);
In the OtherClassToGo's onCreate() method:
Intent intent = getIntent();
int magicVariableFromOtherClass = intent.getIntExtra("EXTRA_VALUE_KEY");
Of course for "EXTRA_VALUE_KEY" you can use a public static variable so it is easier to "remember" it.
create variable int page and max, use setterPage getterPage page on question activity. and give value max = listquestion.size, and then use set and get value page on your button
I am programming my first android app.
I am trying to create a quiz app. I have questions stored in a SQLite Database, which are displayed one after the other.
The user selects one of the answers (a radio button) and the clicks the 'next button' and the next question is displayed and so on.
Following code shows my Activity file displaying each question one after the other, which was working perfectly.
ACEActivity (old, working version)
public class ACEActivity extends Activity {
List<Question> quesList;
int score = 0;
int qid = 0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc, rdd;
Button butNext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
DatabaseHelper db = new DatabaseHelper(this);
quesList = db.getAllACEQuestions();
currentQ = quesList.get(qid);
txtQuestion = (TextView)findViewById(R.id.textView);
rda = (RadioButton)findViewById(R.id.radio0);
rdb = (RadioButton)findViewById(R.id.radio1);
rdc = (RadioButton)findViewById(R.id.radio2);
rdd = (RadioButton)findViewById(R.id.radio3);
butNext = (Button)findViewById(R.id.nextButton);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RadioGroup grp = (RadioGroup)findViewById(R.id.radioGroup);
RadioButton answer = (RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
// If the correct answer was clicked display the next question
if(currentQ.getANSWER().equals(answer.getText())) {
currentQ = quesList.get(qid);
setQuestionView();
}
}
});
}
// Load the next question
private void setQuestionView() {
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
rdd.setText(currentQ.getOPTD());
qid++;
}
}
In my newer version of this activity I load another activity to give the user immediate feedback, if the correct or wrong answer was clicked.
After displaying the feedback activity I would like to return to this activity and display the next question.
I am trying to do this by passing the question id from the feedback activity (ACECorrectActivity) to this activity (ACEActivity) without any success.
How I tried to solve this problem:
ACEActivity (new version, just working for the first question)
public class ACEActivity extends Activity {
List<Question> quesList;
int score = 0;
int qid = 0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc, rdd;
Button checkBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
// THIS PART IS NEW ================================
// Get the intent
Intent intent = getIntent();
// Get the question id (if there are any extras)
Bundle extras = intent.getExtras();
if (extras != null) {
int qid = extras.getInt("nextQuestionID");
} else {
int qid = 0;
}
// ==================================================
DatabaseHelper db = new DatabaseHelper(this);
quesList = db.getAllACEQuestions();
currentQ = quesList.get(qid);
txtQuestion = (TextView)findViewById(R.id.textView);
rda = (RadioButton)findViewById(R.id.radio0);
rdb = (RadioButton)findViewById(R.id.radio1);
rdc = (RadioButton)findViewById(R.id.radio2);
rdd = (RadioButton)findViewById(R.id.radio3);
checkBtn = (Button) findViewById(R.id.checkButton);
setQuestionView(qid);
checkBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup);
RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
// THIS PART IS NEW AND WORKING FINE ================================
// If the correct answer was clicked
if (currentQ.getANSWER().equals(answer.getText())) {
Intent intent = new Intent(ACEActivity.this, CorrectACEActivity.class);
startActivity(intent);
// If the wrong answer was clicked
} else {
Intent intent = new Intent(ACEActivity.this, FalseACEActivity.class);
startActivity(intent);
}
}
});
}
private void setQuestionView() {
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
rdd.setText(currentQ.getOPTD());
qid++;
}
}
ACECorrectActivity (feedback activity loaded, when the correct answer is chosen and the next button is clicked in the ACEActivity)
public class CorrectACEActivity extends Activity {
List<Question> quesList;
int score = 0;
int qid = 0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc, rdd;
Button nextBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_correct);
DatabaseHelper db = new DatabaseHelper(this);
quesList = db.getAllACEQuestions();
currentQ = quesList.get(qid);
txtQuestion = (TextView) findViewById(R.id.textView);
rda = (RadioButton) findViewById(R.id.radio0);
rdb = (RadioButton) findViewById(R.id.radio1);
rdc = (RadioButton) findViewById(R.id.radio2);
rdd = (RadioButton) findViewById(R.id.radio3);
nextBtn = (Button) findViewById(R.id.nextButton);
// Set colors according to correct answer
rda.setBackgroundColor(Color.RED);
rdb.setBackgroundColor(Color.RED);
rdc.setBackgroundColor(Color.RED);
rdd.setBackgroundColor(Color.RED);
if(currentQ.getANSWER().equals(currentQ.getOPTA())) {
rda.setBackgroundColor(Color.GREEN);
} else if(currentQ.getANSWER().equals(currentQ.getOPTB())) {
rdb.setBackgroundColor(Color.GREEN);
} else if(currentQ.getANSWER().equals(currentQ.getOPTC())) {
rdc.setBackgroundColor(Color.GREEN);
} else if(currentQ.getANSWER().equals(currentQ.getOPTD())) {
rdd.setBackgroundColor(Color.GREEN);
}
setQuestionView();
// WHEN NEXT BUTTON IS CLICKED RETURN TO ACEActivity AND LOAD NEXT QUESTION
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CorrectACEActivity.this, ACEActivity.class);
intent.putExtra("nextQuestionID", currentQ + 1);
startActivity(intent);
}
});
}
private void setQuestionView() {
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
rdd.setText(currentQ.getOPTD());
qid++;
}
}
The first question works perfectly. However, as soon as I return to the ACEActivity after answering the first question, I am presented with the first question once again.
As you can see, I am really new to this and would be tremendously happy for any kind of help! Thank you!!
intent.putExtra("nextQuestionID", currentQ + 1);
you are setting the extra wrong in CorrectACEActivity , shouldn't it be like this?
intent.putExtra("nextQuestionID", qid+ 1);
Hello i'm creating an app to scan buisness cards, it has lot of views but one works very slow it has 19 buttons. Is it possible that slow working of my app is caused by this number of buttons? I've tried to put listeners outside of onCreate but it didn't help. This is my java file conected to this view:
public class Szczegoly extends Activity {
long contactID;
Bitmap contactFoto;
int imagebig=0;
int w;
Contact contact;
EditText tv1;
EditText tv2;
EditText tv3;
EditText tv4;
EditText tv5;
ImageView i;
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
Button b9;
Button b10;
Context to;
Button accept1;
Button accept2;
Button accept3;
Button accept4;
Button accept5;
Button cancel1;
Button cancel2;
Button cancel3;
Button cancel4;
Button cancel5;
Context context;
OnClickListener accept1listener = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==1)
{
contact.savedata_nr(edit_nr,tv1,tv2,tv3,tv4,tv5,context);
closeEdit();
}
}
};
OnClickListener accept2listener = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==2)
{
contact.savedata_nr(edit_nr,tv1,tv2,tv3,tv4,tv5,context);
closeEdit();
}
}
};
OnClickListener accept3listener = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==3)
{
contact.savedata_nr(edit_nr,tv1,tv2,tv3,tv4,tv5,context);
closeEdit();
}
}
};
OnClickListener accept4listener = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==4)
{
contact.savedata_nr(edit_nr,tv1,tv2,tv3,tv4,tv5,context);
closeEdit();
}
}
};
OnClickListener accept5listener = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==5)
{
contact.savedata_nr(edit_nr,tv1,tv2,tv3,tv4,tv5,context);
closeEdit();
}
}
};
OnClickListener cancel1listener = new OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(contact.FirstName);
closeEdit();
}
};
OnClickListener cancel2listener = new OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(contact.FirstName);
closeEdit();
}
};
OnClickListener cancel3listener = new OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(contact.FirstName);
closeEdit();
}
};
OnClickListener cancel4listener = new OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(contact.FirstName);
closeEdit();
}
};
OnClickListener cancel5listener = new OnClickListener() {
#Override
public void onClick(View v) {
tv1.setText(contact.FirstName);
closeEdit();
}
};
OnClickListener b1lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
finish();
}
}
};
OnClickListener b2lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
contact.delete();
finish();
}
}
};
OnClickListener b3lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
}
}
};
OnClickListener b4lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
Uri call = Uri.parse("tel:"+contact.Phone1);
Intent intent = new Intent(Intent.ACTION_CALL, call);
startActivity(intent);
}
}
};
OnClickListener b5lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
}
}
};
OnClickListener b6lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
if(tv1.isEnabled())
{
tv1.setSelected(false);
tv1.setEnabled(false);
}
else
{
cancel1.setVisibility(Button.VISIBLE);
accept1.setVisibility(Button.VISIBLE);
b6.setVisibility(Button.INVISIBLE);
tv1.setEnabled(true);
tv1.setSelected(true);
tv1.setFocusableInTouchMode(true);
tv1.requestFocus();
tv1.setSelection(tv1.getText().length());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
edit_nr= 1;
}
}
};
OnClickListener b7lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0)
{
if(tv2.isEnabled())
tv2.setEnabled(false);
else
{
cancel2.setVisibility(Button.VISIBLE);
accept2.setVisibility(Button.VISIBLE);
b7.setVisibility(Button.INVISIBLE);
tv2.setEnabled(true);
tv2.setSelected(true);
tv2.setFocusableInTouchMode(true);
tv2.requestFocus();
tv2.setSelection(tv2.getText().length());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
edit_nr= 2;
}
}
};
OnClickListener b8lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr == 0)
{
if(tv3.isEnabled())
tv3.setEnabled(false);
else
{
cancel3.setVisibility(Button.VISIBLE);
accept3.setVisibility(Button.VISIBLE);
b8.setVisibility(Button.INVISIBLE);
tv3.setEnabled(true);
tv3.setEnabled(true);
tv3.setSelected(true);
tv3.setFocusableInTouchMode(true);
tv3.requestFocus();
tv3.setSelection(tv3.getText().length());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
edit_nr=3;
}
}
};
OnClickListener b9lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr ==0 )
{
if(tv4.isEnabled())
tv4.setEnabled(false);
else
{
cancel4.setVisibility(Button.VISIBLE);
accept4.setVisibility(Button.VISIBLE);
b9.setVisibility(Button.INVISIBLE);
tv4.setEnabled(true);
tv4.setEnabled(true);
tv4.setSelected(true);
tv4.setFocusableInTouchMode(true);
tv4.requestFocus();
tv4.setSelection(tv4.getText().length());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
edit_nr= 4;
}
}
};
OnClickListener b10lisner = new OnClickListener() {
#Override
public void onClick(View v) {
if(edit_nr==0)
{
if(tv5.isEnabled())
tv5.setEnabled(false);
else
{
cancel5.setVisibility(Button.VISIBLE);
accept5.setVisibility(Button.VISIBLE);
b10.setVisibility(Button.INVISIBLE);
tv5.setEnabled(true);
tv5.setEnabled(true);
tv5.setSelected(true);
tv5.setFocusableInTouchMode(true);
tv5.requestFocus();
tv5.setSelection(tv5.getText().length());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null)
{
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
edit_nr= 5;
}
}
};
int edit_nr=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_szczegoly);
Log.d("dupa","dupa1");
context = this;
Intent intent = getIntent();
contactID = intent.getLongExtra("contactID", 0);
contact = new Contact(contactID, this);
tv1 = (EditText) findViewById(R.id.editText1);
tv2 = (EditText) findViewById(R.id.editText2);
tv3 = (EditText) findViewById(R.id.editText3);
tv4 = (EditText) findViewById(R.id.editText4);
tv5 = (EditText) findViewById(R.id.editText5);
i = (ImageView) findViewById(R.id.imageView1);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
b5 = (Button) findViewById(R.id.button5);
b6 = (Button) findViewById(R.id.button6);
b7 = (Button) findViewById(R.id.button7);
b8 = (Button) findViewById(R.id.button8);
b9 = (Button) findViewById(R.id.button9);
b10 = (Button) findViewById(R.id.button10);
tv1.setText(contact.FirstName);
tv2.setText(contact.LastName);
tv3.setText(contact.Phone1);
tv4.setText(contact.Phone2);
tv5.setText(contact.EmaiL);
accept1 = (Button) findViewById(R.id.accept1);
accept2 = (Button) findViewById(R.id.accept2);
accept3 = (Button) findViewById(R.id.accept3);
accept4 = (Button) findViewById(R.id.accept4);
accept5 = (Button) findViewById(R.id.accept5);
cancel1 =(Button) findViewById(R.id.cancel1);
cancel2 =(Button) findViewById(R.id.cancel2);
cancel3 =(Button) findViewById(R.id.cancel3);
cancel4 =(Button) findViewById(R.id.cancel4);
cancel5 =(Button) findViewById(R.id.cancel5);
}
public void closeEdit()
{
tv1.setSelected(false);
tv1.setEnabled(false);
tv2.setSelected(false);
tv2.setEnabled(false);
tv3.setSelected(false);
tv3.setEnabled(false);
tv4.setSelected(false);
tv4.setEnabled(false);
tv5.setSelected(false);
tv5.setEnabled(false);
cancel1.setVisibility(Button.INVISIBLE);
accept1.setVisibility(Button.INVISIBLE);
b6.setVisibility(Button.VISIBLE);
cancel2.setVisibility(Button.INVISIBLE);
accept2.setVisibility(Button.INVISIBLE);
b7.setVisibility(Button.VISIBLE);
cancel3.setVisibility(Button.INVISIBLE);
accept3.setVisibility(Button.INVISIBLE);
b8.setVisibility(Button.VISIBLE);
cancel4.setVisibility(Button.INVISIBLE);
accept4.setVisibility(Button.INVISIBLE);
b9.setVisibility(Button.VISIBLE);
cancel5.setVisibility(Button.INVISIBLE);
accept5.setVisibility(Button.INVISIBLE);
b10.setVisibility(Button.VISIBLE);
edit_nr=0;
}
public void onResume() {
super.onResume();
Log.d("dupa","dupa");
Point size = new Point();
Display display = getWindowManager().getDefaultDisplay();
display.getSize(size);
to = this;
w = size.x;
contactFoto = Bitmap.createScaledBitmap(contact.photo, w, (int) (w*contact.photo.getHeight()/contact.photo.getWidth()), true);
i.setImageBitmap(contactFoto);
accept1.setOnClickListener(accept1listener);
accept2.setOnClickListener(accept2listener);
accept3.setOnClickListener(accept3listener);
accept4.setOnClickListener(accept4listener);
accept5.setOnClickListener(accept5listener);
cancel1.setOnClickListener(cancel1listener);
cancel2.setOnClickListener(cancel2listener);
cancel3.setOnClickListener(cancel3listener);
cancel4.setOnClickListener(cancel4listener);
cancel5.setOnClickListener(cancel5listener);
b1.setOnClickListener(b1lisner);
b2.setOnClickListener(b2lisner);
b3.setOnClickListener(b3lisner);
b4.setOnClickListener(b4lisner);
b5.setOnClickListener(b5lisner);
b6.setOnClickListener(b6lisner);
b7.setOnClickListener(b7lisner);
b8.setOnClickListener(b8lisner);
b9.setOnClickListener(b9lisner);
b10.setOnClickListener(b10lisner);
}
}
The number of buttons by itself is not a likely cause of slowness. However, from the code it seems as if many actions from listeners (ie contact.savedata_nr) might be running on the main/UI thread which could cause slowness, depending on what happens when these methods are called. As mentioned in the comment, creating Bitmap might be better task to be carried out in the background as well.
To fix this, you should review your code and see what can be run in the background thread instead. Operations like saving can be done on the background thread, and then any updates to the GUI after these should be carried out on UI thread. If the background operation takes a long time, it's a good idea to up a progress indicator to show the user that something is happening and that the screen isn't just stuck.
You can read about Android threading here.
The basic example would be:
new Thread(new Runnable()
{
public void run()
{
//put background code here
contact.save(...)
//after background task is finished, update the UI
runOnUiThread(new Runnable()
{
#Override
public void run()
{
//put UI code here
textview.setText(...);
}
});
}
}).start();
In android app coded for common method for all buttons click event, here is the code,
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.filter);
btnOne = (Button)findViewById(R.id.btnone);
btnTwo = (Button)findViewById(R.id.btntwo);
btnThree = (Button)findViewById(R.id.btnthree);
btnFour = (Button)findViewById(R.id.btnfour);
btnFive = (Button)findViewById(R.id.btnfive);
btnSix = (Button)findViewById(R.id.btnsix);
btnSeven = (Button)findViewById(R.id.btnseven);
btnEight = (Button)findViewById(R.id.btneight);
btnNine = (Button)findViewById(R.id.btnnine);
btnTen = (Button)findViewById(R.id.btnten);
OnClickListener listener = new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
doAction(v);
}
};
}
public void doAction(View v)
{
Object tagObject = v.getTag();
int tag = (Integer) v.getTag();
String val = (String) d.get(tag);
if(val.equals("off"))
{
//select(tagObject);
//d.put(tag, "on");
Toast.makeText(getBaseContext(), "Button"+tag+"select", Toast.LENGTH_LONG).show();
}
else if(val.equals("on"))
{
//unSelect(tagObject);
//d.put(tag, "off");
Toast.makeText(getBaseContext(), "Button"+tag+"unselect", Toast.LENGTH_LONG).show();
}
}
This code is not working for me. Please give any idea....... Thanks in advance
How about declaring your listener first and then calling setOnClickListener on your views :
OnClickListener listener = new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
doAction(v);
}
};
btnOne.setOnClickListener(listener);
btnTwo.setOnClickListener(listener);
...
You have declared the listener but you forgot to set the listener for each button.
Do this for all buttons : btnOne.setOnClickListener(listener);
You need to set the listener in the button..
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.filter);
...
OnClickListener listener = new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
doAction(v);
}
};
btnOne.setOnClickListener();
btnTwo = (Button)findViewById(R.id.btntwo);
...
}
Have your class implement View.OnClickListener, like
public class MyActivity extends Activity implements View.OnClickListener {
Button button1, button2, button3;
#Override
public void onCreate(Bundle bundle) {
super.onCreate();
...
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
// do stuff;
break;
case R.id.button2:
// do stuff;
break;
...
}
}