Im having problem sharing a global variable that I need to retain across multiple activities. This variable is created in TextPlay class and copied onto DailyDataEntry class. I have done some research sharing variables between two activities using Intent but Im looking to share that variable to multiple classes. I think I will have to extends Application but all my other classes are extending Activity already. How am I supposed to extend Application while extending Activity? Thanks a million
///TextPlay Class///
package com.armstrong.y.android.app;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;
public class TextPlay extends Activity implements View.OnClickListener {
Button chkCmd;
ToggleButton passTog;
EditText input;
TextView display;
EditText etUserId;
Button btnUserId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
baconAndEggs();
passTog.setOnClickListener(this);
chkCmd.setOnClickListener(this);
// Share variables between classes controls
etUserId = (EditText) findViewById(R.id.etUserId);
btnUserId = (Button) findViewById(R.id.btnUserId);
}
private void baconAndEggs() {
// TODO Auto-generated method stub
chkCmd = (Button) findViewById(R.id.bResults);
passTog = (ToggleButton) findViewById(R.id.tbPassword);
input = (EditText) findViewById(R.id.etCommands);
display = (TextView) findViewById(R.id.tvResults);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bResults:
// get the text from the command box then convert it to string
String check = input.getText().toString();
display.setText(check);
if (check.contentEquals("left")) {
display.setGravity(Gravity.LEFT);
} else if (check.contentEquals("center")) {
display.setGravity(Gravity.CENTER);
} else if (check.contentEquals("right")) {
display.setGravity(Gravity.RIGHT);
} else if (check.contentEquals("blue")) {
display.setTextColor(Color.BLUE);
display.setText("blue");
} else if (check.contains("hellow")) {
Random crazy = new Random();
display.setText("hello!!!!");
display.setTextSize(crazy.nextInt(75));
display.setTextColor(Color.rgb(crazy.nextInt(255),
crazy.nextInt(255), crazy.nextInt(255)));
switch (crazy.nextInt(3)) {
case 0:
display.setGravity(Gravity.LEFT);
break;
case 1:
display.setGravity(Gravity.CENTER);
break;
case 2:
display.setGravity(Gravity.RIGHT);
break;
}
} else {
display.setText("invalid");
display.setGravity(Gravity.CENTER);
display.setTextColor(Color.BLACK);
}
break;
case R.id.tbPassword:
// If toggle button is set to ON, password will be ***
if (passTog.isChecked()) {
input.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
} else {
// if toggle is off, its going be plain text
input.setInputType(InputType.TYPE_CLASS_TEXT);
}
break;
}
}
}
///DailyDataEntry Class///
package com.armstrong.y.android.app;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class DailyDataEntry extends Activity {
String strdate;
EditText etGetDate;
Calendar calendarDateToday = Calendar.getInstance();
Calendar calendarDateNext = Calendar.getInstance();
Calendar calendarDatePrevious = Calendar.getInstance();
int counterNext = 0;
int counterPrevious = 0;
Button btnTodayDate;
Button btnPreviousDate;
Button btnGetNextDate;
EditText etDailyCaloriesIn;
EditText etDailyCaloriesOut;
EditText etDailyAnxietyLevel;
EditText etDailySleepQuality;
TextView tvUserId;
Button btnSubmit;
Button btnReset;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daily_data_entry);
// Toast Message Warning
Toast.makeText(getApplicationContext(),
"DO NOT LEAVE FIELD BLANK OR CRASH IS ON YOU!",
Toast.LENGTH_LONG).show();
btnTodayDate = (Button) findViewById(R.id.btnGetTodayDate);
btnPreviousDate = (Button) findViewById(R.id.btnGetPreviousDate);
btnGetNextDate = (Button) findViewById(R.id.btnGetNextDate);
etGetDate = (EditText) findViewById(R.id.etGetDate);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnReset = (Button) findViewById(R.id.btnReset);
etDailyCaloriesIn = (EditText) findViewById(R.id.etDailyCaloriesIn);
etDailyCaloriesOut = (EditText) findViewById(R.id.etDailyCaloriesOut);
etDailyAnxietyLevel = (EditText) findViewById(R.id.etDailyAnxietyLevel);
etDailySleepQuality = (EditText) findViewById(R.id.etDailySleepQuality);
tvUserId = (TextView) findViewById(R.id.tvUserId);
// When Submit Button is clicked
btnSubmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Grab User Input from EditText and Convert input String to
// Integer
int check1 = Integer.parseInt(etDailyCaloriesIn.getText()
.toString());
int check2 = Integer.parseInt(etDailyCaloriesOut.getText()
.toString());
int check4 = Integer.parseInt(etDailyAnxietyLevel.getText()
.toString());
int check5 = Integer.parseInt(etDailySleepQuality.getText()
.toString());
strdate = etGetDate.getText().toString();
// Run Legal Value Integrity Check
if (strdate.length() != 8) {
etGetDate.setText("Please enter in the correct format.");
} else if (strdate.equals("")) {
etGetDate.setText("Please enter a date.");
} else if (check1 > 10 || check1 < 1) {
etDailyCaloriesIn.setText("Incorrect Value!");
} else if (check2 > 10 || check2 < 1) {
etDailyCaloriesOut.setText("Incorrect Value!");
} else if (check4 > 10 || check4 < 1) {
etDailyAnxietyLevel.setText("Incorrect Value!");
} else if (check5 > 10 || check5 < 1) {
etDailySleepQuality.setText("Incorrect Value!");
} else {
etGetDate.setText("Submited!");
etDailyCaloriesIn.setText("Submited!");
etDailyCaloriesOut.setText("Submited!");
etDailyAnxietyLevel.setText("Submited!");
etDailySleepQuality.setText("Submited!");
}
}
});
// When Reset Button is Clicked
btnReset.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String empty = "";
etDailyCaloriesIn.setText(empty);
etDailyCaloriesOut.setText(empty);
etDailyAnxietyLevel.setText(empty);
etDailySleepQuality.setText(empty);
etGetDate.setText(empty);
}
});
// Capture Today's Date
btnTodayDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
strdate = sdf.format(calendarDateToday.getTime());
etGetDate.setText(strdate);
}
});
// Capture Previous's Date
btnPreviousDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
if (counterPrevious == 0) {
calendarDatePrevious.add(Calendar.DATE, -1);
}
strdate = sdf.format(calendarDatePrevious.getTime());
etGetDate.setText(strdate);
counterPrevious++;
}
});
// Capture Next's Date
btnGetNextDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
if (counterNext == 0) {
calendarDateNext.add(Calendar.DATE, +1);
}
strdate = sdf.format(calendarDateNext.getTime());
etGetDate.setText(strdate);
counterNext++;
}
});
}
}
Create class that will extends to Application and use it in the following manner whenever you need it:
MyApplication myAppHelper = (MyApplication) context.getApplication();
myAppHelper.setWhatever(whatever);
Whatever whatever = myAppHelper.getWhatever();
Related
I am creating an app which calculates the total marks & percentage and shows the result in the next activity called ResultActvity. I am using same ResultActivity for showing result for 1st yr student & 2nd yr student marks, everything works fine when it come to show the result for 1st yr students but when it comes to show the result for second yr students it is only showing total marks but no % is shown. I am a newbie to android development please help me in fixing this. Thank You :).
FIRSTACTIVITY
package com.jntuhcalculator.anu.jntuhcalculator;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by anu on 18/9/15.
*/
public class FirstMarks extends Activity {
TextView tv_Subjects1, tv_Internal1, tv_External1;
EditText et_Int_Eng, et_Int_M1, et_Int_MM, et_Int_Phy, et_Int_Chem, et_Int_Cp, et_Int_ElcsLab, et_Int_EpLab, et_Int_ItLab, et_Int_Draw;
EditText et_Ext_Eng, et_Ext_M1, et_Ext_MM, et_Ext_Phy, et_Ext_Chem, et_Ext_Cp, et_Ext_ElcsLab, et_Ext_EpLab, et_Ext_ItLab, et_Ext_Draw;
Button btn_Cal1, btn_Ok1;
int IEng,IM1,IMM,IEPhy,IEChem,ICp,IEDraw,IELCS_Lab,IEP_LAB,IIT_LAB;
int EEng,EM1,EMM,EEPhy,EEChem,ECp,EEDraw,EELCS_Lab,EEP_LAB,EIT_LAB;
int ITotal1,ETotal1,Total1;
float Percentage1;
String f1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.marks_1);
//INITIALISING VIEWS.
//INITIALISING TEXT VIEWS.
tv_Subjects1 = (TextView) findViewById(R.id.tv_Subject1);
tv_Internal1 = (TextView) findViewById(R.id.tv_Internal1);
tv_External1 = (TextView) findViewById(R.id.tv_External1);
//INITIALISING EDIT TEXT.
//INTERNAL
et_Int_Eng = (EditText) findViewById(R.id.et_Int_Eng);
et_Int_M1 = (EditText) findViewById(R.id.et_Int_M1);
et_Int_MM = (EditText) findViewById(R.id.et_Int_MM);
et_Int_Phy = (EditText) findViewById(R.id.et_Int_Phy);
et_Int_Chem = (EditText) findViewById(R.id.et_Int_Chem);
et_Int_Cp = (EditText) findViewById(R.id.et_Int_Cp);
et_Int_ElcsLab = (EditText) findViewById(R.id.et_Int_ElcsLab);
et_Int_EpLab = (EditText) findViewById(R.id.et_Int_EpLab);
et_Int_ItLab = (EditText) findViewById(R.id.et_Int_ItLab);
et_Int_Draw = (EditText) findViewById(R.id.et_Int_Draw);
//EXTERNAL
et_Ext_Eng = (EditText) findViewById(R.id.et_Ext_Eng);
et_Ext_M1 = (EditText) findViewById(R.id.et_Ext_M1);
et_Ext_MM = (EditText) findViewById(R.id.et_Ext_MM);
et_Ext_Phy = (EditText) findViewById(R.id.et_Ext_Phy);
et_Ext_Chem = (EditText) findViewById(R.id.et_Ext_Chem);
et_Ext_Cp = (EditText) findViewById(R.id.et_Ext_Cp);
et_Ext_ElcsLab = (EditText) findViewById(R.id.et_Ext_ElcsLab);
et_Ext_EpLab = (EditText) findViewById(R.id.et_Ext_EpLab);
et_Ext_ItLab = (EditText) findViewById(R.id.et_Ext_ItLab);
et_Ext_Draw = (EditText) findViewById(R.id.et_Ext_Draw);
//INITIALISING BUTTON
btn_Cal1 = (Button) findViewById(R.id.btn_Cal1);
btn_Ok1 = (Button) findViewById(R.id.btn_Ok1);
//WHEN SAVE BUTTON IS CLICKED.
btn_Ok1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//GETTING VALUES FROM EDIT TEXT.
//INTERNALS.
IEng = getIntValue(et_Int_Eng);
IM1 = getIntValue(et_Int_M1);
IMM = getIntValue(et_Int_MM);
IEPhy = getIntValue(et_Int_Phy);
IEChem = getIntValue(et_Int_Chem);
ICp = getIntValue(et_Int_Cp);
IEDraw = getIntValue(et_Int_Draw);
IELCS_Lab = getIntValue(et_Int_ElcsLab);
IEP_LAB = getIntValue(et_Int_EpLab);
//EXTERNALS.
EEng = getIntValue(et_Ext_Eng);
EM1 = getIntValue(et_Ext_M1);
EMM = getIntValue(et_Ext_MM);
EEPhy = getIntValue(et_Ext_Phy);
EEChem = getIntValue(et_Ext_Chem);
ECp = getIntValue(et_Ext_Cp);
EEDraw = getIntValue(et_Ext_Draw);
EELCS_Lab = getIntValue(et_Ext_ElcsLab);
EEP_LAB = getIntValue(et_Ext_EpLab);
EIT_LAB = getIntValue(et_Ext_ItLab);
//CALCUATIONS.
//INTERNAL TOTAL.
ITotal1 = (IEng+IM1+IMM+IEPhy+IEChem+ICp+IEDraw+IELCS_Lab+IEP_LAB+IIT_LAB);
//EXTERNAL TOTAL.
ETotal1 = (EEng+EM1+EMM+EEPhy+EEChem+ECp+EEDraw+EELCS_Lab+EEP_LAB+EIT_LAB);
//TOTAL.
Total1 = (ITotal1+ETotal1);
//PERCENTAGE.
Percentage1 = (float)(Total1/10);
f1 = Float.toString(Percentage1);
//CREATING TOAST.
Toast.makeText(getBaseContext(),"Press Result",Toast.LENGTH_SHORT).show();
}
});
btn_Cal1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(FirstMarks.this,FirstResult.class);
Bundle b = new Bundle();
b.putInt("res1",Total1);
b.putString("per1",f1);
i.putExtras(b);
startActivity(i);
finish();
}
});
}
//SUB-FUNCTION FOR GETTING INTEGER FROM EDITTEXT.
private int getIntValue(EditText et) throws NumberFormatException {
int i =0;
String s;
try {
s = et.getText().toString();
if(s.equals("")){
Toast.makeText(getApplicationContext(),"Invalid Marks",Toast.LENGTH_SHORT).show();
}
i = Integer.parseInt(s);
return i;
} catch (NumberFormatException e) {
Log.e("ERROR:", "NOT A NUMBER");
}
return 0;
}
}
**SECONDACTIVITY**
package com.jntuhcalculator.anu.jntuhcalculator;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by anu on 19/9/15.
*/
public class SecondMarks_1 extends Activity {
TextView tv_PS,tv_MFCS,tv_DS,tv_DLD,tv_EDC,tv_BEE,tv_EELab,tv_DSLab;
TextView tv_Subjects21, tv_Internal21, tv_External21;
EditText et_Int_PS, et_Int_MFCS, et_Int_DS, et_Int_DLD, et_Int_EDC, et_Int_BEE, et_Int_EELab, et_Int_DSLab;
EditText et_Ext_PS, et_Ext_MFCS, et_Ext_DS, et_Ext_DLD, et_Ext_EDC, et_Ext_BEE, et_Ext_EELab, et_Ext_DSLab;
Button btn_Cal21, btn_Ok21;
int IPS,IMFCS,IDS,IDLD,IEDC,IBEE,IEE_Lab,IDS_Lab;
int EPS,EMFCS,EDS,EDLD,EEDC,EBEE,EEE_Lab,EDS_Lab;
int ITotal21,ETotal21,Total21;
float Percentage21;
String f21;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondmarks_1);
//INITIALISING VIEWS.
//INITIALISING TEXT VIEWS.
tv_Subjects21 = (TextView) findViewById(R.id.tv_Subject21);
tv_Internal21 = (TextView) findViewById(R.id.tv_Internal21);
tv_External21 = (TextView) findViewById(R.id.tv_External21);
//SUBJECT TEXT VIEWS.
tv_PS = (TextView) findViewById((R.id.tv_PS));
tv_MFCS = (TextView) findViewById((R.id.tv_MFCS));
tv_DS = (TextView) findViewById((R.id.tv_DS));
tv_DLD = (TextView) findViewById((R.id.tv_DLD));
tv_EDC = (TextView) findViewById((R.id.tv_EDC));
tv_BEE = (TextView) findViewById((R.id.tv_BEE));
tv_EELab = (TextView) findViewById((R.id.tv_EE_Lab));
tv_DSLab = (TextView) findViewById((R.id.tv_DS_Lab));
//INITIALISING EDIT TEXT.
//INTERNAL
et_Int_PS = (EditText) findViewById(R.id.et_Int_PS);
et_Int_MFCS = (EditText) findViewById(R.id.et_Int_MFCS);
et_Int_DS = (EditText) findViewById(R.id.et_Int_DS);
et_Int_DLD = (EditText) findViewById(R.id.et_Int_DLD);
et_Int_EDC = (EditText) findViewById(R.id.et_Int_EDC);
et_Int_BEE = (EditText) findViewById(R.id.et_Int_BEE);
et_Int_EELab = (EditText) findViewById(R.id.et_Int_EELab);
et_Int_DSLab = (EditText) findViewById(R.id.et_Int_DSLab);
//EXTERNAL
et_Ext_PS = (EditText) findViewById(R.id.et_Ext_PS);
et_Ext_MFCS = (EditText) findViewById(R.id.et_Ext_MFCS);
et_Ext_DS = (EditText) findViewById(R.id.et_Ext_DS);
et_Ext_DLD = (EditText) findViewById(R.id.et_Ext_DLD);
et_Ext_EDC = (EditText) findViewById(R.id.et_Ext_EDC);
et_Ext_BEE = (EditText) findViewById(R.id.et_Ext_BEE);
et_Ext_EELab = (EditText) findViewById(R.id.et_Ext_EELab);
et_Ext_DSLab = (EditText) findViewById(R.id.et_Ext_DSLab);
//INITIALISING BUTTON
btn_Cal21 = (Button) findViewById(R.id.btn_Cal21);
btn_Ok21 = (Button) findViewById(R.id.btn_Ok21);
//DISPLAYING SUBJECT NAMES WHEN TEXT VIEW IS CLICKED.
tv_PS.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
disp(tv_PS, "PS:Probabilty & Statistics");
}
});
tv_MFCS.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
disp(tv_MFCS, "MFCS:Mathematical Foundation Of Computer Science");
}
});
tv_DS.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {disp(tv_DS, "DS:Data Structures");
}
});
tv_DLD.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
disp(tv_DLD, "DLD:Digital Logic Design");
}
});
tv_EDC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
disp(tv_EDC, "EDC:Electronic Devices & Circuits");
}
});
tv_BEE.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { disp(tv_BEE, "BEE:Basic Electrical Engineering");
}
});
tv_EELab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { disp(tv_EELab, "Electrical & Electronics Lab");
}
});
tv_DSLab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {disp(tv_DSLab, "Data Structures Lab");
}
});
//WHEN SAVE BUTTON IS CLICKED.
btn_Ok21.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//GETTING VALUES FROM EDIT TEXT.
//INTERNALS.
IPS = getIntValue(et_Int_PS);
IMFCS = getIntValue(et_Int_MFCS);
IDS = getIntValue(et_Int_DS);
IDLD = getIntValue(et_Int_DLD);
IEDC = getIntValue(et_Int_EDC);
IBEE = getIntValue(et_Int_BEE);
IEE_Lab = getIntValue(et_Int_EELab);
IDS_Lab = getIntValue(et_Int_DSLab);
//EXTERNALS.
EPS = getIntValue(et_Ext_PS);
EMFCS = getIntValue(et_Ext_MFCS);
EDS = getIntValue(et_Ext_DS);
EDLD = getIntValue(et_Ext_DLD);
EEDC = getIntValue(et_Ext_EDC);
EBEE = getIntValue(et_Ext_BEE);
EEE_Lab = getIntValue(et_Ext_EELab);
EDS_Lab = getIntValue(et_Ext_DSLab);
//CALCULATIONS.
//INTERNAL TOTAL.
ITotal21 = (IPS+IMFCS+IDS+IDLD+IEDC+IBEE+IEE_Lab+IDS_Lab);
//EXTERNAL TOTAL.
ETotal21 = (EPS+EMFCS+EDS+EDLD+EEDC+EBEE+EEE_Lab+EDS_Lab);
//TOTAL.
Total21 = (ITotal21+ETotal21);
//PERCENTAGE.
Percentage21 = ((Total21/750)*100);
f21 = Float.toString(Percentage21);
//CREATING TOAST.
Toast.makeText(getBaseContext(),"Press Result",Toast.LENGTH_SHORT).show();
}
});
btn_Cal21.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(SecondMarks_1.this,FirstResult.class);
Bundle b = new Bundle();
b.putInt("res1",Total21);
b.putString("per1",f21);
i.putExtras(b);
startActivity(i);
finish();
}
});
}
//SUB-FUNCTION FOR GETTING INTEGER FROM EDITTEXT.
private int getIntValue(EditText et) throws NumberFormatException {
int i =0;
String s;
try {
s = et.getText().toString();
if(s.equals("")){
Toast.makeText(getApplicationContext(),"Invalid Marks",Toast.LENGTH_SHORT).show();
}
i = Integer.parseInt(s);
return i;
} catch (NumberFormatException e) {
Log.e("ERROR:", "NOT A NUMBER");
}
return 0;
}
private void disp(TextView tv,String s){
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
}
**RESULTACTIVITY**
<pre><code>
package com.jntuhcalculator.anu.jntuhcalculator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by anu on 18/9/15.
*/
public class FirstResult extends Activity {
EditText et_Total1,et_Percentage1;
Button btn_Exit1;
int sres1;
String sper1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_first);
//INITIALISING VIEWS.
et_Total1 = (EditText) findViewById(R.id.et_Total1);
et_Percentage1 = (EditText) findViewById(R.id.et_Percentage1);
btn_Exit1 = (Button) findViewById(R.id.btn_Exit1);
//GETTING DATA FROM PREVIOUS ACTIVITY.
Bundle b = getIntent().getExtras();
sres1 = b.getInt("res1", 0);
sper1 = b.getString("per1");
//Converting Float Into String.
//f = Float.toString(sper1);
//SETTING RESULTS.
et_Total1.setText(sres1+"");
et_Percentage1.setText(sper1+"%");
//WHEN EXIT IS CLICKED.
btn_Exit1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getBaseContext(), YearDetails.class);
i.putExtra("Total",sres1);
startActivity(i);
finish();
System.exit(0);
}
});
}
}
You are properly not able to send values between different activities, i will give you an example, try it.
The syntax is like this
For sending data
String data = getIntent().getExtras().getString("keyName");
Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
intent.putExtra("keyName","value");
For receiving at ActivityTwo
String value= getIntent().getStringExtra("keyName");
Intent intent = getIntent();
Bundle extras = intent.getExtras();
Additional hint:-
So you need to edit in FirstActivity
btn_Cal21.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(SecondMarks_1.this,FirstResult.class);
Bundle b = new Bundle();
b.putInt("res1",Total21);
b.putString("per1",f21);
i.putExtras(b);
startActivity(i);
finish();
}
});
Same for the following activities.
Still Did not get ? Actually in you FirstActivity you have put
Intent i = new Intent(FirstMarks.this,FirstResult.class);
Bundle b = new Bundle();
b.putInt("res1",Total1);
b.putString("per1",f1);
And in your second activity.
Intent i = new Intent(SecondMarks_1.this,FirstResult.class);
Bundle b = new Bundle();
b.putInt("res1",Total21);
b.putString("per1",f21);
i.putExtras(b);
startActivity(i);
You see you have used per1 string in both places, change that, that is probably causing the app not to get values(possibly give some other string).
And in this part, you need to add code for 2nd activity result to be received.
Bundle b = getIntent().getExtras();
sres1 = b.getInt("res1", 0);
sper1 = b.getString("per1");
Hope that helps.
I have probably invested about 10 hours on this mistake xD still cant find it :/
I have basically 3 layouts. The first layout is for the menu/home, the second is a quiz, and the third is the results screen. The problem right now is that I have no idea how to connect the button from the result screen. (has a button to go back to the menu layout). I dont know where I can add this:
Button haupt = (Button) findViewById(R.id.hauptmeldung);
haupt.setOnclickListener(this); //this refers to the one in the QuizActivty,
abschlusslayout = result screen
activtymain = home/menu layout
quizlayout = quiz
Here are my 3 java files:
MainActivity (QuizActivity):
package at.lorenzdirry.philosophenquiz;
import at.lorenzdirry.philosophenquiz.R.id;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class QuizActivity extends Activity implements android.view.View.OnClickListener {
Spiellogik spiel;
Button startbut1;
View myView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myView = (View)findViewById(id.mainlay);
startbut1= (Button)findViewById(id.startb1);
startbut1.setOnClickListener(this);
spiel = new Spiellogik();
}
public View getTheView(){
return myView;
}
public void onClick(View v) {
int id = v.getId();
if (id == R.id.antwort1)
spiel.auswerten(1, this); // spielAuswerten(1);
else if (id == R.id.antwort2)
spiel.auswerten(2, this); // spielAuswerten(2);
else if (id == R.id.antwort3)
spiel.auswerten(3, this); // spielAuswerten(3);
else if (id == R.id.antwort4)
spiel.auswerten(4, this); // spielAuswerten(4);
else if (id == R.id.startb1)
aufrufen();
else if (id==R.id.hauptmeldung)
Toast.makeText(this, "Leider nichts gewonnen. :-(", Toast.LENGTH_LONG).show();
}
View v;
#Override
public void onBackPressed() {
if(this.findViewById(id.mainlay)==getTheView()){
backButtonHandler();
}
else{
super.onBackPressed();
Intent intent = new Intent(getApplicationContext(), QuizActivity.class);
startActivity(intent);
finish();
}
}
public void backButtonHandler() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
QuizActivity.this);
alertDialog.setTitle("Beenden");
alertDialog.setMessage("Sicher Beenden?");
alertDialog.setPositiveButton("Ja",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialog.setNegativeButton("Nein",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
public void aufrufen(){
setContentView(R.layout.quiz_layout);
for (int n = 1; n <= 4; n++) {
Button btn = null;
switch (n) {
case 1:
btn = (Button) this.findViewById(R.id.antwort1);
btn.setOnClickListener(this);
break;
case 2:
btn = (Button) this.findViewById(R.id.antwort2);
btn.setOnClickListener(this);
break;
case 3:
btn = (Button) this.findViewById(R.id.antwort3);
btn.setOnClickListener(this);
break;
case 4:
btn = (Button) this.findViewById(R.id.antwort4);
btn.setOnClickListener(this);
break;
}
}
spiel.fragen[spiel.aktFrage].anzeigen(this);
}
}
Frage:
package at.lorenzdirry.philosophenquiz;
import at.lorenzdirry.philosophenquiz.R.id;
import android.app.Activity;
import android.widget.Button;
import android.widget.TextView;
class Frage {
private String frage;
private String frage2;
private String frage3;
private String option1;
private String option2;
private String option3;
private String option4;
private int loesung;
public Frage(String f,String f2,String f3, String o1, String o2, String o3, String o4, int l) {
frage = f;
frage2 = f2;
frage3 = f3;
option1 = o1;
option2 = o2;
option3 = o3;
option4 = o4;
loesung = l;
}
public void anzeigen(Activity quizActivity) {
((TextView) quizActivity.findViewById(id.frage)).setText("\u2022 "+frage);
((TextView) quizActivity.findViewById(id.frage2)).setText("\u2022 "+frage2);
((TextView) quizActivity.findViewById(id.frage3)).setText("\u2022 "+frage3);
((Button) quizActivity.findViewById(id.antwort1)).setText(option1);
((Button) quizActivity.findViewById(id.antwort2)).setText(option2);
((Button) quizActivity.findViewById(id.antwort3)).setText(option3);
((Button) quizActivity.findViewById(id.antwort4)).setText(option4);
}
public boolean richtig(int ausgewaehlt) {
if (ausgewaehlt == this.loesung)
return true;
else
return false;
}
}
Spiellogik:
package at.lorenzdirry.philosophenquiz;
import at.lorenzdirry.philosophenquiz.R.id;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
class Spiellogik{
final int ANZAHL_FRAGEN = 8;
Frage[] fragen = new Frage[ANZAHL_FRAGEN];
Random r = new Random();
int aktFrage = r.nextInt(ANZAHL_FRAGEN);
int gewinnstufe = 0;
Spiellogik() {
fragen[0] = new Frage("624-546 v.Chr.","Monismus","\"Alles besteht aus Wasser.\"",
"Thales von Milet", "Aristoteles", "Platon", "Immanuel Kant", 1);
fragen[1] = new Frage("\u2248600 v.Chr.","Daoismus","\"Wer andere kennt, ist klug. Wer sich selbst kennt, ist weise.\"",
"Laotse ", "Alkmaion", "Roger Bacon", "Sokrates", 1);
fragen[2] = new Frage("570-495 v.Chr.","a\u00B2+b\u00B2=c\u00B2","\"Vernunft ist unsterblich, alles andere sterblich.\"",
"Pythagoras", "Thomas von Aquin", "Archytas von Tarent", "Platon", 1);
fragen[3] = new Frage("563-483 v.Chr.","Buddhismus","\"Gl\u00fccklich ist, wer sein ich \u00fcberwunden hat.\"",
"Siddharta Gautama (Buddha)", "Anaximenes", "Antiphon aus Athen", "Kleanthes", 1);
fragen[4] = new Frage("551-479 v.Chr.","Konfuzianismus","\"Mache Treue und Aufrichtigkeit zu obersten Prinzipien.\"",
"Konfuzius", "Melissos", "Damaskios", "Panaitios", 1);
fragen[5] = new Frage("535-475 v.Chr.","Monismus","\"Alles flie\u00dft. (panta rhei)\"",
"Heraklit", "Pyrrhon", "Proklos", "Theodoros", 1);
fragen[6] = new Frage("490-420 v.Chr.","Relativismus","\"Der Mensch ist Ma\u00df aller Dinge.\"",
"Protagoras", "Xenokrates", "Zenon von Sidon", "Jamblichos", 1);
fragen[7] = new Frage("460-370 v.Chr.","Atomismus","\"In Wirklichkeit gibt es nur die Atome und das Leere.\"",
"Leukipp", "Epicharmos", "Thales von Milet", "Philolaos", 1);
}
void auswerten(int schalter, Activity quizActivity) {
if (!fragen[aktFrage].richtig(schalter)) { // falsch beantwortet
if (gewinnstufe == 0) {
Toast.makeText(quizActivity, "Leider nichts gewonnen. :-(", Toast.LENGTH_LONG).show();
}
else {
String str = "Sie haben Gewinnstufe " + gewinnstufe + " erreicht! :-) - Glckwunsch!!!";
Toast.makeText(quizActivity, str, Toast.LENGTH_LONG).show();
}
// Schalter deaktivieren
((Button) quizActivity.findViewById(id.antwort1)).setEnabled(false);
((Button) quizActivity.findViewById(id.antwort2)).setEnabled(false);
((Button) quizActivity.findViewById(id.antwort3)).setEnabled(false);
((Button) quizActivity.findViewById(id.antwort4)).setEnabled(false);
} else {
aktFrage= r.nextInt(ANZAHL_FRAGEN);
if (gewinnstufe < 7) {
fragen[aktFrage].anzeigen(quizActivity);
gewinnstufe++;
((TextView) quizActivity.findViewById(id.richtigeFragen)).setText("Richtig beantwortete Fragen : "+gewinnstufe+"/8");
((ProgressBar) quizActivity.findViewById(id.progressBar1)).setProgress(gewinnstufe);
}
else {
gewinnstufe++;
((TextView) quizActivity.findViewById(id.richtigeFragen)).setText("Richtig beantwortete Fragen : " + gewinnstufe + "/8");
((ProgressBar) quizActivity.findViewById(id.progressBar1)).setProgress(gewinnstufe);
quizActivity.setContentView(R.layout.abschluss_layout);
}
}
}
}
I hope you can help me, pls :)
To return on your Menu activity by using a Button :
Button yourbutton = (Button)findViewById(R.id.yourbutton);
yourbutton.setOnClickListener(new OnClickLIstener(){
#Override
public void onClick(View v){
startActivity(new Intent(this,Menu.class));
}
}
Replace all the vars by yours ;)
This is a source code with switch case code. But the bold part was didn't work. I try to make "next" to be visible after press buttonD, buttonO and buttonG. FYI, "next" is a button. So it use three condition to make one command. How it can be work? Am I put that code in wrong place?
package com.example.fun;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
public class DogActivity extends Activity {
Button buttonA;
Button buttonB;
Button buttonC;
Button buttonD;
Button buttonE;
Button buttonF;
Button buttonG;
Button buttonH;
Button buttonI;
Button buttonJ;
Button buttonK;
Button buttonL;
Button buttonM;
Button buttonN;
Button buttonO;
Button buttonP;
Button buttonQ;
Button buttonR;
Button buttonS;
Button buttonT;
Button buttonU;
Button buttonV;
Button buttonX;
Button buttonW;
Button buttonY;
Button buttonZ;
Button next;
Intent i;
TextView txtD;
TextView txtO;
TextView txtG;
int life=3;
TextView nyaw;
TextView nyaw2;
TextView nyaw3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dog);
buttonA = (Button)findViewById(R.id.buttonAbout);
buttonB = (Button)findViewById(R.id.buttonhelp);
buttonC = (Button)findViewById(R.id.buttonc);
buttonD = (Button)findViewById(R.id.buttond);
buttonE = (Button)findViewById(R.id.buttonE);
buttonF = (Button)findViewById(R.id.buttonf);
buttonG = (Button)findViewById(R.id.buttong);
buttonH = (Button)findViewById(R.id.buttonh);
buttonI = (Button)findViewById(R.id.buttoni);
buttonJ = (Button)findViewById(R.id.buttonj);
buttonK = (Button)findViewById(R.id.buttonk);
buttonL = (Button)findViewById(R.id.buttonl);
buttonM = (Button)findViewById(R.id.buttonm);
buttonN = (Button)findViewById(R.id.buttonn);
buttonO = (Button)findViewById(R.id.buttono);
buttonP = (Button)findViewById(R.id.buttonp);
buttonQ = (Button)findViewById(R.id.buttonq);
buttonR = (Button)findViewById(R.id.buttonr);
buttonS = (Button)findViewById(R.id.buttons);
buttonT = (Button)findViewById(R.id.buttont);
buttonU = (Button)findViewById(R.id.buttonu);
buttonV = (Button)findViewById(R.id.buttonv);
buttonW = (Button)findViewById(R.id.buttonw);
buttonX = (Button)findViewById(R.id.buttonx);
buttonY = (Button)findViewById(R.id.buttony);
buttonZ = (Button)findViewById(R.id.buttonz);
next = (Button)findViewById(R.id.buttonnext);
txtD = (TextView)findViewById(R.id.textViewD);
txtO = (TextView)findViewById(R.id.textViewO);
txtG = (TextView)findViewById(R.id.textViewG);
nyaw = (TextView)findViewById(R.id.nyawa);
nyaw2 = (TextView)findViewById(R.id.nyawa2);
nyaw3 = (TextView)findViewById(R.id.nyawa3);
buttonA.setOnClickListener(myOnlyhandler);
buttonB.setOnClickListener(myOnlyhandler);
buttonC.setOnClickListener(myOnlyhandler);
buttonD.setOnClickListener(myOnlyhandler);
buttonE.setOnClickListener(myOnlyhandler);
buttonF.setOnClickListener(myOnlyhandler);
buttonG.setOnClickListener(myOnlyhandler);
buttonH.setOnClickListener(myOnlyhandler);
buttonI.setOnClickListener(myOnlyhandler);
buttonJ.setOnClickListener(myOnlyhandler);
buttonK.setOnClickListener(myOnlyhandler);
buttonL.setOnClickListener(myOnlyhandler);
buttonM.setOnClickListener(myOnlyhandler);
buttonN.setOnClickListener(myOnlyhandler);
buttonO.setOnClickListener(myOnlyhandler);
buttonP.setOnClickListener(myOnlyhandler);
buttonQ.setOnClickListener(myOnlyhandler);
buttonR.setOnClickListener(myOnlyhandler);
buttonS.setOnClickListener(myOnlyhandler);
buttonT.setOnClickListener(myOnlyhandler);
buttonU.setOnClickListener(myOnlyhandler);
buttonV.setOnClickListener(myOnlyhandler);
buttonW.setOnClickListener(myOnlyhandler);
buttonX.setOnClickListener(myOnlyhandler);
buttonY.setOnClickListener(myOnlyhandler);
buttonZ.setOnClickListener(myOnlyhandler);
next.setOnClickListener(myOnlyhandler);
}
View.OnClickListener myOnlyhandler = new View.OnClickListener() {
public void onClick(View v) {
if( buttonD.getId() == ((Button)v).getId() ){
txtD.setVisibility(View.VISIBLE);
}
else if( buttonO.getId() == ((Button)v).getId() ){
txtO.setVisibility(View.VISIBLE);
}
else if( buttonG.getId() == ((Button)v).getId() ){
txtG.setVisibility(View.VISIBLE);
}
else if( next.getId() == ((Button)v).getId() ){
i = new Intent(DogActivity.this, HouseActivity.class);
startActivity(i);
}
else{
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.no);
mp.start();
life--;
Log.i("Current Life Value is",""+life);
switch (life) {
case 2:
nyaw.setVisibility(View.INVISIBLE);
break;
case 1:
nyaw2.setVisibility(View.INVISIBLE);
break;
case 0:
nyaw3.setVisibility(View.INVISIBLE);
i = new Intent(DogActivity.this, TamatActivity.class);
startActivity(i);
break;
}
}
}
};
**private boolean clickedBtnD;
private boolean clickedBtnO;
private boolean clickedBtnG;
public void onClick(View v) {
final int id = v.getId();
switch(id) {
case R.id.buttond:
clickedBtnD = true;
txtD.setVisibility(View.VISIBLE);
break;
case R.id.buttono:
clickedBtnO = true;
txtO.setVisibility(View.VISIBLE);
break;
case R.id.buttong:
clickedBtnG = true;
txtG.setVisibility(View.VISIBLE);
break;
case R.id.buttonnext:
if(clickedBtnD && clickedBtnO && clickedBtnG) {
next.setVisibility(View.VISIBLE);
}
break;
}
}**
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dog, menu);
return false;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
i = new Intent(DogActivity.this, LevelActivity.class);
startActivity(i);
}
return super.onKeyDown(keyCode, event);
}
}
you are missing to implement onClicklistenor from your Activity and #Override before your onclick method like in code only one button is here ..
Try out this code
package com.example.browsebutton;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id=v.getId();
switch(id) {
case R.id.button1:
//CODE
break;
}
}
}
I Have an activty in my app which uses putextra(int) method to pass the value 0 first when the aactivty is started. Then on pressing a next button it passes 6,Subsequently 12 and so on. But the trouble is on pressing the next second time I found that the value of index received using getextras method is 0.Is it cause i am calling the activity from itself . This is the code snippet:
package com.movie;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
public class movielist extends Activity implements OnClickListener{
Button bm[] = new Button[6];
Button nxt,prev;
String namesdb[]=new String[50];
databaseconnect db;
String lang;
int start,index,end;
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.mallu_list);
db = new databaseconnect(this);
db.open();
start=this.getIntent().getExtras().getInt("index");
lang =this.getIntent().getExtras().getString("lang");
Toast.makeText(this, start+"", Toast.LENGTH_SHORT).show();
Log.i("info",start+"");
if(!db.isdata(lang))
{
Toast.makeText(this, "No data in Database", Toast.LENGTH_SHORT).show();
finish();
return;
}
end=start+6;
bm[0] = (Button) findViewById(R.id.bm1);
bm[1] = (Button) findViewById(R.id.bm2);
bm[2] = (Button) findViewById(R.id.bm3);
bm[3] = (Button) findViewById(R.id.bm4);
bm[4] = (Button) findViewById(R.id.bm5);
bm[5] = (Button) findViewById(R.id.bm6);
nxt = (Button) findViewById(R.id.nxt);
prev = (Button) findViewById(R.id.prev);
if(start==0)
{
// prev.setBackgroundResource(0);
// prev.setText("");
}
else
prev.setOnClickListener(this);
namesdb = db.getmovie("",lang);
for (int i = 0; start+i < namesdb.length && i<6; i++) {
bm[i].setText(namesdb[start+i]);
bm[i].setOnClickListener(this);
}
int flag=0;
int i=namesdb.length;
Log.i("info",i+"");
Toast.makeText(this, i+"", Toast.LENGTH_SHORT).show();
for(int j=5;j>=i-start;j--)
{
bm[j].setBackgroundResource(0);
flag=1;
}
if(flag==1)
{
// nxt.setBackgroundResource(0);
// nxt.setText("");
}
else
nxt.setOnClickListener(this);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Intent myint2 = new Intent(this, list.class);
startActivity(myint2);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bm1:
call(namesdb[start],start,lang);
break;
case R.id.bm2:
call(namesdb[start+1],start,lang);
break;
case R.id.bm3:
call(namesdb[start+2],start,lang);
break;
case R.id.bm4:
call(namesdb[start+3],start,lang);
break;
case R.id.bm5:
call(namesdb[start+4],start,lang);
break;
case R.id.bm6:
call(namesdb[start+5],start,lang);
break;
case R.id.nxt:
int i=namesdb.length;
Intent myint2 = new Intent(this,movielist.class);
myint2.putExtra("index",end);
myint2.putExtra("lang",lang);
startActivity(myint2);
case R.id.prev:
if(start==0)
{
prev.setBackgroundResource(0);
}
else
{
myint2 = new Intent(this,movielist.class);
myint2.putExtra("index",start-6);
myint2.putExtra("lang",lang);
startActivity(myint2);}
break;
}
}
public void call(String name,int start2,String lang) {
Intent myint = new Intent(this, detail.class);
myint.putExtra("nameid", name);
myint.putExtra("index", start2);
myint.putExtra("lang", lang);
startActivity(myint);
}
}
Here's your problem. This part of the switch statement:
case R.id.nxt:
int i=namesdb.length;
Intent myint2 = new Intent(this,movielist.class);
myint2.putExtra("index",end);
myint2.putExtra("lang",lang);
startActivity(myint2);
is missing a break at the end. So it falls through to the next case, which starts the activity with different extras.
In main.xml I made a row containing a TextView, an EditText and a "+" and "-" button.
Underneath that I made an "Add" button that will help you create a new row When you click the add button, you get an EditText and a Submit and Cancel button.
On "Submit" it outputs the EditText value to the TextView and creates the same row as the first one.
The numeric value "NewValueBox" should +1 when the "+" button is pressed.
But because I call it in another function it is not recognized by createNewAddButton() function in which the button is set up.
So in short:
"How do I change the value of NewValueBox when I click NewAddButton?"
Here's the code:
package com.lars.MyApp;
import com.google.ads.*;
import com.lars.MyApp.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
public class DrinkRecOrderActivity extends Activity {
int currentValue1 = 0;
int currentValueNew = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText firstValue = (EditText) findViewById(R.id.firstValue);
Button valuePlus = (Button) findViewById(R.id.valuePlus);
Button valueMinus = (Button) findViewById(R.id.valueMinus);
final Button addValue = (Button) findViewById(R.id.add);
final TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);
final LinearLayout addValueRow = (LinearLayout) findViewById(R.id.addValueRow);
final EditText addNewValue = (EditText) findViewById(R.id.addNewValue);
final Button submitNewValue = (Button) findViewById(R.id.submitNewValue);
final Button cancelNewValue = (Button) findViewById(R.id.cancelNewValue);
// BEGIN ONCLICKLISTENERS
valuePlus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
plusValue();
firstValue.setText("" + currentValue1);
}
});
valueMinus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
minValue();
firstValue.setText("" + currentValue1);
}
});
addValue.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addValueRow.setVisibility(View.VISIBLE);
addValue.setVisibility(View.GONE);
}
});
cancelNewValue.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addValueRow.setVisibility(View.GONE);
addValue.setVisibility(View.VISIBLE);
}
});
submitNewValue.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tableLayout1.addView(createnewRow());
addValueRow.setVisibility(View.GONE);
addValue.setVisibility(View.VISIBLE);
addNewValue.setText("");
}
});
// END ONCLICKLISTENERS
// Look up the AdView as a resource and load a request.
AdView adView = (AdView) this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
}
public TableRow createNewRow() {
final TableRow newRow = new TableRow(this);
final EditText addNewValue = (EditText) findViewById(R.id.addNewValue);
newRow.addView(createNewTextView(addNewValue.getText().toString()));
newRow.addView(createNewValueBox());
newRow.addView(createNewAddButton());
newRow.addView(createNewMinusButton());
return newRow;
}
public TextView createNewTextView(String text) {
final TextView textView = new TextView(this);
textView.setText(text);
return textView;
}
public EditText createNewValueBox() {
EditText NewValueBox = new EditText(this);
NewValueBox.setHint("0");
NewValueBox.setInputType(InputType.TYPE_CLASS_NUMBER);
return NewValueBox;
}
public Button createNewAddButton() {
final Button NewAddButton = new Button(this);
NewAddButton.setText("+");
NewAddButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
plusNew();
//NewValueBox.setText("" + currentValueNew);
}
});
return NewAddButton;
}
public Button createNewMinusButton() {
final Button NewMinusButton = new Button(this);
NewMinusButton.setText("-");
return NewMinusButton;
}
// BEGIN PLUS AND MIN FUNCTIONS
public void plusNew() {
if (currentValueNew <= 999) {
currentValueNew = currentValueNew + 1;
}
}
public void plusValue() {
if (currentValue1 <= 999) {
currentValue1 = currentValue1 + 1;
}
}
public void minValue() {
if (currentValue1 >= 1) {
currentValue1 = currentValue1 - 1;
}
}
// END PLUS AND MIN FUNCTIONS
}
Add IDs for your Views so you can later reference them. Make 3 private static int field in your activity(the ID for NewValueBox, NewAddButton and NewMinusButton):
private static int edt = 1;
private static int add = 1001;
private static int minus = 2001;
Then in your createNewValueBox() method set the ID:
NewValueBox.setId(edt);
edt++;
Do the same for the NewAddButton and the NewMinusButton:
NewAddButton.setId(add);
add++;
NewMinusButton.setId(minus);
minus++;
Then in your listener for the buttons find out exactly which add button has been clicked and set the text in the corresponding EditText:
NewAddButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
plusNew();
int tmp = v.getId();
EditText temp = (EditText) findViewById(1 + (tmp - 1001));
temp.setText("" + currentValueNew);
}
Kind of hackish method.