Get Resource Id from ImageView - android

I am trying to develop a little game.
I have a ViewFlipper which has 50 pictures (random frequence of 4 pictures) in ImageViews. Then I have 4 Buttons with the same 4 pictures which can appear in the ViewFlipper.
The task is to click the right button when the right picture appears.
(When Picture 1 appears Button 1 has to be pressed and so on)
My Problem is I don't know how to get the displayed ImageView id.
flipper.getCurrentView().getId()
gives me "-1" as Id. But I want to have the Id of "R.drawable.pic1"
My code so far:
my Loader-Method:
protected void loadPicturesIntoFlipper() {
Random generator = new Random();
pictures = new ArrayList();
for(int i = 0; i < 50;i++){
int number = generator.nextInt(4) + 1;
if(number == 1){
pic = R.drawable.pic1;
}
if(number == 2){
pic = R.drawable.pic2;
}
if(number == 3){
pic = R.drawable.pic3;
}
if(number == 4){
pic = R.drawable.pic4;
}
pictures.add(pic);
}
for(int i=0;i<pictures.size();i++)
{
setFlipperImage((Integer) pictures.get(i));
}
}
My Insert-Method:
private void setFlipperImage(int res) {
image = new ImageView(getApplicationContext());
image.setBackgroundResource(res);
flipper.addView(image);
}
My Check-Method :
protected void check(int number, int id) {
int code = 0;;
if(number == 1){
code = R.drawable.button_tip_finder;
}
if(number == 2){
code = R.drawable.button_about_us;
}
if(number == 3){
code = R.drawable.button_power_calculator;
}
if(number == 4){
code = R.drawable.button_powerpedia;
}
if(code == id){
test.setText(""+id);
}
else{
test.setText(""+id);
}
}
I call it like:
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
check(1,flipper.getCurrentView().getId());
flipper.showNext();
}
});

Do it like this:
private void setFlipperImage(int res) {
image = new ImageView(getContext());
image.setBackgroundResource(res);
image.setTag(res); //<------
flipper.addView(image);
}
and then:
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
check(1,(Integer)flipper.getCurrentView().getTag());//<----
flipper.showNext();
}
});
BTW use else in all of your code please, E.g:
if(number == 1){
pic = R.drawable.pic1;
} else if(number == 2){
pic = R.drawable.pic2;
} else if(number == 3){
pic = R.drawable.pic3;
}

may this one helps you,
int icon = getResources().getIdentifier([YOUR IMAGE NAME], "drawable",
getPackageName());

Related

How to solve this scenario on Android Studio?

Click on red button 2 times and green button 3 times after this click on yellow button 5 times. If this done successfully you go on next screen for next quiz.
Assuming your buttons already connected to corresponding functions in Android Manifest
private int redClicks = 0;
private int greenClicks = 0;
private int yellowClicks = 0;
public void redClickCount(View view) {
if (redClicks < 2){
redClicks++;
}
greenClicks = 0;
yellowClicks = 0;
}
public void greenClickCount(View view) {
if (redClicks == 2 && greenClicks < 3){
greenClicks++;
} else { //start all over again
redClicks = 0;
greenClicks = 0;
yellowClicks = 0;
}
}
public void yellowClickCount(View view) {
if (redClicks == 2 && greenClicks == 3 && yellowClicks <5){
yellowClicks++;
} else {
if (redClicks == 2 && greenClicks == 3 && yellowClicks = 5){
// go to next round
} else {
//start all over again
redClicks = 0;
greenClicks = 0;
yellowClicks = 0;
}
}
}

How to calculate the values with using TextWatcher

