I am trying to make a button send data from a number input from one activity to another and display that number. When I click the add button it does not change the value and it displays as null.
//EXTRA_NUMBER is initalized under class header as
//public static final String EXTRA_NUMBER = "com.example.operationhydration.EXTRA_NUMBER";
public void addWater()
{
EditText editText = (EditText) findViewById(R.id.water_input);
double number = Double.parseDouble(editText.getText().toString());
Intent intent = new Intent(this, Progress.class);
intent.putExtra(EXTRA_NUMBER,number);
}
//Code in class I am trying to send it to
Intent intent = getIntent();
String number = intent.getStringExtra(AddWater.EXTRA_NUMBER);
TextView textGoal = (TextView) findViewById(R.id.goal_text);
textGoal.setText("" + number);
EDIT:
I got the variable to display but how do I make it stay if I change pages and come back.
Related
hello there i'm a new beginner in android development. I tried several times to figure out my issue but i couldn't please help me.I created a simple login screen once the user enters the username and password I simply want to display it in the next activity. I used putExtra and getExtra but the values are null all the time.
This is my code
Intent i = new Intent(Data1.this , Data2.class);
i.putExtra("uname",username.getText().toString());
i.putExtra("pass",password.getText().toString());
Log.d("username",username.getText().toString());
Log.d("password",password.getText().toString());
startActivity(i);
data = (TextView)findViewById(R.id.txt);
Intent i = this.getIntent();
u = i.getStringExtra("username");
p = i.getStringExtra("password");
data.setText(u+" Successfully logged in User Name - "+ u + " Password - "+ p);
You've Passed the Data to the next Intent Correctly using PutExtra but in the new intent when you've used getStringExtra you've used another String variable which is completely different from what you've passed.Try this example it should work.You should use i.getStringExtra("uname"); and i.getStringExtra("pass"); instead of what you have passed.
public class Data1 extends AppCompatActivity {
EditText username;
EditText password;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data1);
username = (EditText)findViewById(R.id.edusername);
password = (EditText)findViewById(R.id.edpassword);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Data1.this , Data2.class);
i.putExtra("uname",username.getText().toString());
i.putExtra("pass",password.getText().toString());
Log.d("username",username.getText().toString());
Log.d("password",password.getText().toString());
startActivity(i);
}
});
}
}
public class Data2 extends AppCompatActivity {
TextView data;
String u;
String p;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data2);
data = (TextView)findViewById(R.id.txt);
// Intent i = getIntent();
Intent i = this.getIntent();
u = i.getStringExtra("uname");
p = i.getStringExtra("pass");
data.setText(u+" Successfully logged in User Name - "+ u + " Password - "+ p);
}
}
You a passing the value using
i.putExtra("uname",username.getText().toString());
i.putExtra("pass",password.getText().toString());
and getting value using
u = i.getStringExtra("username");
p = i.getStringExtra("password");
here you must be use same key as you passed in intent so you have to use "uname" instead of "username" and "pass" instead of "password"..
example :
FirstActivity.java
i.putExtra("key", "value");
secondActivity.java
i.getStringExtra("key");
You are getting null values because strings are not same in put and get method.And values are always stored in same key.So change your code in get method like as-
Intent i = this.getIntent();
u = i.getStringExtra("uname");
p = i.getStringExtra("pass");`
I'm sending a string from one Activity to another Activity with putExtras and getExtras.
Then I displaying this string to a TextView. When I go back and choose another string it ovewrites the previous string in TextView.
I want to place the new string in a new line in textView.
How to do this?
(I'm sending strings with a button)
This is my code in the recieve Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_completed_tasks);
TextView completedTasksView = (TextView)findViewById(R.id.completed_tasks);
Intent intent = getIntent();
String completedTasks = intent.getExtras().getString("completedTasks");
completedTasksView.setText(completedTasks);
}
}
According to: TextView
append(CharSequence text)
Convenience method: Append the specified
text to the TextView's display buffer, upgrading it to
BufferType.EDITABLE if it was not already editable.
So use:
completedTasksView.append("\n" + completedTasks);
You have to store the string that you sent previously using putExtras because next time activity will be recreated again and your previous data will be lost. To save previous String you can create a static arraylist and on getIntent add your string to arraylist and when you want to print use a for loop and create a single string with appended "/n" and set that string to textview like this
For eg.
public static ArrayList<String> list;
To add string to list
list.add(completedTasks);
To create a single string from arraylist
String text="";
for(int i=0;i<list.size();i++)
{
if(text!="")
{
text=text+"\n"+list(i);
}else{
text=list(i);
}
}
textview.setText(text);
NOTE:
Arraylist should be static for app and not within activity so define arraylist in different class then activity
you can create One globle class with one String variable which will store your completedTask Text every time set text when you get new string ...
Create one global String and always append to it:
String completedTasks = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_completed_tasks);
TextView completedTasksView = (TextView)findViewById(R.id.completed_tasks);
Intent intent = getIntent();
completedTasks += "\n" + intent.getExtras().getString("completedTasks");
completedTasksView.setText(completedTasks);
}
}
I'm trying to get the user information typed in the edit text. I want it saved into the Intent result variable. Trying to sending it back to the main activity afterwards. Keep getting the cannot resolve method. I'm thinking it must be that I'm missing a parameter in the putExtra() method
public class EnterDataActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enterdata);
Button doneButton = (Button) findViewById(R.id.button_done);
final EditText getData = (EditText) findViewById(R.id.enter_data_here);
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent result = new Intent();
result.putExtra(getData);
setResult(RESULT_OK, result);
finish(); // Ends sub-activity
}//ends onClick
});
}//ends onCreate void button
}
Well if you are trying to send the EditText that's not possible.
If you are trying to send the text that are on that EditText that's possible.
How to do it?
Declare a String to save the data (You can avoid this step, but that's more clear)
String mText = getData.getText().toString();
Then you'll use the getExtra() method to send the String to the new Activity
Intent i = new Intent(this, MyNewActivity.class);
i.putExtra("text_from_editText", mText);
startActivity(i);
Then the last step (You don't ask for it, but you'll need it), get the text.
//onCreate() of the second Activity
Intent i = getIntent();
String mText = i.getStringExtra("text_from_editText");
Replace result.putExtra(getData); with result.putExtra(getData.getText().toString()); to get the text from the EditText. Right now you're trying to put the entire EditText object into the intent as an extra instead of just the text from the EditText.
How to open another activity on button click? So now my app has one main window ( when you run the app ). Then there is a menu whit some buttons and when I click on some button is open another activity for this button.
So I have one form which user must enter his details but now I want to click on button continue and open another activity where he must enter some more info. Now when this button is clicked the info goes into database.
I know how to add more activities on normal page like main where I have only buttons but on this one I don't really know. Here is the code
public class Reservation extends Activity {
String Name;
String Email;
String Phone;
String Comment;
String DateTime;
String numberOfPeople;
private EditText editText1, editText3, editText2, editText4, txtDate, numberOfPeoples; //, txtTime;
private Button btnMenues, btnCalendar, btnTimePicker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reservation);
editText1 = (EditText) findViewById(R.id.personName);
editText3 = (EditText) findViewById(R.id.personPhone);
editText2 = (EditText) findViewById(R.id.personEmail);
editText4 = (EditText) findViewById(R.id.personComment);
txtDate = (EditText) findViewById(R.id.datePicker);
numberOfPeoples = (EditText) findViewById(R.id.numberOfPeoples);
btnCalendar = (Button) findViewById(R.id.btnCalendar);
//btnTimePicker = (Button) findViewById(R.id.btnTimePicker);
btnMenues = (Button) findViewById(R.id.continueWithReservation);
btnMenues.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Reservation.this, ReservationSecond.class);
intent.putExtra(Name, Name.getBytes().toString());
intent.putExtra(Email, Email.getBytes().toString());
intent.putExtra(Phone, Phone.getBytes().toString());
intent.putExtra(Comment, Comment.getBytes().toString());
intent.putExtra(DateTime, DateTime.getBytes().toString());
intent.putExtra(numberOfPeople, numberOfPeople.getBytes().toString());
startActivity(intent);
}
});
}
So what I can put in setOnClickListener to go on next activity?
Update:
If I change this
public void onClick(View v) {
Name = editText1.getText().toString();
Phone = editText3.getText().toString();
Email = editText2.getText().toString();
Comment = editText4.getText().toString();
DateTime = txtDate.getText().toString();
numberOfPeople = numberOfPeoples.getText().toString();
new SummaryAsyncTask().execute((Void) null);
}
to this
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Contacts.class);
startActivity(intent);
}
How to keep the information that user is added so far for next activity and save in DB after he finish with second activity?
You can use an Intent and then call startActivity() with that Intent:
myBtn.setOnClickListener() {
public void onClick() {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
Android Docs give a pretty good explanation
EDIT
To address the question you gave in the comment, the easiest way to pass information between Activities is to use extras like:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(NAME, VALUE);
startActivity(intent);
There are multiple versions of putExtra to accomodate passing different types of values
Another way to store information would be to use SharedPreferences. Here is a simple example
You could also create a public class called, for example, Storage and have your data be represented as static member(s) of this class (accessed like Storage.MY_DATA) so it can be accessed from any point in your application.
This is probably quite basic but I've only needed a use for a feature like this now. I have a button that carries out a calculation when I click on it. The output is a number. I want to put that number into a TextView on a different layout. On it's own page basically.
I can already get what I want on the same page. Just do the whole
TextView.setText();
Can anyone help me put the data onto it's own page? So when I click the button it carries out the calculation AND opens this new page to put the answer on it?
I tried putting the TextView in a new layout file and calling it via a findViewById but that gave me a force close.
Any solutions? Thanks.
EDIT: Here's the code, I'm trying to display the time on another page. See below
public void getWakeUpTime (View v) {
LocalTime localtime = new LocalTime();
LocalTime dt = new LocalTime(localtime.getHourOfDay(), localtime.getMinuteOfHour());
LocalTime twoHoursLater = dt.plusHours(2);
DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm");
Text1.setText("Time: " + twoHoursLater.toString(formatter));
}
Right now it displays on the same page under the TextView Text1.
You can pass integer to next activity using this code:
String num;
Intent i = new Intent(this, NextActivity.class);
i.putExtra("value", num);
startActivity(i);
You can retrieve the data on next activity as shown below:
Intent i = getIntent();
String num = i.getStringExtra("value");
textview.setText("Number is: "+num);
So brother, here you want is that you have calculated a value in 1st page and you want to show it on the 2nd page am I right?
For this brother you need to pass some data(i.e. Bundle) while calling the next activity.
Here's a similar Application that I built few months back. It passes the message created in one activity to another. Hope this works for you.
The following code is from the first activity where message is created and it is bundled and sent to the next activity.
public void yourMethod(View view ){
// Fetching the message to be sent to the next activity.
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
Intent intent = new Intent(this, DisplayMessageActivity.class);
// Remember this constant(or any string you can give). to be used on other activity.
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
And this is what I did on the onCreate Method of the Activity which was called by the previous Activity.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
// save the passed message as string so that it can be displayed anywhere in this activity.
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
Hope My Program give you enough light to your solution. Hope that satisfies you.
Thanks brother.
You need to send the data as an Extra. Starting Another Activity has all the information you need.