How to put EditText info into a Intent variable - android

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.

Related

Pass updated data to home activity without refreshing any activity

Here I have 3 activity and ActivityA contain textView which show username , ActivityB contain only button to redirect ActivityC, ActivityC Contain EditText and save button.
Now i want to change updated text from ActivityC to direct ActivityA when save button clicked without refreshing any Activity than what i have to do? can you please suggest simple way.
I have a same requirement in complex project, i need to save user location at server side using api call and show updated data in ActivityA.
Another way would be to use LocalBroadcastManager.
The code will be as follows.
//ACTIVITY A CODE
TextView username_tv;
String username;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
username_tv = (TextView) findViewById(R.id.username_tv);
//Get your username and store it in username
username = getYourUsername();
username_tv.setText(username);
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
lbm.registerReceiver(receiver, new IntentFilter("USER_NAME_CHANGED_ACTION"));
}
public BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
username = intent.getStringExtra("username");
username_tv.setText(username);
}
}
};
//ACTIVITY C CODE
//ADD this code to your 'SAVE' Button Listener
Intent intent = new Intent("USER_NAME_CHANGED_ACTION");
intent.putExtra("username", editText.getText().toString());
// put your all data using put extra
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
For more information on localBroadcastManager look here.
I do not understand why you need this, but you can do it by saving the value of EditText in ActivityC in SparedPreferences here is an example how can make it. Then you should check in onResume() method of ActivityAif there is saved value in SparedPreferences and if has some get the value and set it to "Username" TextView.
EventBus will solve your problem
http://greenrobot.org/eventbus/

Using intent to pass spinner objects to a new activity via a button

On the first activity I have a spinner and a button. I want to make it so that when a spinner option is chosen and then the button is clicked, a new activity is opened and the spinner option is displayed (along with additional information about it) in a TextView. In short, I'm trying to use intent to send and retrieve data, and then add additional information afterwards, which I think I would use putExtra() method.
Here I have the button directing to the second page and I am trying to store the spinner's object via intent.
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button yourButton = (Button) findViewById(R.id.done);
yourButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(MainActivity.this, Selected.class);
intent.putExtra("new_variable_name","value");
startActivity(intent);
}
});
Here is Selected.class (second activity)
I want to retrieve the spinner info and put it into a textview.
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
}
Also, here is the spinner array named "person_array":
<string-array name="person_array">
<item>Nick</item>
<item>Isaac</item>
<item>Sally</item>
<item>Matt</item>
<item>Tim</item>
</string-array>
problem in your code is that you are creating two different intent object. The former filled up with the string:
Intent i = new Intent(getApplicationContext(), Spinner.class);
i.putExtra("new_variable_name","value");
and you use the latter to star the Selected activity.
startActivity(new Intent(MainActivity.this, Selected.class));
You should use only one
Intent intent = new Intent(MainActivity.this, Selected.class);
intent.putExtra("new_variable_name","value");
startActivity(intent);
In your first code snippet, you create an Intent (i), pointing to something that is not an Android application component (Spinner), put an extra on it, and then throw it away. This will not be useful.
Instead, put your extra on the Intent that you are using with startActivity():
startActivity(new Intent(MainActivity.this, Selected.class)
.putExtra("new_variable_name","value"));

Open new class/activity on button click in android

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.

Activity That has Several Extras from Other Activities?

I have an Activity that uses the following code to retrieve information from another activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
int tok = extras.getInt("Token");
tempToken += tok;
}
This is the Code inside the first other class that sends this information:
final Button mainMen = (Button) findViewById(R.id.toMainMenu);
mainMen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
Menu.class);
i.putExtra("Token", tok + teTok);
startActivity(i);
}
});
Now i have another Activity that also wants to sen information to the Main Activity like so:
maMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Campaign.this, Menu.class);
intent.putExtra("Token", player.tokens);
intent.putExtra("Round", player.round);
intent.putExtra("Rank", player.rank);
intent.putExtra("Score", player.score);
intent.putExtra("Sec", player.secondsTapped);
intent.putExtra("Min", player.minutesTapped);
intent.putExtra("Hour", player.hoursTapped);
intent.putExtra("Day", player.daysTapped);
intent.putExtra("LifeTap", player.tapsInLife);
intent.putExtra("SecTap", player.tapsPerSec);
intent.putExtra("TapRo", player.tapps);
startActivity(intent);
}
});
Now my question is, how do i handle these different extras from multiple Activities inside the one Main Activity?
Thank You for your time :)
There are two ways to solve your problem..
1)
You can pass one boolean value to or and int variable with some value.. And retrieve this in your new Activity and check with boolean value or int value and get correct data correspond to Activity.
2) You can save your all Data in Shared Preference. And get your all Data in any Activity.
you can send one boolean value that data is in first class or second class and in MainActivity check the value and get the correct data

Passing value from one window to the another in android

I want to show the value inserted by user in first window to the next window.
I am accepting the User weight & height in first window and I want to show it on the second screen as Your weight & Height.
I search a lot and even tried a code but in emulator m getting forcefully closed error.
First Activity :
public class BMI_Main extends Activity
{
EditText BMI_weight;
public String weight;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi_main);
Button submit =(Button)findViewById(R.id.BMI_submit);
BMI_weight = (EditText)findViewById(R.id.BMI_EdTx_kg);
submit.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
weight = BMI_weight.getText().toString();
// create a bundle
Bundle bundle = new Bundle();
// add data to bundle
bundle.putString("wt", weight);
// add bundle to the intent
Intent intent = new Intent(v.getContext(), BMI_Result.class);
intent.putExtras(bundle);
startActivityForResult(intent, 0);
}
}
);
Second Activity :
public class BMI_Result extends Activity
{
TextView Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi_result);
//get the bundle
Bundle bundle = getIntent().getExtras();
// extract the data
String weight = bundle.getString("wt");
Weight.setText(weight);
}
So please help me for it..
As far as I can see you have the following member definition in BMI_Result:
TextView Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
But you can only call findViewById after the class was initialized, since it is a member function of the View class, so change this line to:
TextView Weight;
And add this line to the onCreate method right after setContentView(...):
Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
Edit: It said "...right after super.onCreate(...)", now it's correct ;)
You should override onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
And the token at the end of onCreate is wrong.
You should use the Context.startActivity(Intent intent) method in your first window/Activity.
Store your data which you want to pass to the second window/Activity in the Intent object, like:
Intent intent = new Intent();
intent.putExtra("weight", weight);
intent.putExtra("height", height);
this.startActivity(intent);
And retrieve them in the second screen/Activity in the onCreate method, like:
Intent intent = getIntent(); // This is the intent you previously created.
int weight = intent.getExtra("weight");
int height = intent.getExtra("height");

Categories

Resources