I want to calculate totals of T,T2,T3,T4 which i given in calculate method.
how to calculate this values using TextWatcher
and if i don't put any value in calculate 3 () method. how can i store 0 value in T3.
for example
T= 2*3*1/144*2 = 0.08333
T2=2*3*1/144*2 = 0.08333
T3= if no any num of value put =0
T4=2*3*1/144*2 = 0.08333
fullTotal = 0.2499
I want this type of output. please help me
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Edtinch1 = (EditText)findViewById(R.id.inch1);
Edtinch2 = (EditText)findViewById(R.id.inch2);
Edtfeet = (EditText)findViewById(R.id.feet);
Edtquan = (EditText)findViewById(R.id.qunt);
Totaltxt1= (EditText)findViewById(R.id.total1);
Edtquan.addTextChangedListener(this);
Edtinch21 = (EditText)findViewById(R.id.inch21);
Edtinch22 = (EditText)findViewById(R.id.inch22);
Edtfeet2 = (EditText)findViewById(R.id.feet2);
Edtquan2 = (EditText)findViewById(R.id.qunt2);
Totaltxt2= (EditText)findViewById(R.id.total2);
Edtquan2.addTextChangedListener(this);
Edtinch31 = (EditText)findViewById(R.id.inch31);
Edtinch32 = (EditText)findViewById(R.id.inch32);
Edtfeet3 = (EditText)findViewById(R.id.feet3);
Edtquan3 = (EditText)findViewById(R.id.qunt3);
Totaltxt3= (EditText)findViewById(R.id.total3);
Edtquan3.addTextChangedListener(this);
fulltotal1=(EditText)findViewById(R.id.fulltotal);
btnsnap = (Button)findViewById(R.id.btncapture);
btnsnap.setOnClickListener(this);
}
public void calculate(){
//get entered texts from the edittexts,and convert to integers.
inch11 = Double.parseDouble(Edtinch1.getText().toString());
inch12 = Double.parseDouble(Edtinch2.getText().toString());
feet11 = Double.parseDouble(Edtfeet.getText().toString());
quan11 = Double.parseDouble(Edtquan.getText().toString());
//do the calculation
Double calculated1 = (inch11*inch12)*(feet11)/144*(quan11);
//set the value to the textview, to display on screen.
T= calculated1.toString();
Totaltxt1.setText(T);
}
public void calculate2(){
//get entered texts from the edittexts,and convert to integers.
inch21 = Double.parseDouble(Edtinch21.getText().toString());
inch22 = Double.parseDouble(Edtinch22.getText().toString());
feet2 = Double.parseDouble(Edtfeet2.getText().toString());
quan2 = Double.parseDouble(Edtquan2.getText().toString());
//do the calculation
Double calculated2 = (inch21*inch22)*(feet2)/144*(quan2);
//set the value to the textview, to display on screen.
T2= calculated2.toString();
Totaltxt2.setText(T2);
}
public void calculate3(){
//get entered texts from the edittexts,and convert to integers.
inch31 = Double.parseDouble(Edtinch31.getText().toString());
inch32 = Double.parseDouble(Edtinch32.getText().toString());
feet3 = Double.parseDouble(Edtfeet3.getText().toString());
quan3 = Double.parseDouble(Edtquan3.getText().toString());
//do the calculation
Double calculated3 = (inch31*inch32)*(feet3)/144*(quan3);
//set the value to the textview, to display on screen.
T3= calculated3.toString();
Totaltxt3.setText(T3);
}
public void calculate4(){
//get entered texts from the edittexts,and convert to integers.
inch41 = Double.parseDouble(Edtinch41.getText().toString());
inch42 = Double.parseDouble(Edtinch42.getText().toString());
feet4 = Double.parseDouble(Edtfeet4.getText().toString());
quan4 = Double.parseDouble(Edtquan4.getText().toString());
//do the calculation
Double calculated4 = (inch41*inch42)*(feet4)/144*(quan4);
//set the value to the textview, to display on screen.
T4= calculated4.toString();
Totaltxt4.setText(T4);
}
public void fullcalculate()
{
}
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int arg1, int arg2,
int arg3) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if (Edtinch1.getText().toString().length() <= 0)
{
Edtinch1.setError("Input Inch");
}
else if (Edtinch2.getText().toString().length() <= 0)
{
Edtinch2.setError("Input Inch");
}
else if (Edtfeet.getText().toString().length() <= 0)
{
Edtfeet.setError("Input feet");
}
else if (Edtquan.getText().toString().length() <= 0) {
Edtquan.setError("Input Quantity");
}
else
{
if (Edtquan.getText().hashCode() == s.hashCode())
{
calculate();
}
}
if (Edtinch21.getText().toString().length() <= 0)
{
Edtinch21.setError("Input Inch");
}
else if (Edtinch22.getText().toString().length() <= 0)
{
Edtinch22.setError("Input Inch");
}
else if (Edtfeet2.getText().toString().length() <= 0)
{
Edtfeet2.setError("Input feet");
}
else if (Edtquan2.getText().toString().length() <= 0) {
Edtquan2.setError("Input Quantity");
}
else
{
if (Edtquan2.getText().hashCode() == s.hashCode())
{
calculate2();
}
}
if (Edtinch31.getText().toString().length() <= 0)
{
Edtinch31.setError("Input Inch");
}
else if (Edtinch32.getText().toString().length() <= 0)
{
Edtinch32.setError("Input Inch");
}
else if (Edtfeet3.getText().toString().length() <= 0)
{
Edtfeet3.setError("Input feet");
}
else if (Edtquan3.getText().toString().length() <= 0) {
Edtquan3.setError("Input Quantity");
}
else
{
if (Edtquan3.getText().hashCode() == s.hashCode())
{
calculate3();
}
}
if (Edtinch41.getText().toString().length() <= 0)
{
Edtinch41.setError("Input Inch");
}
else if (Edtinch42.getText().toString().length() <= 0)
{
Edtinch42.setError("Input Inch");
}enter code here
else if (Edtfeet4.getText().toString().length() <= 0)
{
Edtfeet4.setError("Input feet");
}
else if (Edtquan4.getText().toString().length() <= 0) {
Edtquan4.setError("Input Quantity");
}
else
{
if (Edtquan4.getText().hashCode() == s.hashCode())
{
calculate4();
}
}
}

