So I'm developing currently my own private App which I want to use only for me and maybe some friends.
Well I'm german so my english is maybe not the best I hope you can forgive me.
Now my Problem is that I want to set in my Optionsmenu a Budget for the current month to keep track of. I'm doing that by using an EditText with a Button.
Now I want to save this String which is getting entered in my EditText to a String value and a Integer value because I want to show the Value in a TextView on my MainPage and use the Integer value to calculate my current budget I got and so on.
Now I'm showing you guys my code and I hope u can tell me whats wrong with it.
I'm trying to get my Value in the Options class which is related to my OptionsMenu and later Trying to get the Value out of my Options class into my Main class.
public class Options extends Activity {
Button GoBack, SetBudget;
private int Budget;
String BudgetString = "";
EditText BudgetEdit;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
GoBack = (Button) findViewById(R.id.button2);
GoBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent GoBackToMain = new Intent("com.lunarrepublic.STARTINGPOINT");
startActivity(GoBackToMain);
}
});
SetBudget = (Button) findViewById(R.id.button8);
SetBudget.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
BudgetEdit = (EditText) findViewById(R.id.editText1);
BudgetString = EditText.getText();
//In the Line above this is the error "Type mismatch. Cannot convert Editable to String
Budget = Integer.parseInt(BudgetString);
}
});
}
But if I try to set my "BudgetString" to Editable It won't work either.
The GoBack Button is unnecessary for my problem here so you don't have to read over it.
So I hope you guys understood what my problem is and can maybe help me getting it fixed
Thanks in advance
This is wrong
BudgetString = EditText.getText();
Use below one
BudgetString = BudgetEdit.getText().toString();
change this
SetBudget.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
BudgetEdit = (EditText) findViewById(R.id.editText1);
BudgetString = EditText.getText();
//In the Line above this is the error "Type mismatch. Cannot convert Editable to String
Budget = Integer.parseInt(BudgetString);
}
});
to
SetBudget.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
BudgetEdit = (EditText) Options.this.findViewById(R.id.editText1);
BudgetString = BudgetEdit.getText();
//In the Line above this is the error "Type mismatch. Cannot convert Editable to String
Budget = Integer.parseInt(BudgetString);
}
});
Related
I am trying to make a simple android application which contains a EditText and a Button . User will enter his data and this application will show him the data as a Toast.
Now The problem I am facing is , whenever button is pressed by user it's just showing a blank toast.Here is my code
EditText et;
String data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bsave=(Button) findViewById(R.id.bsave);
et=(EditText) findViewById(R.id.editText1);
data=et.getText().toString();
bsave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.v("EditText",et.getText().toString());
Intent in=new Intent(MainActivity.this,SecondAct.class);
in.putExtra("DATA", data);
Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
}
});
}
please help me . Thanks in Advance!!!!
You are calling gettext() in oncreate(),when there is no text at all in editbox.. So your log inside onClick() will show data but toast will never show any thing with this code as variable data is ""..
You should do like
bsave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
data=et.getText().toString();
Toast.makeText(MainActivity.this,data, Toast.LENGTH_SHORT).show();
}
});
in button onClick(...)
As shown above you need to save the data in the EditText inside of the Button's on click listener. As your code is now as soon as the edit text is created the text is being saved into the data variable, but you want it to set as soon as the button is clicked.
I am trying to create a simple grade tracker for android. The way I have it set up is that the user has a list of classes that are in their major. When they click on the title of the class, a new activity starts that has the name of the class, the catalog description, a space to place your grade, and a button to return to the list of classes.
What I would like to do is save the grade number that they input on the second page, and when the button is pressed, on the first page the grade is shown next to the course title.
I have edited to the code to reflect the comments given.
This is the code that works!
CoreClasses
TextView eng101, eng101scr;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.classcore);
eng101 = (TextView)findViewById(R.id.eng101);
eng101scr = (TextView)findViewById(R.id.eng101CoreScr);
eng101.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(ClassCore.this, Eng101.class);
i.putExtra("grades", "eng101" );
startActivityForResult(i, 1);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(data.getExtras().containsKey("e101FinalScore")){
eng101scr.setText(data.getStringExtra("e101FinalScore"));
}
Eng101
public class Eng101 extends Activity {
Button btnSubmit;
EditText userGrade;
String strGrade;
OutsideVariables outside = new OutsideVariables();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.eng101);
btnSubmit = (Button)findViewById(R.id.btnE101);
userGrade = (EditText)findViewById(R.id.eng101Scr);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
strGrade = userGrade.getText().toString();
Intent i = getIntent();
String msg = i.getStringExtra("grades");
if(msg.contentEquals("eng101")){
i.putExtra("e101FinalScore", strGrade);
setResult(RESULT_OK, i);
finish();
}
}
});
}
}
You can pass the values to the previous activity by,
Intent intent = new Intent(Eng101.this, ClassCore.class);
intent.putExtra("grade",grade);
startActivity(new Intent(Eng101.this, ClassCore.class));
and in ClassCore you can get the value grade by,
Bundle extras = getIntent().getExtras();
// get data via the key
if(extras!=null){
int value1 = intent.getIntExtra("grade", 0);
if (value1 != null) {
eng101scr.setText(value1);
}
}
Because you're using an instance, each new instance will have it's own storage and values. What you're looking to do is actually much simpler than this, and it's to do everything using static access.
However, the better way to do this would be to create some sort of class like your OutsideVariables and pass an instance into your new activity, that way you're able to access them in both. Take a look at this other post for some more information. How to pass the values from one activity to previous activity
I use this code to get a number from an EditText and pass it into a Count Down Timer
final int mytime;
mytime = Integer.parseInt(textIn.getText().toString());
btnStartTimer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startCountDownTimer(mytime);
}
});
However the app crashed and I get a:
java.lang.NumberFormatException: Invalid int:
However when I write it like this it works:
btnStartTimer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startCountDownTimer(Integer.parseInt(textIn.getText().toString()));
}
});
Can please someone explain me difference ? Can I use the first code somehow ?
I really don't understand the problem
In you first example the value of the text has not been set and will be null. You want to do the action after the use has filled in the form and clicked on the button don't you.
I am trying to use an EditText one one Activity to change the text of a button on another. I know I have to go through the SharedPreferences, and although this is where I am stuck.
Activity with the Button:
protected void onResume() {
super.onResume();
class1.setText(this.getButtonText());
}
public String getButtonText()
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String buttonText = prefs.getString("ButtonText", "Default button text"); // I am not sure how to get the button text here. This is what someone was trying to have me do?
return buttonText;
}
This is my Activity that has the EditText and the button to go back to the activity with the button:
public class EditClass1 extends Activity implements OnClickListener{
Button class1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editclass1);
SettingButtons();
class1.setOnClickListener(this);
}
private void SettingButtons() {
// TODO Auto-generated method stub
class1 = (Button) findViewById(R.id.edittoclass1);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.edittoclass1:
startActivity(new Intent("com.clayton.calendar.TOCLASS"));
break;
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit();
editor.putString("ButtonText", // This is not working
((TextView)findViewById(R.id.edittoclass1)).getText().toString());
editor.commit();
}
}
Try this:
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
EditText text = (EditText)findViewById(R.id.Setclass);
String text2 = text;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit();
editor.putString("ButtonText", // This is not working
((TextView)findViewById(R.id.edittoclass1)).getText().toString());
editor.commit();
}
Ignoring the shares preferences for a moment, why not just have a public static String variable in the class containing the button.
public static String buttonText = "somthing";
When in the class containing the edit text you can call in an event handler which listens for changes to the edit text or an event handler that is fired when a button is pressed.
ButtonActivity.buttonText = text.getText();
Then in the onResume() method of the activity containing the button
button.setText(buttonText);
Try this it might be a simpler way of doing what you want. Remember when declaring the buttonText variable make sure you remember to use the static keyword. Without it you will need a direct reference to the object, with the static keyword, you can just refer to the required class. However, being static button Text will be the same for all instances of the button containing activity. If you only ever intend on having one instance of the activity this is the solution for you. If not then you have to get a little more creative.
ok first of all sorry if its a stupid question, but I have ADHD and my head is going to explode after reading a lot of useless details, I just want a small, clear example and I'll predict the rest!
Basically I am playing with the Android SDK, and thought its similar to Adobe Flex, and everything went smooth until I tried listening to a Button click.
in AS3:
private function onAddedToStage(event:Event):void
{
myButton.addEventListener(MouseEvent.CLICK,handleClick);
}
private function handleClick(event:MouseEvent):void
{
trace("You clicked: " + event.currentTarget.name);
}
what I've done so far with trial and error:
private Button _okButton;
private EditText _name;
private View _view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("trace", "it works!");
_view = getCurrentFocus();
_name = (EditText) _view.findViewById(R.id.editText1);
_okButton = (Button) _view.findViewById(R.id.button1);
_name.setText("Yupee I found it!");
OnClickListener l = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("trace", "Button clicked");
}
};
_okButton.setOnClickListener(l);
}
The code above crashes the application
Thanks in advance.
replace _view by this.