Issue
I am fairly new to Android and I am trying to create an App that will keep the score of a match.
I have 6 buttons, 3 for each team, which are assigned different incremental values in relation to scores in a rugby match. With these buttons I also want to be able to track the amount of times each button has been clicked and return these values in a Toast
message and also an E-Mail Intent.
I have already created the Toast message and email intent, I just need to populate them with the tally from the button clicks which I have already declared as countPenA
, etc. The App works fine as is just this issue.
I know its probably simple thing to some, but it has beaten me. I cant figure it out.
public class rugby_counter extends Activity implements View.OnClickListener {
EditText editTextA, editTextB;
TextView textViewA, textViewB ;
Button penA, conA, tryA, penB, conB, tryB, reSet;
int countA = 0;
int countB = 0;
int countPenA, countConA, countTryA, countPenB, countConB, countTryB = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rugby_counter);
editTextA = (EditText) findViewById(R.id.editTextA);
editTextB = (EditText) findViewById(R.id.editTextB);
textViewA = (TextView) findViewById(R.id.textViewA);
textViewB = (TextView) findViewById(R.id.textViewB);
penA = (Button) findViewById(R.id.penBtnA);
tryA = (Button) findViewById(R.id.tryBtnA);
conA = (Button) findViewById(R.id.conBtnA);
penB = (Button) findViewById(R.id.penBtnB);
tryB = (Button) findViewById(R.id.tryBtnB);
conB = (Button) findViewById(R.id.conBtnB);
reSet = (Button) findViewById(R.id.restBtn);
//---set on click listeners on the buttons-----
penA.setOnClickListener(this);
tryA.setOnClickListener(this);
conA.setOnClickListener(this);
penB.setOnClickListener(this);
tryB.setOnClickListener(this);
conB.setOnClickListener(this);
reSet.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.penBtnA:
countA += 3;
textViewA.setText(Integer.toString(countA));
break;
case R.id.conBtnA:
countA += 2;
textViewA.setText(Integer.toString(countA));
break;
case R.id.tryBtnA:
countA += 5;
textViewA.setText(Integer.toString(countA));
break;
case R.id.penBtnB:
countB += 3;
textViewB.setText(Integer.toString(countB));
break;
case R.id.conBtnB:
countB += 2;
textViewB.setText(Integer.toString(countB));
break;
case R.id.tryBtnB:
countB += 5;
textViewB.setText(Integer.toString(countB));
break;
case R.id.restBtn:
if (v == reSet) {
countA = 0;
countB = 0;
textViewA.setText(Integer.toString(countA));
textViewB.setText(Integer.toString(countB));
editTextA.setText("");
editTextB.setText("");
}
}
}
public void summary(View view) {
String teamA = editTextA.getText().toString();
String teamB = editTextB.getText().toString();
String scoreA = textViewA.getText().toString();
String scoreB = textViewB.getText().toString();
if (teamA.equals("") || teamB.equals("") || scoreA.equals("") || scoreB.equals(""))
{
Toast noScore = Toast.makeText(this, getString(R.string.noScoreToast)
, Toast.LENGTH_LONG);
noScore.show();
}
else {
Toast scores = Toast.makeText(this, "SCORE: \n" + teamA + ":" + scoreA + "\n" + teamB + ":" + scoreB +
"\n \n" + "*****MATCH STATISTICS*****" +
"\n \n" + teamA + ":" + "PENALTIES-" + "CONVERSIONS-" + "TRIES- \n" + teamB + ":" + "PENALTIES-" + "CONVERSIONS-" + "TRIES-", Toast.LENGTH_LONG);
scores.show();
}
}
public void email (View view)
{
String teamA = editTextA.getText().toString();
String teamB = editTextB.getText().toString();
String scoreA = textViewA.getText().toString();
String scoreB = textViewB.getText().toString();
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Match Result");
emailIntent.putExtra(Intent.EXTRA_TEXT, "SCORE: \n" + teamA + ":" + scoreA + "\n" + teamB + ":" + scoreB +
"\n \n" + "*****MATCH STATISTICS*****" +
"\n \n" + teamA + ":" + "PENALTIES-" + "CONVERSIONS-" +"TRIES- \n" + teamB + ":"+ "PENALTIES-" + "CONVERSIONS-" +"TRIES-");
startActivity(Intent.createChooser(emailIntent, getString(R.string.email_choose)));
}
}
Solution
First of all initialize your countPenA and countPenB to zero at the start then, on click of the button "A" just increment the values like this in your onClickListener code for button "A" :-
countPenA++;
Now, you can use this variable to get the count of clicks made by the user on button "A" and similarly do the same for button "B" and any other button you need to.
Answered By - Varun Kumar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.