How to enable and disable listview items based on my quiz score?

I have my List-view Item: Easy Medium Hard.
How am I going to Disable Medium and Hard? . and how to Enable it after
reaching certain score on "Easy" quiz . Please Help me guys. :(
This is my code.
String classes[] = {"Easy", "Medium", "Hard" };
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String easy = classes[position];
try{
Class openClass = Class.forName("first.project." + easy);
Intent openIntent = new Intent(this , openClass);
startActivity(openIntent);
}
catch (ClassNotFoundException e){
e.printStackTrace();
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
This is the code on my QuestionActivity
public class QuestionActivity extends Activity {
// Called when the activity is first created.
EditText question = null;
RadioButton answer1 = null;
RadioButton answer2 = null;
RadioButton answer3 = null;
RadioButton answer4 = null;
RadioGroup answers = null;
Button finish = null;
int selectedAnswer = -1;
int quesIndex = 0;
int numEvents = 0;
int selected[] = null;
int correctAns[] = null;
boolean review =false;
Button prev, next = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
TableLayout quizLayout = (TableLayout) findViewById(R.id.quizLayout);
quizLayout.setVisibility(android.view.View.INVISIBLE);
try {
question = (EditText) findViewById(R.id.question);
answer1 = (RadioButton) findViewById(R.id.a0);
answer2 = (RadioButton) findViewById(R.id.a1);
answer3 = (RadioButton) findViewById(R.id.a2);
answer4 = (RadioButton) findViewById(R.id.a3);
answers = (RadioGroup) findViewById(R.id.answers);
RadioGroup questionLayout = (RadioGroup)findViewById(R.id.answers);
Button finish = (Button)findViewById(R.id.finish);
finish.setOnClickListener(finishListener);
prev = (Button)findViewById(R.id.Prev);
prev.setOnClickListener(prevListener);
next = (Button)findViewById(R.id.Next);
next.setOnClickListener(nextListener);
selected = new int[Easy.getQuesList().length()];
java.util.Arrays.fill(selected, -1);
correctAns = new int[Easy.getQuesList().length()];
java.util.Arrays.fill(correctAns, -1);
this.showQuestion(0,review);
quizLayout.setVisibility(android.view.View.VISIBLE);
} catch (Exception e) {
Log.e("", e.getMessage().toString(), e.getCause());
}
}
private void showQuestion(int qIndex,boolean review) {
try {
JSONObject aQues = Easy.getQuesList().getJSONObject(qIndex);
String quesValue = aQues.getString("Question");
if (correctAns[qIndex] == -1) {
String correctAnsStr = aQues.getString("CorrectAnswer");
correctAns[qIndex] = Integer.parseInt(correctAnsStr);
}
question.setText(quesValue.toCharArray(), 0, quesValue.length());
answers.check(-1);
answer1.setTextColor(Color.WHITE);
answer2.setTextColor(Color.WHITE);
answer3.setTextColor(Color.WHITE);
answer4.setTextColor(Color.WHITE);
JSONArray ansList = aQues.getJSONArray("Answers");
String aAns = ansList.getJSONObject(0).getString("Answer");
answer1.setText(aAns.toCharArray(), 0, aAns.length());
aAns = ansList.getJSONObject(1).getString("Answer");
answer2.setText(aAns.toCharArray(), 0, aAns.length());
aAns = ansList.getJSONObject(2).getString("Answer");
answer3.setText(aAns.toCharArray(), 0, aAns.length());
aAns = ansList.getJSONObject(3).getString("Answer");
answer4.setText(aAns.toCharArray(), 0, aAns.length());
Log.d("",selected[qIndex]+"");
if (selected[qIndex] == 0)
answers.check(R.id.a0);
if (selected[qIndex] == 1)
answers.check(R.id.a1);
if (selected[qIndex] == 2)
answers.check(R.id.a2);
if (selected[qIndex] == 3)
answers.check(R.id.a3);
setScoreTitle();
if (quesIndex == (Easy.getQuesList().length()-1))
next.setEnabled(false);
if (quesIndex == 0)
prev.setEnabled(false);
if (quesIndex > 0)
prev.setEnabled(true);
if (quesIndex < (Easy.getQuesList().length()-1))
next.setEnabled(true);
if (review) {
Log.d("review",selected[qIndex]+""+correctAns[qIndex]);;
if (selected[qIndex] != correctAns[qIndex]) {
if (selected[qIndex] == 0)
answer1.setTextColor(Color.RED);
if (selected[qIndex] == 1)
answer2.setTextColor(Color.RED);
if (selected[qIndex] == 2)
answer3.setTextColor(Color.RED);
if (selected[qIndex] == 3)
answer4.setTextColor(Color.RED);
}
if (correctAns[qIndex] == 0)
answer1.setTextColor(Color.GREEN);
if (correctAns[qIndex] == 1)
answer2.setTextColor(Color.GREEN);
if (correctAns[qIndex] == 2)
answer3.setTextColor(Color.GREEN);
if (correctAns[qIndex] == 3)
answer4.setTextColor(Color.GREEN);
}
} catch (Exception e) {
Log.e(this.getClass().toString(), e.getMessage(), e.getCause());
}
}
private OnClickListener finishListener = new OnClickListener() {
public void onClick(View v) {
setAnswer();
//Calculate Score
int score = 0;
for(int i=0; i<correctAns.length; i++){
if ((correctAns[i] != -1) && (correctAns[i] == selected[i]))
score++;
}
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(QuestionActivity.this).create();
alertDialog.setTitle("Score");
alertDialog.setMessage((score) +" out of " + (Easy.getQuesList().length()));
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Retake", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
review = false;
quesIndex=0;
QuestionActivity.this.showQuestion(0, review);
}
});
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Review", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
review = true;
quesIndex=0;
QuestionActivity.this.showQuestion(0, review);
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,"Quit", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
review = false;
finish();
}
});
alertDialog.show();
}
};
private void setAnswer() {
if (answer1.isChecked())
selected[quesIndex] = 0;
if (answer2.isChecked())
selected[quesIndex] = 1;
if (answer3.isChecked())
selected[quesIndex] = 2;
if (answer4.isChecked())
selected[quesIndex] = 3;
Log.d("",Arrays.toString(selected));
Log.d("",Arrays.toString(correctAns));
}
private OnClickListener nextListener = new OnClickListener() {
public void onClick(View v) {
setAnswer();
quesIndex++;
if (quesIndex >= Easy.getQuesList().length())
quesIndex = Easy.getQuesList().length() - 1;
showQuestion(quesIndex,review);
}
};
private OnClickListener prevListener = new OnClickListener() {
public void onClick(View v) {
setAnswer();
quesIndex--;
if (quesIndex < 0)
quesIndex = 0;
showQuestion(quesIndex,review);
}
};
private void setScoreTitle() {
this.setTitle("Question # " + (quesIndex+1)+ "/" + Easy.getQuesList().length());
}
#Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("Warning!!")
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
Use a loop like so:
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
adapter.remove(adapter.getItem(i));
}
I think that should work
XcutionX
this what i understood, what you are going to do is:
First of all the 'easy' button will be enabled and the rest of two will be disabled (I guess).
buttonEasy.setEnabled(true);
buttonMediun.setEnabled(false);
buttonHard.setEnabled(false);
then after the quiz with 'easy' level is finished, check the score if it has reached a certain value. If yes, then you will enable the 'Medium' button with the code:
buttonMedium.setEnabled(true);

