I am developing a game program where I want to store images in the buttons. I want to randomize the images shown in the buttons when the users play the game again, but I don't know how to randomize the images.
public class MainActivity extends Activity {
public static final String COME_FROM = "come_from";
private int[] id_mc = new int[16];
private Integer[][] img_mc = new Integer [16][2];
private Button[] myMcs = new Button[16];
private int mc_counter = 0;
private int firstid = 0;
private int secondid = 0;
private Boolean mc_isfirst = false;
private int correctcounter = 0;
private TextView tFeedback;
private MediaPlayer mp;
private Boolean b_snd_inc, b_snd_cor;
Random r = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
initGame();
}
private void initGame() {
setContentView(R.layout.activity_main);
SharedPreferences settings = getSharedPreferences("memoryPrefs", 0);
b_snd_cor =settings.getBoolean("play_sound_when_correct", true);
b_snd_inc =settings.getBoolean("play_sound_when_incorrect", true);
mc_counter = 0;
firstid = 0;
secondid = 0;
mc_isfirst = false;
correctcounter = 0;
tFeedback = (TextView) findViewById(R.id.mc_feedback);
// setup button listeners
Button startButton = (Button) findViewById(R.id.game_menu);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startMenu();
}
});
Button settingsButton = (Button) findViewById(R.id.game_settings);
settingsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startPrefs();
}
});
// fill arrays with resources
id_mc[0] = R.id.mc0;
id_mc[1] = R.id.mc1;
id_mc[2] = R.id.mc2;
id_mc[3] = R.id.mc3;
id_mc[4] = R.id.mc4;
id_mc[5] = R.id.mc5;
id_mc[6] = R.id.mc6;
id_mc[7] = R.id.mc7;
id_mc[8] = R.id.mc8;
id_mc[9] = R.id.mc9;
id_mc[10] = R.id.mc10;
id_mc[11] = R.id.mc11;
id_mc[12] = R.id.mc12;
id_mc[13] = R.id.mc13;
id_mc[14] = R.id.mc14;
id_mc[15] = R.id.mc15;
img_mc[0][0] = R.drawable.back1;
img_mc[0][1] = R.drawable.ic_img1;
img_mc[1][0] = R.drawable.back2;
img_mc[1][1] = R.drawable.ic_img2;
img_mc[2][0] = R.drawable.back3;
img_mc[2][1] = R.drawable.ic_img3;
img_mc[3][0] = R.drawable.back4;
img_mc[3][1] = R.drawable.ic_img4;
img_mc[4][0] = R.drawable.back5;
img_mc[4][1] = R.drawable.ic_img5;
img_mc[5][0] = R.drawable.back6;
img_mc[5][1] = R.drawable.ic_img6;
img_mc[6][0] = R.drawable.back7;
img_mc[6][1] = R.drawable.ic_img7;
img_mc[7][0] = R.drawable.back8;
img_mc[7][1] = R.drawable.ic_img8;
img_mc[8][0] = R.drawable.back1;
img_mc[8][1] = R.drawable.ic_img1;
img_mc[9][0] = R.drawable.back2;
img_mc[9][1] = R.drawable.ic_img2;
img_mc[10][0] = R.drawable.back3;
img_mc[10][1] = R.drawable.ic_img3;
img_mc[11][0] = R.drawable.back4;
img_mc[11][1] = R.drawable.ic_img4;
img_mc[12][0] = R.drawable.back5;
img_mc[12][1] = R.drawable.ic_img5;
img_mc[13][0] = R.drawable.back6;
img_mc[13][1] = R.drawable.ic_img6;
img_mc[14][0] = R.drawable.back7;
img_mc[14][1] = R.drawable.ic_img7;
img_mc[15][0] = R.drawable.back8;
img_mc[15][1] = R.drawable.ic_img8;
//Collections.shuffle(Arrays.asList(img_mc));
for (int i = 0; i < 16; i++) {
try{
myMcs[i] = (Button) findViewById(id_mc[i]);
myMcs[i].setBackgroundResource(img_mc[i][0]);
myMcs[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int i = 0;
for (int n = 0; n < 16; n++) {
if (id_mc[n] == view.getId())
i = n;
}
doClickAction(view, i);
}
});
}catch(Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}}
}
private void doClickAction(View v, int i)
{
v.setBackgroundResource(img_mc[i][1]);
mc_isfirst = !mc_isfirst;
// disable all buttons
for (Button b : myMcs) {
b.setEnabled(false);
}
if (mc_isfirst) {
// turning the first card
firstid = i;
// re enable all except this one
for (Button b : myMcs) {
if (b.getId() != firstid) {
b.setEnabled(true);
}
}
} else {
// turning the second card
secondid = i;
doPlayMove();
}
}
private void doPlayMove() {
mc_counter++;
if (img_mc[firstid][1] - img_mc[secondid][1] == 0) {
//correct
if (b_snd_cor) playSound(R.raw.correct);
waiting(200);
myMcs[firstid].setVisibility(View.INVISIBLE);
myMcs[secondid].setVisibility(View.INVISIBLE);
correctcounter++;
} else {
//incorrect
if (b_snd_inc) playSound(R.raw.incorrect);
waiting(400);
}
// reenable and turn cards back
for (Button b : myMcs) {
if (b.getVisibility() != View.INVISIBLE) {
b.setEnabled(true);
b.setBackgroundResource(R.drawable.memory_back);
for (int i = 0; i < 16; i++) {
myMcs[i].setBackgroundResource(img_mc[i][0]);
}
}
}
tFeedback.setText("" + correctcounter + " / " + mc_counter);
if (correctcounter > 7) {
Intent iSc = new Intent(getApplicationContext(), Scoreboard.class);
iSc.putExtra("com.gertrietveld.memorygame.SCORE", mc_counter);
startActivity(iSc);
finish();
}
}
public void playSound(int sound) {
mp = MediaPlayer.create(this, sound);
mp.setVolume((float).5,(float).5);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
public static void waiting(int n) {
long t0, t1;
t0 = System.currentTimeMillis();
do {
t1 = System.currentTimeMillis();
} while ((t1 - t0) < (n));
}
private void startMenu() {
Intent launchMenu = new Intent(this, MenuScreen.class);
launchMenu.putExtra(COME_FROM,"PlayGame");
startActivity(launchMenu);
}
private void startPrefs() {
Intent launchPrefs = new Intent(this, Setting.class);
startActivity(launchPrefs);
}
////////////////////////////////
#Override
protected void onRestart() {
super.onRestart();
//String sender = getIntent().getExtras().getString("SENDER");
//initGame();
Toast.makeText(this, "onRestart-sender is " , Toast.LENGTH_SHORT).show();
}
#Override
protected void onResume() {
super.onResume();
SharedPreferences settings = getSharedPreferences("memoryPrefs", 0);
b_snd_cor =settings.getBoolean("play_sound_when_correct", true);
b_snd_inc =settings.getBoolean("play_sound_when_incorrect", true);
Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
}
////////////////////////////////
}
You had the Collections.shuffle() part right, you just need to get the randomized list back into your array:
private Integer[][] img_mc = new Integer [16][2];
...
List<Integer[]> img_mc_list = new ArrayList<Integer[]>();
for (Integer[] img : img_mc) {
img_mc_list.add(img);
}
Collections.shuffle(img_mc_list);
img_mc_list.toArray(img_mc);
Or use this:
private void randomize(Integer[][] array) {
int index;
Integer[] temp;
Random random = new Random();
for (int i = array.length - 1; i > 0; i--) {
index = random.nextInt(i + 1);
temp = array[index];
array[index] = array[i];
array[i] = temp;
}
}
Related
I have a game activity about different alphabet are randomly available user would select some of them that are making a correct word.
i made the string array of word which is answer
but i want to knew how to display this answer word alphabets and adding some randomly other alphabet as a confusion?
like taking the answer for example [World] and divid it's alphabet like that [W , L, D , O] AND make them randomly displayed and the player choose from them ?
TextView guessItTimer;
CountDownTimer timer;
Random r;
String currentWord;
private int presCounter = 0;
private int maxPresCounter = 4;
private String[] keys = {"R", "I", "B", "D", "X"};
String dictionary[] = {
"remember",
"hungry",
"crying",
"sour",
"sleep",
"awesome",
"Seven",
"color",
"began",
"appear",
"weight",
"language"
};
TextView textScreen, textQuestion, textTitle;
Animation smallbigforth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guess_it);
guessItTimer = findViewById(R.id.guessItTimer);
smallbigforth = AnimationUtils.loadAnimation(this, R.anim.smallbigforth);
keys = shuffleArray(keys);
for (String key : keys) {
addView(( findViewById(R.id.layoutParent)), key, findViewById(R.id.et_guess));
}
maxPresCounter = 4;
resetTimer();
}
//CountdownTimer
void resetTimer() {
timer = new CountDownTimer(30150, 1000) {
#Override
public void onTick(long l) {
guessItTimer.setText(String.valueOf(l / 1000));
}
#Override
public void onFinish() {
Toast.makeText(GuessItActivity.this, "Time is over", Toast.LENGTH_SHORT).show();
startActivity(new Intent(GuessItActivity.this, BossFinalActivity.class));
finish();
}
}.start();
}
private String[] shuffleArray(String[] ar) {
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
String a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
return ar;
}
private void addView(LinearLayout viewParent, final String text, final EditText editText) {
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
linearLayoutParams.rightMargin = 30;
final TextView textView = new TextView(this);
textView.setLayoutParams(linearLayoutParams);
textView.setBackground(this.getResources().getDrawable(R.drawable.bgpink));
textView.setTextColor(this.getResources().getColor(R.color.colorPurple));
textView.setGravity(Gravity.CENTER);
textView.setText(text);
textView.setClickable(true);
textView.setFocusable(true);
textView.setTextSize(32);
textQuestion = findViewById(R.id.textQuestionBoss);
textScreen = findViewById(R.id.gametitle);
textTitle = findViewById(R.id.Ammo);
textView.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
#Override
public void onClick(View v) {
if(presCounter < maxPresCounter) {
if (presCounter == 0)
editText.setText("");
editText.setText(editText.getText().toString() + text);
textView.startAnimation(smallbigforth);
textView.animate().alpha(0).setDuration(300);
presCounter++;
if (presCounter == maxPresCounter)
doValidate();
}
}
});
viewParent.addView(textView);
}
private void doValidate() {
presCounter = 0;
EditText editText = findViewById(R.id.et_guess);
LinearLayout linearLayout = findViewById(R.id.layoutParent);
currentWord = dictionary[r.nextInt(dictionary.length)];
if(editText.getText().toString().equals(currentWord)) {
//Toast.makeText(GuessItActivity.this, "Correct", Toast.LENGTH_SHORT).show();
Intent a = new Intent(GuessItActivity.this,BossFinalActivity.class);
startActivity(a);
editText.setText("");
} else {
Toast.makeText(GuessItActivity.this, "Wrong", Toast.LENGTH_SHORT).show();
editText.setText("");
}
keys = shuffleArray(keys);
linearLayout.removeAllViews();
for (String key : keys) {
addView(linearLayout, key, editText);
}
}
public void onBackPressed() {
timer.cancel();
this.finish();
super.onBackPressed();
}
You can achieve it like this
String a2z = "abcdefghijklmnopqrstuvwxyz";
String answer = "World";
// lets assume you need 16 char to select from
ArrayList<Character> chars = new ArrayList<Character>();
for (int i = 0; i < answer.length(); i++) {
chars.add(answer.charAt(i));
}
int dif = 16 - chars.size();
Random rand = new Random();
for (int i = 0; i < dif; i++) {
int ranIndex = rand.nextInt(a2z.length());
chars.add(a2z.charAt(ranIndex));
}
Collections.sort(chars);
System.out.println("nameArray2" + chars.toString());
Hi I want to open a activity when a if statement comes true. like "if gameStatus are equal to 12, then open scoreActivity". The Code:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import java.util.Random;
import android.os.Build;
import android.os.Handler;
public class Game6x4Activity extends AppCompatActivity implements View.OnClickListener {
private int numberOfElements;
private int[] buttonGraphicLocations;
private MemoryButton selectedButton1;
private MemoryButton selectedButton2;
private boolean isBusy = false;
public int gameStatus;
public int gameScore;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_mode);
gameScore = 0;
gameStatus = 0;
GridLayout gridLayout = (GridLayout)findViewById(R.id.grid_layout_6x4);
int numColumns = gridLayout.getColumnCount();
int numRow = gridLayout.getRowCount();
numberOfElements = numColumns * numRow;
MemoryButton[] buttons = new MemoryButton[numberOfElements];
int[] buttonGraphics = new int[numberOfElements / 2];
buttonGraphics[0] = R.drawable.card1;
buttonGraphics[1] = R.drawable.card2;
buttonGraphics[2] = R.drawable.card3;
buttonGraphics[3] = R.drawable.card4;
buttonGraphics[4] = R.drawable.card5;
buttonGraphics[5] = R.drawable.card6;
buttonGraphics[6] = R.drawable.card7;
buttonGraphics[7] = R.drawable.card8;
buttonGraphics[8] = R.drawable.card9;
buttonGraphics[9] = R.drawable.card10;
buttonGraphics[10] = R.drawable.card11;
buttonGraphics[11] = R.drawable.card12;
buttonGraphicLocations = new int[numberOfElements];
shuffleButtonGraphics();
for(int r=0; r < numRow; r++)
{
for(int c=0; c <numColumns; c++)
{
MemoryButton tempButton = new MemoryButton(this, r, c, buttonGraphics[buttonGraphicLocations[r * numColumns + c]]);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
tempButton.setId(View.generateViewId());
}
tempButton.setOnClickListener(this);
buttons[r * numColumns + c] = tempButton;
gridLayout.addView(tempButton);
}
}
}
protected void shuffleButtonGraphics(){
Random rand = new Random();
for (int i=0; i < numberOfElements; i++)
{
buttonGraphicLocations[i] = i % (numberOfElements / 2);
}
for (int i=0; i < numberOfElements; i++)
{
int temp = buttonGraphicLocations[i];
int swapIndex = rand.nextInt(16);
buttonGraphicLocations[i] = buttonGraphicLocations[swapIndex];
buttonGraphicLocations[swapIndex] = temp;
}
}
private int buttonGraphicLocations(int i) {
return 0;
}
#Override
public void onClick(View view) {
if(isBusy) {
return;
}
MemoryButton button = (MemoryButton) view;
if(button.isMatched) {
return;
}
if(selectedButton1 == null)
{
selectedButton1 = button;
selectedButton1.flip();
return;
}
if(selectedButton1.getId()== button.getId())
{
return;
}
if (selectedButton1.getFrontDrawableId()== button.getFrontDrawableId())
{
button.flip();
button.setMatched(true);
if (selectedButton1 != null) {
selectedButton1.setEnabled(false);
System.out.println("not null");
}
else{
System.out.println("null");
}
if (selectedButton2 != null) {
selectedButton2.setEnabled(false);
System.out.println("not null");
}
else{
System.out.println("null");
}
gameStatus = gameStatus + 1;
gameScore = gameScore + 10;
if (gameStatus == 12){
Intent it = new Intent(Game6x4Activity.this, ActivityScore.class);
startActivity(it);
}
selectedButton1 = null;
return;
}
else
{
selectedButton2 = button;
selectedButton2.flip();
isBusy = true;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run(){
selectedButton2.flip();
selectedButton1.flip();
selectedButton1 = null;
selectedButton2 = null;
isBusy = false;
}
},500);
return;
}
}
}
The activity that i want to open will show to the player his score. the activity is equal to all game modes, there will be some test to the app understant what path should go on. test like this one:
"
if (gameStatus == 12) {
gameScore = gameScore*55;
TextView scoreText = (TextView) findViewById(R.id.textView8);
scoreText.setText(gameScore);
}
else if (gameStatus == 15){
"
There are 4 game modes: This is the 6x4 game, where we can find 24 cards (12 images).
else if (gameStatus == 15){
Intent intent = new Intent(Game6x4Activity.this, NextActivity.class);
startActivity(intent);
}
I think, you are asking for this. You can pass value to another activity with
intent.putExtra("key",desired value);
I'm streaming a video in my app, and it works fine.
The problem is that when I try to make it full screen (in a new activity or otherwise), the screen is blank.
I have tried doing it without starting a new activity, as suggested here:
In my main activity:
butFullScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isFS)
setContentView(R.layout.activity_full_screen_video);
else
setContentView(R.layout.activity_starting_point);
}
});
I've also tried opening it in a new activity (which I prefer):
butFullScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Start fs intent
Intent myIntent = new Intent(StartingPoint.this, FullScreenVideo.class);
StartingPoint.this.startActivity(myIntent);
}
});
Where FullScreenVideo:
public class FullScreenVideo extends StartingPoint{
//private VideoView vv;
public FullScreenVideo(){
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_full_screen_video);
vv.start();
}
}
And activity_full_screen_video:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:background="#ff000000">
<VideoView
android:id="#+id/vv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
And in my manifest:
<activity
android:name=".FullScreenVideo"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize" />
It doesn't show any error or crash, just a blank screen. Also, I'm not using the media controller, so answers without that would be appreciated :)
Edit::
StartingPoint:
public class PlayVid extends ActionBarActivity {
int play = -1;
int k;
int m = 0;
int where;
int pausePressed = 0;
int displaySub = 0;
String curSub = " ";
ArrayList<Stime> timeArray = new ArrayList<Stime>();
Button but;
Button butStop;
Button butSub;
Button butFS;
TextView subs, log;
VideoView vv;
ProgressBar pBar;
int isFS, space;
String srt = "00:00:01,478 --> 00:00:04,020\n" +
"VimeoSrtPlayer Example\n" +
"\n" +
"00:00:05,045 --> 00:00:09,545\n" +
"Support for <i>italic</i> font\n" +
"\n" +
"00:00:09,378 --> 00:00:13,745\n" +
"Support for <b>bold</b> font\n" +
"\n" +
"00:00:14,812 --> 00:00:16,144\n" +
"Multi\n" +
"Line\n" +
"Support ;)\n" +
"\n" +
"00:00:18,211 --> 00:00:21,211\n" +
"Fonts: DejaVu\n" +
"http://dejavu-fonts.org\n" +
"\n" +
"00:00:22,278 --> 00:00:25,678 \n" +
"END OF EXAMPLE FILE";
subParse sp = new subParse(this, srt);
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
pBar = (ProgressBar) findViewById(R.id.progressBar);
subs = (TextView) findViewById(R.id.subtitleBox);
log = (TextView) findViewById(R.id.logBox);
but = (Button) findViewById(R.id.but);
butStop = (Button) findViewById(R.id.butStop);
butSub = (Button) findViewById(R.id.butSub);
butFS = (Button) findViewById(R.id.butFS);
vv = (VideoView) findViewById(R.id.vv);
vv.setVideoPath("#string/video_link");
String[] lines = {" \n", " \n"};
sp.parseSub(srt);
subs.setText("--Click Play to start--\n--Click on SUB to view subtitles--");
final String[] sub2 = log.getText().toString().split(System.getProperty("line.separator"));
final subHelper sob = new subHelper(this, sub2);
final onPauseHelper oph = new onPauseHelper();
final fsHelper fsh = new fsHelper(this);
final playHelper ph = new playHelper();
timeArray.add(new Stime(vv.getDuration(), 0));
but.setOnClickListener(new View.OnClickListener() {
long startTime = 0, trop = 0;
int space = 0;
public void onClick(View v) {
subs.setText(curSub);
fsh.space = space;
if (fsh.isFS == 1) {
//Chill
} else if (play == 0 || play == -1) {
vv.start();
pBar.setVisibility(View.VISIBLE);
if (vv.isPlaying()) {
startTime = Calendar.getInstance().getTimeInMillis();
pBar.setVisibility(View.INVISIBLE);
//Play
if (play == -1) {
sob.setDefaults();
}
oph.putAttr(3, startTime);
but.setText("||");
but.setTextColor(Color.parseColor("#ffaaaaaa"));
if (k < m) {
play = 1;
final Handler handler = new Handler();
final Runnable run = new Runnable() {
#Override
public void run() {
if (k < m && play == 1 && fsh.isFS == 0) {
if (pausePressed == 0) {
oph.putAttr(0, Calendar.getInstance().getTimeInMillis());
} else {
oph.putAttr(0, startTime);
where--;
}
//oph.nextPressed=Calendar.getInstance().getTimeInMillis();
if (space == 0) {
space = 1;
fsh.space = space;
play = sob.subPlay(startTime, 0);
fsh.play = play;
trop = (timeArray.get(k).getTime(1) - timeArray.get(k).getTime(0));
handler.postDelayed(this, Math.abs(trop > 0 ? (trop - oph.done) : 0));
} else if (space == 1) {
space = 0;
fsh.space = space;
play = sob.subPlay(startTime, 1);
fsh.play = play;
trop = timeArray.get(k + 1).getTime(0) - timeArray.get(k).getTime(1);
handler.postDelayed(this, Math.abs(trop > 0 ? (trop - oph.done) : 0));
k++;
}
oph.putAttr(1, Calendar.getInstance().getTimeInMillis());
pausePressed = 0;
sob.pp = 0;
oph.reset();
oph.done = 0;
} else if (k >= m && fsh.isFS == 0) {
final Runnable run = new Runnable() {
#Override
public void run() {
but.setText("↻");
but.setTextColor(Color.parseColor("#ffcdcdcd"));
subs.setText("Play Again?");
handler.removeCallbacks(this);
play = -1;
fsh.play = play;
//ph.saveState(play,0,k,0,curSub,displaySub);
curSub = " ";
}
};
while (vv.isPlaying()) {
}
handler.postDelayed(run, 0);
}
}
};
if (k == 0)
handler.postDelayed(run, (timeArray.get(0).getTime(0)));
else if (k < m) handler.postDelayed(run, 0);
}
}
} else if (play == 1) {
//Pause
vv.pause();
oph.reset();
oph.putAttr(2, Calendar.getInstance().getTimeInMillis());
subs.setText(curSub);
pausePressed = 1;
oph.done = +oph.getProg();
sob.pp++;
but.setText("▶");
but.setTextColor(Color.parseColor("#ffcdcdcd"));
if (k < m) {
if (space == 0) {
space = 1;
fsh.space = space;
} else if (space == 1) {
space = 0;
fsh.space = space;
}
play = sob.subPause(oph.getAttr(2));
fsh.play = play;
}
}
}
});
butStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vv.stopPlayback();
sob.setDefaults();
play = 0;
fsh.play = play;
but.setText("▶");
subs.setText("--Click Play to start--");
curSub = " ";
}
});
butSub.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (displaySub == 0) {
displaySub = 1;
subs.setText(curSub);
butSub.setTextColor(Color.parseColor("#ffaaaaaa"));
} else if (displaySub == 1) {
displaySub = 0;
subs.setText(" ");
butSub.setTextColor(Color.parseColor("#ffcdcdcd"));
}
}
});
butFS.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Start fs intent
Intent myIntent = new Intent(PlayVid.this, TwoFullScreenVideo.class);
PlayVid.this.startActivity(myIntent);
}
});
}
}
FullScreenVideo:
public class FullScreenVideo extends StartingPoint {
fsHelper fsh;
onPauseHelper oph;
subHelper sob;
playHelper ph;
protected PlayVid context;
TextView subs;
Button but, butStop, butSub, butFS;
public FullScreenVideo(){}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_full_screen_video);
subs = (TextView) findViewById(R.id.subtitleBox);
but = (Button) findViewById(R.id.but);
butStop = (Button) findViewById(R.id.butStop);
butSub = (Button) findViewById(R.id.butSub);
butFS = (Button) findViewById(R.id.butFS);
vv = (VideoView) findViewById(R.id.vv);
butFS.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FullScreenVideo.this.finish();
}
});
}
}
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.
I am making an app to search a database and i have a part where i type in a search detail and the name of the possible results are displayed on buttons in a new activity. It works fine first time round but if i press back from that activity then try to search for something different then the last button results but the old results are still there with the new ones.
public class search_page extends Activity implements OnClickListener {
static int number;
static int[] numberArray = new int[8];
static int looped;
static int typeFound = 0;
TextView editText1;
Button search_button, search_button2 ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_page);
editText1 = (EditText) findViewById(R.id.editText1);
search_button = (Button) findViewById(R.id.search_button);
search_button2 = (Button) findViewById(R.id.search_button2);
search_button.setOnClickListener(this);
search_button2.setOnClickListener(this);
}
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.search_button:
sqlStuff search1 = new sqlStuff(search_page.this);
boolean found = false;
String Systname = editText1.getText().toString();
search1.open();
String[] IDSysNames = search1.getIDSysName();
search1.close();
for(int i = 0; i < IDSysNames.length; i++) {
if(Systname.equalsIgnoreCase(IDSysNames[i].toString())) {
found = true;
number = i;
}
}
if(found==true) {
Intent search = new Intent("com.MC.ChemPal.RESULT");
startActivity(search);
}
else {
Dialog d = new Dialog(this);
d.setTitle("result not found");
TextView tv = new TextView(this);
d.setContentView(tv);
d.show();
}
break;
case R.id.search_button2:
boolean found2 = false;
boolean found3 = false;
sqlStuff search2 = new sqlStuff(search_page.this);
search2.open();
String entry = editText1.getText().toString();
String[] IDSysNames2 = search2.getIDSysName();
String[] IDGroup = search2.getIDGroup();
String[] IDMP = search2.getIDMP();
String[] IDBP = search2.getIDBP();
String[] IDComname = search2.getIDComname();
String[] IDElement = search2.getIDElement();
String[] IDMolarmass = search2.getIDMOLARMASS();
search2.close();
for(int i = 0; i < IDSysNames2.length; i++) {
if(entry.equalsIgnoreCase(IDSysNames2[i].toString())) {
found2 = true;
found3 = true;
typeFound = 1;
numberArray[looped] = i;
}
if(entry.equalsIgnoreCase(IDGroup[i].toString())) {
found2 = true;
found3 = true;
typeFound = 2;
numberArray[looped] = i;
}
if(entry.equalsIgnoreCase(IDMP[i].toString())) {
found2 = true;
found3 = true;
typeFound = 3;
numberArray[looped] = i;
}
if(entry.equalsIgnoreCase(IDBP[i].toString())) {
found2 = true;
found3 = true;
typeFound = 4;
numberArray[looped] = i;
}
if(entry.equalsIgnoreCase(IDComname[i].toString())) {
found2 = true;
found3 = true;
typeFound = 5;
numberArray[looped] = i;
}
if(IDElement[i].toString().contains(entry)) {
found2 = true;
found3 = true;
typeFound = 6;
numberArray[looped] = i;
}
if(entry.equalsIgnoreCase(IDMolarmass[i].toString())) {
found2 = true;
found3 = true;
typeFound = 7;
numberArray[looped] = i;
}
if(found2 == true) {
looped++;
}
found2 = false;
}
if (found3==true) {
Intent searching2 = new Intent("com.MC.ChemPal.SEARCHLIST");
startActivity(searching2);
}
else {
Dialog d = new Dialog(this);
d.setTitle("result not found");
TextView tv = new TextView(this);
d.setContentView(tv);
d.show();
}
break;
}
}
public static int returnNum() {
return number;
}
public static int[] returnNumArray() {
return numberArray;
}
public static int returnlooped() {
return looped;
}
}
That activity then links to this one.
public class searchlist extends Activity implements OnClickListener {
static int buttonPress = 0;
int loops = 0;
public void onCreate(Bundle savedinstance){
super.onCreate(savedinstance);
setContentView(R.layout.searchlist);
Button[] mybuttons = new Button[10];
mybuttons[0] = (Button) findViewById(R.id.search1);
mybuttons[1] = (Button) findViewById(R.id.search2);
mybuttons[2] = (Button) findViewById(R.id.search3);
mybuttons[3] = (Button) findViewById(R.id.search4);
mybuttons[4] = (Button) findViewById(R.id.search5);
mybuttons[5] = (Button) findViewById(R.id.search6);
mybuttons[6] = (Button) findViewById(R.id.search7);
mybuttons[7] = (Button) findViewById(R.id.search8);
mybuttons[8] = (Button) findViewById(R.id.search9);
mybuttons[9] = (Button) findViewById(R.id.search10);
int i = 0;
if(!mybuttons[0].getText().equals("-"))
{
mybuttons[0].setText("-");
mybuttons[0].setVisibility(View.INVISIBLE);
}
if(!mybuttons[1].getText().equals("-"))
{
mybuttons[1].setText("-");
mybuttons[1].setVisibility(View.INVISIBLE);
}
if(!mybuttons[2].getText().equals("-"))
{
mybuttons[2].setText("-");
mybuttons[2].setVisibility(View.INVISIBLE);
}
if(!mybuttons[3].getText().equals("-"))
{
mybuttons[3].setText("-");
mybuttons[3].setVisibility(View.INVISIBLE);
}
if(!mybuttons[4].getText().equals("-"))
{
mybuttons[4].setText("-");
mybuttons[4].setVisibility(View.INVISIBLE);
}
if(!mybuttons[5].getText().equals("-"))
{
mybuttons[5].setText("-");
mybuttons[5].setVisibility(View.INVISIBLE);
}
if(!mybuttons[6].getText().equals("-"))
{
mybuttons[6].setText("-");
mybuttons[6].setVisibility(View.INVISIBLE);
}
if(!mybuttons[7].getText().equals("-"))
{
mybuttons[7].setText("-");
mybuttons[7].setVisibility(View.INVISIBLE);
}
if(!mybuttons[8].getText().equals("-"))
{
mybuttons[8].setText("-");
mybuttons[8].setVisibility(View.INVISIBLE);
}
if(!mybuttons[9].getText().equals("-"))
{
mybuttons[9].setText("-");
mybuttons[9].setVisibility(View.INVISIBLE);
}
sqlStuff searching = new sqlStuff(searchlist.this);
searching.open();
String[] IDSysNames = searching.getIDSysName();
loops = search_page.returnlooped();
int[] teacup = search_page.returnNumArray();
searching.close();
for(i=0; i < loops; i++ )
{
if(IDSysNames[teacup[i]] != null)
{
mybuttons[i].setText(IDSysNames[teacup[i]]);
}
}
if(!mybuttons[0].getText().equals("-"))
{
mybuttons[0].setOnClickListener(this);
mybuttons[0].setVisibility(View.VISIBLE);
}
if(!mybuttons[1].getText().equals("-"))
{
mybuttons[1].setOnClickListener(this);
mybuttons[1].setVisibility(View.VISIBLE);
}
if(!mybuttons[2].getText().equals("-"))
{
mybuttons[2].setOnClickListener(this);
mybuttons[2].setVisibility(View.VISIBLE);
}
if(!mybuttons[3].getText().equals("-"))
{
mybuttons[3].setOnClickListener(this);
mybuttons[0].setVisibility(View.VISIBLE);
}
if(!mybuttons[4].getText().equals("-"))
{
mybuttons[4].setOnClickListener(this);
mybuttons[4].setVisibility(View.VISIBLE);
}
if(!mybuttons[5].getText().equals("-"))
{
mybuttons[5].setOnClickListener(this);
mybuttons[5].setVisibility(View.VISIBLE);
}
if(!mybuttons[6].getText().equals("-"))
{
mybuttons[6].setOnClickListener(this);
mybuttons[6].setVisibility(View.VISIBLE);
}
if(!mybuttons[7].getText().equals("-"))
{
mybuttons[7].setOnClickListener(this);
mybuttons[7].setVisibility(View.VISIBLE);
}
if(!mybuttons[8].getText().equals("-"))
{
mybuttons[8].setOnClickListener(this);
mybuttons[8].setVisibility(View.VISIBLE);
}
if(!mybuttons[9].getText().equals("-"))
{
mybuttons[9].setOnClickListener(this);
mybuttons[9].setVisibility(View.VISIBLE);
}
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case
R.id.search1:
buttonPress = 0;
Intent search = new Intent("com.MC.ChemPal.RESULT2");
startActivity(search);
break;
case
R.id.search2:
buttonPress = 1;
Intent search2 = new Intent("com.MC.ChemPal.RESULT2");
startActivity(search2);
break;
case
R.id.search3:
buttonPress = 2;
Intent search3 = new Intent("com.MC.ChemPal.RESULT2");
startActivity(search3);
break;
case
R.id.search4:
buttonPress=3;
Intent search4 = new Intent("com.MC.ChemPal.RESULT2");
startActivity(search4);
break;
case
R.id.search5:
buttonPress=4;
Intent search5 = new Intent("com.MC.ChemPal.RESULT2");
startActivity(search5);
break;
case
R.id.search6:
buttonPress=5;
Intent search6 = new Intent("com.MC.ChemPal.RESULT2");
startActivity(search6);
break;
case
R.id.search7:
buttonPress=6;
Intent search7 = new Intent("com.MC.ChemPal.RESULT2");
startActivity(search7);
break;
case
R.id.search8:
buttonPress=7;
Intent search8 = new Intent("com.MC.ChemPal.RESULT2");
startActivity(search8);
break;
case
R.id.search9:
buttonPress=8;
Intent search9 = new Intent("com.MC.ChemPal.RESULT2");
startActivity(search9);
break;
case
R.id.search10:
buttonPress=9;
Intent search10 = new Intent("com.MC.ChemPal.RESULT2");
startActivity(search10);
break;
}
}
public static int getButtonPress() {
return buttonPress;
}
public void onResume(){
super.onResume();
setContentView(R.layout.searchlist);
Button[] mybuttons = new Button[10];
onResume();
mybuttons[0] = (Button) findViewById(R.id.search1);
mybuttons[1] = (Button) findViewById(R.id.search2);
mybuttons[2] = (Button) findViewById(R.id.search3);
mybuttons[3] = (Button) findViewById(R.id.search4);
mybuttons[4] = (Button) findViewById(R.id.search5);
mybuttons[5] = (Button) findViewById(R.id.search6);
mybuttons[6] = (Button) findViewById(R.id.search7);
mybuttons[7] = (Button) findViewById(R.id.search8);
mybuttons[8] = (Button) findViewById(R.id.search9);
mybuttons[9] = (Button) findViewById(R.id.search10);
int i = 0;
if(!mybuttons[0].getText().equals("-"))
{
mybuttons[0].setText("-");
mybuttons[0].setVisibility(View.INVISIBLE);
}
if(!mybuttons[1].getText().equals("-"))
{
mybuttons[1].setText("-");
mybuttons[1].setVisibility(View.INVISIBLE);
}
if(!mybuttons[2].getText().equals("-"))
{
mybuttons[2].setText("-");
mybuttons[2].setVisibility(View.INVISIBLE);
}
if(!mybuttons[3].getText().equals("-"))
{
mybuttons[3].setText("-");
mybuttons[3].setVisibility(View.INVISIBLE);
}
if(!mybuttons[4].getText().equals("-"))
{
mybuttons[4].setText("-");
mybuttons[4].setVisibility(View.INVISIBLE);
}
if(!mybuttons[5].getText().equals("-"))
{
mybuttons[5].setText("-");
mybuttons[5].setVisibility(View.INVISIBLE);
}
if(!mybuttons[6].getText().equals("-"))
{
mybuttons[6].setText("-");
mybuttons[6].setVisibility(View.INVISIBLE);
}
if(!mybuttons[7].getText().equals("-"))
{
mybuttons[7].setText("-");
mybuttons[7].setVisibility(View.INVISIBLE);
}
if(!mybuttons[8].getText().equals("-"))
{
mybuttons[8].setText("-");
mybuttons[8].setVisibility(View.INVISIBLE);
}
if(!mybuttons[9].getText().equals("-"))
{
mybuttons[9].setText("-");
mybuttons[9].setVisibility(View.INVISIBLE);
}
sqlStuff searching = new sqlStuff(searchlist.this);
searching.open();
String[] IDSysNames = searching.getIDSysName();
loops = search_page.returnlooped();
int[] teacup = search_page.returnNumArray();
searching.close();
for(i=0; i < loops; i++ )
{
if(IDSysNames[teacup[i]] != null)
{
mybuttons[i].setText(IDSysNames[teacup[i]]);
}
}
if(!mybuttons[0].getText().equals("-"))
{
mybuttons[0].setOnClickListener(this);
mybuttons[0].setVisibility(View.VISIBLE);
}
if(!mybuttons[1].getText().equals("-"))
{
mybuttons[1].setOnClickListener(this);
mybuttons[1].setVisibility(View.VISIBLE);
}
if(!mybuttons[2].getText().equals("-"))
{
mybuttons[2].setOnClickListener(this);
mybuttons[2].setVisibility(View.VISIBLE);
}
if(!mybuttons[3].getText().equals("-"))
{
mybuttons[3].setOnClickListener(this);
mybuttons[0].setVisibility(View.VISIBLE);
}
if(!mybuttons[4].getText().equals("-"))
{
mybuttons[4].setOnClickListener(this);
mybuttons[4].setVisibility(View.VISIBLE);
}
if(!mybuttons[5].getText().equals("-"))
{
mybuttons[5].setOnClickListener(this);
mybuttons[5].setVisibility(View.VISIBLE);
}
if(!mybuttons[6].getText().equals("-"))
{
mybuttons[6].setOnClickListener(this);
mybuttons[6].setVisibility(View.VISIBLE);
}
if(!mybuttons[7].getText().equals("-"))
{
mybuttons[7].setOnClickListener(this);
mybuttons[7].setVisibility(View.VISIBLE);
}
if(!mybuttons[8].getText().equals("-"))
{
mybuttons[8].setOnClickListener(this);
mybuttons[8].setVisibility(View.VISIBLE);
}
if(!mybuttons[9].getText().equals("-"))
{
mybuttons[9].setOnClickListener(this);
mybuttons[9].setVisibility(View.VISIBLE);
}
}
}
You changed the state of the buttons within the view. Moving to another activity and back won't reset the view. It maintains the states within your app, which would make not knowing the state far more of a problem.
Where you're setting the states programatically, I would suggest an override on onResume to set the states of your buttons to where you want them. This will be called when your activity is initially started, if it is restarted, and each time your activity is brought to the foreground.
Refer to: Android life cycle activities
Additional information:
You posted 2 activities (search_page and searchlist) where search_page clearly calls searchlist. You mentioned that your problem is hitting the back button. The problem isn't actually hitting the back -- that's obviously working, it is what the activity does when it resumes.
From your last comment it looks to me like you're making the buttons visible if they have text in them, so the problem isn't making them visible -- that's working, the problem is that the wrong buttons have text.
When should you clear them? When you return to search_page? If so, make your onResume in your search_page clear the text in all buttons.