How to pass value of textview form one activity going to the other activity?
I have a scoring on my game that is shown on a textview and after it increment it will intent to the next activity. but the value of score from the first activity doesn't show on the textview of the second activity.
this is my code for my first activity
final TextView score2 = (TextView) findViewById(R.id.tvscore2);
Button page1 = (Button) findViewById(R.id.btnDog);
page1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText etDog1 = (EditText) findViewById(R.id.etDog);
String Dog = etDog1.getText().toString();
if (Dog.equalsIgnoreCase("dog")) {
global.score += 10;
score2.setText(String.valueOf(global.score));
Toast.makeText(getApplicationContext(), "Correct",
Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(view.getContext(),
sound1_3pig.class);
startActivityForResult(myIntent, 0);
} else if (global.score <= 0) {
global.score += 0;
score2.setText(String.valueOf(global.score));
Toast.makeText(getApplicationContext(), "Wrong",
Toast.LENGTH_SHORT).show();
} else {
global.score -= 5;
score2.setText(String.valueOf(global.score));
Toast.makeText(getApplicationContext(), "Wrong",
Toast.LENGTH_SHORT).show();
}
}
});
}
I want to display the result of the score activity 1 to the textview of the second activity
this is my second activity
final TextView score1 = (TextView) findViewById(R.id.tvscore1);
Button page1 = (Button) findViewById(R.id.btnCat);
page1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText etCat = (EditText) findViewById(R.id.etCat);
String Cat = etCat.getText().toString();
if (Cat.equalsIgnoreCase("cat")) {
global.score += 10;
score2.setText(String.valueOf(global.score));
Toast.makeText(getApplicationContext(), "Correct",
Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(view.getContext(),
sound1_3pig.class);
startActivityForResult(myIntent, 0);
finish();
} else if (global.score <= 0) {
global.score += 0;
score2.setText(String.valueOf(global.score));
Toast.makeText(getApplicationContext(), "Wrong",
Toast.LENGTH_SHORT).show();
} else {
global.score -= 5;
score2.setText(String.valueOf(global.score));
Toast.makeText(getApplicationContext(), "Wrong",
Toast.LENGTH_SHORT).show();
}
}
});
}
As your second activity inflates a new layout, you need to explicitly pass your value from the first to the second activity and initialize it's TextView using this value.
Activity 1:
void goToSecondActivity() {
String value = mTextView.getText();
Intent in = new Intent(getActivity(), YourSecondClass.class);
in.putExtra("score", value);
startActivity(in);
}
Activity 2:
void onCreate(Bundle bundle) {
...
String score = getIntent().getStringExtra("score", "No score.");
mTextView.setText(score);
}
Use something like this.
Activity 1:
Intent i = new Intent(getActivity(), Activity2.class);
i.putExtra("Score", 20);
startActivity(i);
Activity 2:
Intent i = getIntent();
int score = i.getIntExtra("Score", 0);
My answer is also similar to what the others have suggested. try this, I have added some extra statements within your code(I presumed you want to send the value of variable score to the next activity, you can replace it with the variable you want to send) :
first activity:
score2.setText(String.valueOf(score));
Toast.makeText(getApplicationContext(), "Correct",
Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(this,
sound1_3pig.class);
myIntent.putExtra("Score", global.score);
startActivityForResult(myIntent, 0);
second activity:
put this in the onCreate() method of the second activity:
Intent intent = getIntent();
int score = intent.getIntExtra("Score",0);
so now you will get the value of score from the previous activity in the second activity. then you can set it in the textView you want to display it in by calling
textView.setText(score);
Related
I cannot get the four multiple choice buttons and the question textView to display text. What happens is there is an intent transferred over with a selected category "who" or "what". That category is compared with the "who" and "what" if statements. If it is one of them then it is supposed to post the question and run through the ten question. I am using a switch case inside the if statement to help distinguish what button was clicked and move on. When I run the emulator it gets to the screen with four multiple choice buttons but nothing is display on the text or text view.
public class QuestionActivity extends AppCompatActivity {
List<QuestionsTable> whoQuesList;
List<QuestionsTable> whatQuesList;
int score=0;
int qid=0;
QuestionsTable currentWhatQ;
QuestionsTable currentWhoQ;
Button btnA, btnB, btnC, btnD;
Button butSkip;
TextView txtQuestion;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
butSkip = (Button) findViewById(R.id.btnSkip);
MySQLiteHelper db = new MySQLiteHelper(this);
whoQuesList = db.getAllWhos();
whatQuesList = db.getAllWhos();
currentWhatQ = whatQuesList.get(qid);
currentWhoQ = whoQuesList.get(qid);
}
public void onClickQuestion(View txtViewQuestion) {
Bundle d = getIntent().getExtras();
int c = d.getInt("category");
String category = Integer.toString(c);
if ("Who".equals(category)) {
txtQuestion = (TextView) findViewById(R.id.txtViewQuestion);
btnA = (Button) findViewById(R.id.btnOptionA);
btnB = (Button) findViewById(R.id.btnOptionB);
btnC = (Button) findViewById(R.id.btnOptionC);
btnD = (Button) findViewById(R.id.btnOptionD);
setQuestionView();
switch (txtViewQuestion.getId()) {
case btnOptionA:
if (currentWhoQ.getAnswer().equals(btnA.getText())) {
score++;
}
if (qid < 10) {
currentWhoQ = whoQuesList.get(qid);
setQuestionView();
} else {
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 Intent
startActivity(intent);
}
break;
case btnOptionB:
if (currentWhoQ.getAnswer().equals(btnB.getText())) {
score++;
}
if (qid < 10) {
currentWhoQ = whoQuesList.get(qid);
setQuestionView();
} else {
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 Intent
startActivity(intent);
}
break;
case btnOptionC:
if (currentWhoQ.getAnswer().equals(btnC.getText())) {
score++;
}
if (qid < 10) {
currentWhoQ = whoQuesList.get(qid);
setQuestionView();
} else {
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 Intent
startActivity(intent);
}
break;
case R.id.btnOptionD:
if (currentWhoQ.getAnswer().equals(btnD.getText())) {
score++;
}
if (qid < 10) {
currentWhoQ = whoQuesList.get(qid);
setQuestionView();
} else {
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 Intent
startActivity(intent);
}
break;
}
}
if ("What".equals(category)) {
MySQLiteHelper db = new MySQLiteHelper(this);
whoQuesList = db.getAllWhos();
whatQuesList = db.getAllWhos();
currentWhatQ = whatQuesList.get(qid);
currentWhoQ = whoQuesList.get(qid);
txtQuestion = (TextView) findViewById(R.id.txtViewQuestion);
btnA = (Button) findViewById(R.id.btnOptionA);
btnB = (Button) findViewById(R.id.btnOptionB);
btnC = (Button) findViewById(R.id.btnOptionC);
btnD = (Button) findViewById(R.id.btnOptionD);
setWhatQuestionView();
switch (txtViewQuestion.getId()) {
case btnOptionA:
if (currentWhatQ.getAnswer().equals(btnA.getText())) {
score++;
}
if (qid < 10) {
currentWhatQ = whatQuesList.get(qid);
setQuestionView();
} else {
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 Intent
startActivity(intent);
}
break;
case btnOptionB:
if (currentWhatQ.getAnswer().equals(btnB.getText())) {
score++;
}
if (qid < 10) {
currentWhatQ = whatQuesList.get(qid);
setQuestionView();
} else {
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 Intent
startActivity(intent);
}
break;
case btnOptionC:
if (currentWhatQ.getAnswer().equals(btnC.getText())) {
score++;
}
if (qid < 10) {
currentWhatQ = whatQuesList.get(qid);
setQuestionView();
} else {
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 Intent
startActivity(intent);
}
break;
case R.id.btnOptionD:
if (currentWhatQ.getAnswer().equals(btnD.getText())) {
score++;
}
if (qid < 10) {
currentWhatQ = whatQuesList.get(qid);
setQuestionView();
} else {
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 Intent
startActivity(intent);
}
break;
}
}
}
public void setQuestionView() {
txtQuestion.setText(currentWhoQ.getQuestion());
btnA.setText(currentWhoQ.getMultipleChoiceA());
btnB.setText(currentWhoQ.getMultipleChoiceB());
btnC.setText(currentWhoQ.getMultipleChoiceC());
btnD.setText(currentWhoQ.getMultipleChoiceD());
qid++;
}
public void setWhatQuestionView() {
txtQuestion.setText(currentWhatQ.getQuestion());
btnA.setText(currentWhatQ.getMultipleChoiceA());
btnB.setText(currentWhatQ.getMultipleChoiceB());
btnC.setText(currentWhatQ.getMultipleChoiceC());
btnD.setText(currentWhatQ.getMultipleChoiceD());
qid++;
}
Not Sure, what type of Data your txtViewQuestion is...
The Switch Statement Works perfectly with primitive Data types...
If you try to call the Name of the Button Variable, this will Not Work because a program only works with memory adress, so Java will Not be able to give you the name of your object (if its a Button)
To solve this, you could Pass a Parameter to the Methods (for example int)
0 for the first button
1 for the second button
2 for the third...
And then you could "Switch" this Parameter
Bundle d = getIntent().getExtras();
int c = d.getInt("category");
String category = Integer.toString(c);
if ("Who".equals(category)) {
What is the value of c? How could it ever equal "Who" or "What"?
And like others have said, use the full ID of the buttons in the switch statements.
case R.id.btnOptionA:
instead of
case btnOptionA:
What is btnOptionA, btnOptionB and btnOptionC? txtViewQuestion.getId() return an Integer, in case tag you should use R.id.btnOptionA, R.id.btnOptionB, R.id.btnOptionC...
I'm doing basic demo of android related to login, update user
I used user's id from login page to load user's information to update page
username = (EditText) findViewById(R.id.txtUsername);
age = (EditText) findViewById(R.id.txtAge);
weight = (EditText) findViewById(R.id.txtWeight);
height = (EditText) findViewById(R.id.txtHeight);
username.setText(db_username);
age.setText(db_age);
weight.setText(db_weight);
height.setText(db_height);
and update button
update.setOnClickListener(new View.OnClickListener() {
int sweight = Integer.parseInt(weight.getText().toString());
double sheight = Double.parseDouble(height.getText().toString());
int sage = Integer.parseInt(age.getText().toString());
#Override
public void onClick(View v) {
if (weight.equals("")) {
Toast.makeText(getApplicationContext(), "Please input weight!",
Toast.LENGTH_LONG).show();
return;
}else if(height.equals("")){
Toast.makeText(getApplicationContext(), "Please input height!",
Toast.LENGTH_LONG).show();
return;
}else if(age.equals("")){
Toast.makeText(getApplicationContext(), "Please input age!",
Toast.LENGTH_LONG).show();
return;
}else{
dbHandler.updateUser(sid,sweight,sheight,sage);
Toast.makeText(getApplicationContext(),
"User's information updated! ", Toast.LENGTH_LONG)
.show();
Toast.makeText(getApplicationContext(),
sid+sweight+sheight+sage+" and test", Toast.LENGTH_LONG)
.show();
Intent i = new Intent(UserUpdate.this, MainActivity.class);
startActivity(i);
finish();
}
}
});
For exam: weight: 70,height:1,65,age:21
when i changed it to: weight:50,height:1,65,age:21
the 2nd Toast show me that weight : 70 but not 50
You need to move these top three lines inside of the click method, not declare them as member variables of the anonymous class.
int sweight = Integer.parseInt(weight.getText().toString());
double sheight = Double.parseDouble(height.getText().toString());
int sage = Integer.parseInt(age.getText().toString());
#Override
public void onClick(View v) {
// Move them here
put this :
int sweight = Integer.parseInt(weight.getText().toString());
double sheight = Double.parseDouble(height.getText().toString());
int sage = Integer.parseInt(age.getText().toString());
in onclick method , before -> if (weight.equals(""))
Here is my current AddDebt.java section I'm looking at:
public void ButtonOnClick(View v) {
Intent intent = new Intent(this, MainActivity.class);
EditText debtors = (EditText) findViewById(R.id.editDebtor);
String debtor = debtors.getText().toString();
EditText myEdit = (EditText) findViewById(R.id.editBalance);
String myEditValue = myEdit.getText().toString();
double loanAmount = Double.parseDouble(myEditValue);
EditText myEdit2 = (EditText) findViewById(R.id.editRate);
String myEditValue2 = myEdit2.getText().toString();
double interestRate = Double.parseDouble(myEditValue2);
EditText myEdit3 = (EditText) findViewById(R.id.editTerm);
String myEditValue3 = myEdit3.getText().toString();
Double loanPeriod = Double.parseDouble(myEditValue3);
double r = interestRate/1200;
double r1 = Math.pow(r+1,loanPeriod);
double editMnthlypmt = (double) ((r+(r/(r1-1))) * loanAmount);
DecimalFormat df = new DecimalFormat("#.##");
editMnthlypmt = Double.valueOf(df.format(editMnthlypmt));
TextView textMnthlypmt = (TextView)findViewById(R.id.textMntlypmt);
switch (v.getId()) {
case R.id.calculate:
textMnthlypmt.setText("" + String.valueOf(editMnthlypmt));
break;
case R.id.addDebt:
if(debtors.getText().length() == 0){
Toast.makeText(getApplicationContext(), "Please enter debtors value", Toast.LENGTH_SHORT).show();
debtors.requestFocus();
}else if(myEdit.getText().length() == 0){
Toast.makeText(getApplicationContext(), "Please enter myedit value", Toast.LENGTH_SHORT).show();
myEdit.requestFocus();
}
else
{
//Transferring data to MainActivity
intent.putExtra("debtor",debtor);
intent.putExtra("loanAmount",loanAmount);
intent.putExtra("editMnthlypmt",editMnthlypmt);
//Next moves back to MainActivity
startActivity(intent);
}
break;
}
}
when "case R.id.addDebt:" is being chosen, I want to ensure that editDebtor, editBalance, editRate, and editTerm are all completed. If not, I want it to set focus on the topmost box that is incomplete. If completed I want it to switch to my intent.
Any suggestions.
try this,
case R.id.addDebt:
if(debtors.getText().length() == 0){
Toast.makeText(Activityname, "Please enter debtors value", Toast.LENGTH_SHORT).show();
debtors.requestFocus();
}else if(myEdit.getText().length() == 0){
Toast.makeText(Activityname, "Please enter myedit value", Toast.LENGTH_SHORT).show();
myEdit.requestFocus();
}else if().......
}else{
//Add your Intent here
}
I have read the previously posted questions and answers for 2 days and I've tried every variation suggested as well as setting my launchMode attribute to "standard" in my manifest.
I'm trying to pass data from my second activity back to my first activity after pressing a button. After I press the button, the first activity is launched but it doesn't go back to my onActivityResult() method. I can't figure out why this is happening.
Here's my code from activity 2:
Button btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Check that message is printed out to LogCat
System.out.println("hello test1 Activity2");
EditText band = (EditText) findViewById(R.id.txtBand);
band.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(9)
});
EditText album = (EditText) findViewById(R.id.txtAlbum);
album.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(9)
});
final Spinner genre = (Spinner) findViewById(R.id.spin_genre);
TextView selection = (TextView)genre.getSelectedView();
CharSequence strBand = band.getText();
CharSequence strAlbum = album.getText();
CharSequence strGenre = selection.getText();
//Check that we got input values
System.out.println("hello test Activity2- " +
strBand + " - " + strAlbum + " - " + strGenre);
//**********Intent Declaration************
Intent i = new Intent(getApplicationContext(), Activity1.class);
i.putExtra("strBand", strBand);
i.putExtra("strAlbum", strAlbum);
i.putExtra("strGenre", strGenre);
startActivityForResult(i, 0);
setResult(RESULT_OK, i);
finish();
}
});
Here's activity 1:
public class Activity1 extends Activity {
/** Called when the activity is first created. */
#SuppressWarnings("deprecation")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button addAlbum = (Button) findViewById(R.id.btnMain);
addAlbum.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent();
i.setClassName("jorge.jorge.jorge",
"jorge.jorge.jorge.Activity2");
startActivity(i);
}
});
}// end of onCreate()
//******************Callback Method****************************
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//Checks if we got to ActivityResult
System.out.println("hello test 2: We got to Activity1");
if (resultCode == RESULT_OK)
{
Bundle returndata = data.getExtras();
String strAlbum = returndata.getString("strAlbum");
String strBand = returndata.getString("strBand");
String strGenre = returndata.getString("strGenre");
// check to see if we got the variable values from activity2
System.out.println("hello test 2: We got to Activity1 with variables - "
+ strBand + " - " + strAlbum + " - " + strGenre);
//Create table layout to contains views with variable values
TableLayout table = new TableLayout(this);
table.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//creates row with parameters
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
//text views to contain variable values
TextView tv1 = new TextView(this);
tv1.setText(strBand);
row.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(strAlbum);
row.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(strGenre);
row.addView(tv3);
//adds the table row to the table layout
table.addView(row);
}
}// end onActivityResult()
}
I'm not sure if my activity callbacks are not placed properly in the code or if I'm not firing the intent properly or if I'm not setting up the callback with the right method or what. I know this topics has been discussed but I'm out of ideas. Thanks.
You've just got it backwards. If Activity1 is supposed to startActivity2 and Activity2 is supposed to send the result back to Activity1, you need to do it like this:
in Activity1:
Intent i = new Intent();
i.setClassName("jorge.jorge.jorge", "jorge.jorge.jorge.Activity2");
startActivityForResult(i); // This starts Activity2 and waits for the result
in Activity2:
Intent i = new Intent(getApplicationContext(), Activity1.class);
i.putExtra("strBand", strBand);
i.putExtra("strAlbum", strAlbum);
i.putExtra("strGenre", strGenre);
setResult(RESULT_OK, i);
finish(); // This closes Activity2 and generates the callback to Activity.onActivityResult()
I'm having trouble with making my randomly generated image, which is interpreted as a button, become clickable. Each leads to a different activity.
The random images work perfect actually, the only problem it's not clickable.
Here's my Main.java:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final List<String> images = new ArrayList<String>();
for (int i=1; i<=13; i++) {
images.add("img"+i);
}
final Button imgView = (Button)findViewById(R.id.top1);
String imgName = null;
int id = 0;
Collections.shuffle(images, new Random());
imgName = images.remove(0);
imageRandomizer(imgName, id, imgView);
}
public void imageRandomizer(String imgName, int id, final Button imgView) {
id = getResources().getIdentifier(imgName,
"drawable",
getPackageName());
imgView.setBackgroundResource(id);
}
}
On my layout, I specified the id top1 as a Button. So the above code will look up to my drawable images, which have the names img1.jpg, img2.jpg, img3.jpg , until img13.jpg.
Making an ImageButton clickable to one activity without being dependent on the shown random image is easy, I can do it without problem.
But what I wanna make is something like, when img1.jpg is generated, it becomes clickable and leads to Activity1.java, for img2.jpg the intent goes to Activity2.java, etc.
EDIT
#Roflcoptr
Here's my OnClickListener:
private OnClickListener top_listener = new OnClickListener() {
public void onClick(View v) {
switch((Integer) v.getTag()) {
case 1:
Intent aid = new Intent(Main.this, ProjektAID.class);
startActivity(aid);
case 2:
Intent adh = new Intent(Main.this, ProjektADH.class);
startActivity(adh);
case 3:
Intent bos = new Intent(Main.this, ProjektBOS.class);
startActivity(bos);
case 4:
Intent brot = new Intent(Main.this, ProjektBROT.class);
startActivity(brot);
case 5:
Intent care = new Intent(Main.this, ProjektCARE.class);
startActivity(care);
case 6:
Intent caritas = new Intent(Main.this, ProjektCARITAS.class);
startActivity(caritas);
case 7:
Intent doc = new Intent(Main.this, ProjektDOC.class);
startActivity(doc);
case 8:
Intent drk = new Intent(Main.this, ProjektDRK.class);
startActivity(drk);
case 9:
Intent give = new Intent(Main.this, ProjektGIVE.class);
startActivity(give);
case 10:
Intent hive = new Intent(Main.this, ProjektHIV.class);
startActivity(hive);
case 11:
Intent jo = new Intent(Main.this, ProjektJOHANNITER.class);
startActivity(jo);
case 12:
Intent kind = new Intent(Main.this, ProjektKINDERHERZ.class);
startActivity(kind);
case 13:
Intent kult = new Intent(Main.this, ProjektKULTURGUT.class);
startActivity(kult);
}
}
};
and here's the randomizer method:
public void imageRandomizer(String imgName, int id, final Button imgView) {
id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setBackgroundResource(id);
imgView.setTag(new Integer(1)); //example for image 1
if (imgName.equals("img1")) {
imgView.setTag(new Integer(1)); //example for image 1
} else if (imgName.equals("img2")) {
imgView.setTag(new Integer(2));
} else if (imgName.equals("img3")) {
imgView.setTag(new Integer(3));
} else if (imgName.equals("img4")) {
imgView.setTag(new Integer(4));
} else if (imgName.equals("img5")) {
imgView.setTag(new Integer(5));
} else if (imgName.equals("img6")) {
imgView.setTag(new Integer(6));
} else if (imgName.equals("img7")) {
imgView.setTag(new Integer(7));
} else if (imgName.equals("img8")) {
imgView.setTag(new Integer(8));
} else if (imgName.equals("img9")) {
imgView.setTag(new Integer(9));
} else if (imgName.equals("img10")) {
imgView.setTag(new Integer(10));
} else if (imgName.equals("img11")) {
imgView.setTag(new Integer(11));
} else if (imgName.equals("img12")) {
imgView.setTag(new Integer(12));
}
else if (imgName.equals("img13")) {
imgView.setTag(new Integer(13));
}
}
I would use a tag to identify the button. So in your imateRandomizer add a unique ID for each possible Image. I don't know how you can identify the images uniquely, but I'll show the example here for the name:
public void imageRandomizer(String imgName, int id, final Button imgView)
{
id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setBackgroundResource(id);
if (imgName.equals("Name of the Image for your first activity") {
imgView.setTag(new Integer(1)); //example for image 1
} else if (imgName.equals("Name of the Image for your second activity") {
imgView.setTag(new Integer(2));
}
}
And then In your ClickListener you can check which tag the button has:
imgView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch((Integer) v.getTag()) {
case 1: //start Activity 1;
break;
case 2: //start Activity 2;
break;
}
}
});