My activity won't pause when dialog is shown

I have been working on application that displays a problem and 4 possible answers. Answer correctly, and the next problem appears. A wrong answer or letting the timer run out results in a popup that tells the correct answer and that you just lost a "life". Everything works well except that when the popup appears, the countdown timer continues and generates another popup. I can't find a way to get the activity to wait for the button on the dialog to be pressed before continuing to the next problem. I have read many questions/answers here and combed through many pages on the android developers site. Any help with this problem would be greatly appreciated.
public class MainActivity extends Activity {
String problems[][] = {{},{"Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10"},
{"Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10"}};
String answers[][] = {{},{"A1","A2","A3","A4","A5","A6","A7","A8","A9","A10"},
{"A1","A2","A3","A4","A5","A6","A7","A8","A9","A10"}};
public int level;
private int lister[] = {1,1,2,3,4};
Random rand = new Random();
int holder, probcount = 0, score = 0, lives = 5;
String problem, answer1, answer2, answer3, answer4, corrAnswer, l1, l2, l3;
boolean correct = false, changeBG = false, timeup = false, nolives = false, inpopup = false;
MyCount counter;
View lv, lf1, lf2, lf3, lf4, lf5;
MediaPlayer soundright, soundwrong;
final Context context = this;
public 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.main);
lv = findViewById(R.id.quizshow);
lf1 = findViewById(R.id.Life1);
lf2 = findViewById(R.id.Life2);
lf3 = findViewById(R.id.Life3);
lf4 = findViewById(R.id.Life4);
lf5 = findViewById(R.id.Life5);
Log.v("Events","onCreate");
soundright = MediaPlayer.create(this, R.raw.correct1);
soundwrong = MediaPlayer.create(this, R.raw.correct2);
Button btnAnswer1 = (Button)findViewById(R.id.Answer1);
btnAnswer1.setOnClickListener(onAnswer1);
Button btnAnswer2 = (Button)findViewById(R.id.Answer2);
btnAnswer2.setOnClickListener(onAnswer2);
Button btnAnswer3 = (Button)findViewById(R.id.Answer3);
btnAnswer3.setOnClickListener(onAnswer3);
Button btnAnswer4 = (Button)findViewById(R.id.Answer4);
btnAnswer4.setOnClickListener(onAnswer4);
Button btnExit = (Button)findViewById(R.id.btnExit);
btnExit.setOnClickListener(onExit);
Log.v("Events","onCreate2");
TextView sc = (TextView) findViewById(R.id.Score);
sc.setText(String.valueOf(score));
level = 1;
doNext();
}
protected void onResume() {
Log.d("Events", "onResume");
super.onResume();
}
protected void onPause() {
Log.d("Events", "onPause");
super.onPause();
counter.cancel();
}
protected void onDestroy() {
Log.d("Events", "onDestroy");
super.onDestroy();
}
private View.OnClickListener onAnswer1=new View.OnClickListener() {
public void onClick(View v) {
Log.v("Events", "onAnswer1");
if (lister[1] == lister[0]) {
doRight();
}
else {
doWrong();
}
};
};
private View.OnClickListener onAnswer2=new View.OnClickListener() {
public void onClick(View v) {
Log.v("Events", "onAnswer2");
if (lister[2] == lister[0]) {
doRight();
}
else {
doWrong();
}
};
};
private View.OnClickListener onAnswer3=new View.OnClickListener() {
public void onClick(View v) {
Log.v("Events", "onAnswer3");
if (lister[3] == lister[0]) {
doRight();
}
else {
doWrong();
}
};
};
private View.OnClickListener onAnswer4=new View.OnClickListener() {
public void onClick(View v) {
Log.v("Events", "onAnswer4");
if (lister[4] == lister[0]) {
doRight();
}
else {
doWrong();
}
};
};
private View.OnClickListener onExit = new View.OnClickListener() {
public void onClick(View v) {
Log.v("Events", "onExit");
finish();
};
};
private void doNext() {
if (changeBG == true) {
l1 = getResources().getString(R.string.LevelDone1);
l2 = getResources().getString(R.string.LevelDone2)+" "+String.valueOf(level);
l3 = getResources().getString(R.string.LevelDone3);
doPopup();
changeBG = false;
level = level + 1;
score = 0;
probcount = 0;
}
Log.v("Events", "DoNext1");
probcount = probcount + 1;
lister[0] = rand.nextInt(9);
holder = rand.nextInt(3);
lister[holder+1] = lister[0];
This is where I have boring code that makes sure that the 4 answers are different.
problem = problems[level][lister[0]];
TextView pr = (TextView) findViewById(R.id.Problem);
pr.setText(problem);
answer1 = answers[level][lister[1]];
TextView a1 = (TextView) findViewById(R.id.Answer1);
a1.setText(answer1);
answer2 = answers[level][lister[2]];
TextView a2 = (TextView) findViewById(R.id.Answer2);
a2.setText(answer2);
answer3 = answers[level][lister[3]];
TextView a3 = (TextView) findViewById(R.id.Answer3);
a3.setText(answer3);
answer4 = answers[level][lister[4]];
TextView a4 = (TextView) findViewById(R.id.Answer4);
a4.setText(answer4);
corrAnswer = answers[level][lister[0]];
if (probcount < 6){
counter = new MyCount(4000, 1);
} else if (probcount < 11) {
counter = new MyCount(3000, 1);
} else if (probcount < 21) {
counter = new MyCount(2500, 1);
} else if (probcount < 31) {
counter = new MyCount(2000, 1);
} else if (probcount < 41) {
counter = new MyCount(1500, 1);
} else if (probcount < 51) {
counter = new MyCount(1000, 1);
} else {
counter = new MyCount(750, 1);
}
counter.start();
}
private void doRight() {
Log.v("Events", "DoRight");
counter.cancel();
soundright.start();
if (probcount < 6){
score = score +10;
} else if (probcount < 11) {
score = score + 20;
} else if (probcount < 21) {
score = score +30;
} else if (probcount < 31) {
score = score +40;
} else if (probcount < 41) {
score = score + 50;
} else if (probcount < 51) {
score = score + 100;
} else {
score = score + 200;
}
TextView sc = (TextView) findViewById(R.id.Score);
sc.setText(String.valueOf(score));
if (score > 590) {
changeBG = true;
}
doNext();
}
private void doWrong() {
Log.v("Events", "DoWrong");
counter.cancel();
soundwrong.start();
lives = lives - 1;
if (lives != 5) {
if (lives == 4) {
lf5.setVisibility(View.INVISIBLE);
} else if (lives == 3) {
lf4.setVisibility(View.INVISIBLE);
} else if (lives == 2) {
lf3.setVisibility(View.INVISIBLE);
} else if (lives == 1) {
lf2.setVisibility(View.INVISIBLE);
} else if (lives == 0) {
lf1.setVisibility(View.INVISIBLE);
nolives = true;
}
}
if (nolives) {
l1 = getResources().getString(R.string.LivesGone1);
l2 = getResources().getString(R.string.LivesGone2);
l3 = getResources().getString(R.string.LivesGone3);
} else {
if (timeup) {
l1 = getResources().getString(R.string.TimeExpired);
timeup = false;
} else {
l1 = getResources().getString(R.string.WrongChoice);
}
l2 = getResources().getString(R.string.LifeLost);
l3 = getResources().getString(R.string.CorrAnswer)+" "+corrAnswer;
}
doPopup();
if (nolives) {
score = 0;
probcount = 0;
TextView sc = (TextView) findViewById(R.id.Score);
sc.setText(String.valueOf(score));
lf1.setVisibility(View.VISIBLE);
lf2.setVisibility(View.VISIBLE);
lf3.setVisibility(View.VISIBLE);
lf4.setVisibility(View.VISIBLE);
lf5.setVisibility(View.VISIBLE);
nolives = false;
}
doNext();
}
private void doPopup() {
Log.v("Events", "DoPopup");
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.popup);
TextView ln1 = (TextView) dialog.findViewById(R.id.Line1);
TextView ln2 = (TextView) dialog.findViewById(R.id.Line2);
TextView ln3 = (TextView) dialog.findViewById(R.id.Line3);
ln1.setText(l1);
ln2.setText(l2);
ln3.setText(l3);
inpopup = true;
dialog.show();
Button dialogButton = (Button) dialog.findViewById(R.id.btnPopup);
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
inpopup = false;
dialog.dismiss();
}
});
}
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
timeup = true;
doWrong();
}
#Override
public void onTick(long millisUntilFinished) {
double TimeLeft = millisUntilFinished;
TextView timer1 = (TextView) findViewById(R.id.Timer);
timer1.setText(String.valueOf(TimeLeft/1000));
}
}
}
You have a repetitive loop insisde a timer...
Let say, when your timer is is finished, it will call onFinish() and you are calling doWrong() in onFinish(). and doWrong() is calling doPopUp() which will open a Dialog again.(repetitively). To avoid, maintain some flags(like boolean or int) and based on the value of the flag, you decide whether to show PopUp Dialog when timer is finished or cancel the Timer when you are dismissing Diaog...
private void doWrong() {
Log.v("Events", "DoWrong");
counter.cancel();
soundwrong.start();
lives = lives - 1;
if (lives != 5) {
if (lives == 4) {
lf5.setVisibility(View.INVISIBLE);
} else if (lives == 3) {
lf4.setVisibility(View.INVISIBLE);
} else if (lives == 2) {
lf3.setVisibility(View.INVISIBLE);
} else if (lives == 1) {
lf2.setVisibility(View.INVISIBLE);
} else if (lives == 0) {
lf1.setVisibility(View.INVISIBLE);
nolives = true;
}
}
if (nolives) {
l1 = getResources().getString(R.string.LivesGone1);
l2 = getResources().getString(R.string.LivesGone2);
l3 = getResources().getString(R.string.LivesGone3);
} else {
if (timeup) {
l1 = getResources().getString(R.string.TimeExpired);
timeup = false;
} else {
l1 = getResources().getString(R.string.WrongChoice);
}
l2 = getResources().getString(R.string.LifeLost);
l3 = getResources().getString(R.string.CorrAnswer)+" "+corrAnswer;
}
// change here
if(!timeup)
doPopup();
if (nolives) {
score = 0;
probcount = 0;
TextView sc = (TextView) findViewById(R.id.Score);
sc.setText(String.valueOf(score));
lf1.setVisibility(View.VISIBLE);
lf2.setVisibility(View.VISIBLE);
lf3.setVisibility(View.VISIBLE);
lf4.setVisibility(View.VISIBLE);
lf5.setVisibility(View.VISIBLE);
nolives = false;
}
doNext();
}
I'm adding this answer to be able to mark the question as answered. Please read the comment I left after the question.
Thanks to all that provide help on this site. I have gained much knowledge here.

Check all the view's background in an if statement in Android?

I have many buttons(81) from the left and right part of my layout. All in all, I have 162 buttons. I put those buttons to Button[] and I handle it properly.
private int[] right_lung = { R.id.btn_right_106, R.id.btn_right_113,
R.id.btn_right_114, R.id.btn_right_115, R.id.btn_right_116, R.id.btn_right_121,
R.id.btn_right_122, R.id.btn_right_123, R.id.btn_right_124, R.id.btn_right_125,
R.id.btn_right_129, R.id.btn_right_130, R.id.btn_right_131, R.id.btn_right_132,
R.id.btn_right_133, R.id.btn_right_134, R.id.btn_right_137, R.id.btn_right_138,
R.id.btn_right_139, R.id.btn_right_140, R.id.btn_right_141, R.id.btn_right_142,
R.id.btn_right_143, R.id.btn_right_145, R.id.btn_right_146, R.id.btn_right_147,
R.id.btn_right_148, R.id.btn_right_149, R.id.btn_right_150, R.id.btn_right_151,
R.id.btn_right_152, R.id.btn_right_153, R.id.btn_right_154, R.id.btn_right_155,
R.id.btn_right_156, R.id.btn_right_157, R.id.btn_right_158, R.id.btn_right_159,
R.id.btn_right_160, R.id.btn_right_161, R.id.btn_right_162, R.id.btn_right_163,
R.id.btn_right_164, R.id.btn_right_165, R.id.btn_right_166, R.id.btn_right_167,
R.id.btn_right_168, R.id.btn_right_169, R.id.btn_right_170, R.id.btn_right_171,
R.id.btn_right_172, R.id.btn_right_173, R.id.btn_right_174, R.id.btn_right_175,
R.id.btn_right_176, R.id.btn_right_177, R.id.btn_right_178, R.id.btn_right_179,
R.id.btn_right_180, R.id.btn_right_181, R.id.btn_right_182, R.id.btn_right_183,
R.id.btn_right_184, R.id.btn_right_185, R.id.btn_right_186, R.id.btn_right_187,
R.id.btn_right_188, R.id.btn_right_189, R.id.btn_right_190, R.id.btn_right_191,
R.id.btn_right_192, R.id.btn_right_194, R.id.btn_right_195, R.id.btn_right_196,
R.id.btn_right_197, R.id.btn_right_198, R.id.btn_right_199, R.id.btn_right_200,
R.id.btn_right_205, R.id.btn_right_206, R.id.btn_right_207 };
private Button[] btn_right = new Button[right_lung.length];
private int[] left_lung = { R.id.btn_left_7, R.id.btn_left_13, R.id.btn_left_14,
R.id.btn_left_15, R.id.btn_left_16, R.id.btn_left_20, R.id.btn_left_21,
R.id.btn_left_22, R.id.btn_left_23, R.id.btn_left_24, R.id.btn_left_27,
R.id.btn_left_28, R.id.btn_left_29, R.id.btn_left_30, R.id.btn_left_31,
R.id.btn_left_32, R.id.btn_left_34, R.id.btn_left_35, R.id.btn_left_36,
R.id.btn_left_37, R.id.btn_left_38, R.id.btn_left_39, R.id.btn_left_40,
R.id.btn_left_41, R.id.btn_left_42, R.id.btn_left_43, R.id.btn_left_44,
R.id.btn_left_45, R.id.btn_left_46, R.id.btn_left_47, R.id.btn_left_48,
R.id.btn_left_49, R.id.btn_left_50, R.id.btn_left_51, R.id.btn_left_52,
R.id.btn_left_53, R.id.btn_left_54, R.id.btn_left_55, R.id.btn_left_56,
R.id.btn_left_57, R.id.btn_left_58, R.id.btn_left_59, R.id.btn_left_60,
R.id.btn_left_61, R.id.btn_left_62, R.id.btn_left_63, R.id.btn_left_64,
R.id.btn_left_65, R.id.btn_left_66, R.id.btn_left_67, R.id.btn_left_68,
R.id.btn_left_69, R.id.btn_left_70, R.id.btn_left_71, R.id.btn_left_72,
R.id.btn_left_73, R.id.btn_left_74, R.id.btn_left_75, R.id.btn_left_76,
R.id.btn_left_77, R.id.btn_left_78, R.id.btn_left_79, R.id.btn_left_80,
R.id.btn_left_81, R.id.btn_left_82, R.id.btn_left_83, R.id.btn_left_84,
R.id.btn_left_85, R.id.btn_left_86, R.id.btn_left_87, R.id.btn_left_88,
R.id.btn_left_89, R.id.btn_left_90, R.id.btn_left_91, R.id.btn_left_92,
R.id.btn_left_93, R.id.btn_left_94, R.id.btn_left_95, R.id.btn_left_98,
R.id.btn_left_99, R.id.btn_left_100, };
private Button[] btn_left = new Button[left_lung.length];
Whenever I click on the button, just like in mine sweeper game, many buttons are randomly opened. And when it is opened, I am changing its background into R.drawable.affected. The goal of the game is to open all the buttons in left and right. My question is this, how can I check if all of the buttons are set in R.drawable.affected? Because after that, I will execute a method that will congratulate the user. Thanks in advance.
EDIT:
for (int i = 0; i < btn_right.length; i++) {
final int a = i;
int counter_total_affected = 0;
btn_right[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 1) {
right_lung.add(a);
btn_right[a].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
} else if (counter == 2) {
for (int i = 0; i < 2; i++) {
int n = i;
right_lung.add(a + n);
}
try {
for (int i = 0; i < 2; i++) {
int n = i;
btn_right[a + n].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (counter == 3) {
right_lung.add(a);
btn_right[a].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
} else if (counter == 4) {
for (int i = 0; i < 25; i++) {
int n = i;
right_lung.add(a + n);
}
try {
for (int i = 0; i < 25; i++) {
int n = i;
btn_right[a + n].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (counter == ...) {
//statements...
} else if (counter_total_affected == (btn_left.length + btn_right.length)) {
//CONGRATULATORY METHOD
}
counter++;
}
}
Increment the counter each time you change background of button and compare its value with length of button array. if both are same that means, all button backgrounds are set.
Try this :
for (int i = 0; i < btn_right.length; i++) {
final int b = i;
btn_right[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if btn_right[i].getDrawable().getConstantState().equals
(getResources().getDrawable(R.drawable.affected).getConstantState()))
{
if(counter == btn_right.length){
//Congratulate user...
}
}else{
btn_right[b].setBackgroundResource(R.drawable.affected);
counter++;
}
}
}
U can set Tag:
for (int i = 0; i < btn_right.length; i++) {
final int b = i;
btn_right[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 1) {
right_affected.add(b);
btn_right[b].setBackgroundResource(R.drawable.affected);
btn_right[b].setTag('1');
} else {
//some stuff here...
}
}
}
after congratulate the user set its Tag to 0.
EDIT:
ArrayList<Integer> arrayofId =new ArrayList<Integer>();
#Override
public void onClick(View v) {
if (counter == 1) {
right_affected.add(b);
btn_right[b].setBackgroundResource(R.drawable.affected);
arrayofId.add(b);//need to convert int to Integer.
} else {
//some stuff here...
}
}
To congratulate:
for(int i=0;i<arrayofId.size();i++)
{
// you can get here id of effected buttons
}

Categories

Resources