Main Activity
`public class MainActivity extends AppCompatActivity {
private Object Context;
public static final String MSG="com.mp.exampleapp.ORDER";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void createBirthdayCard(View view) {
EditText text = (EditText)findViewById(R.id.nameInput);
String str = text.getText().toString();
Toast.makeText(this, "Button was clicked "+str,Toast.LENGTH_LONG).show();
getIntent().putExtra(MSG,str);
startActivity(new Intent(this,birthdaygreeting.class)) ;
ImageView image=(ImageView) findViewById(R.id.cake);
image.setImageResource(R.drawable.wish);
}
`Second activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_birthdaygreeting);
Intent intent = getIntent();
String msg = "HAPPY BIRTHDAY "+intent.getStringExtra(MainActivity.MSG);
// receive the value by getStringExtra() method
// and key must be same which is send by first activity
TextView textView= (TextView)findViewById(R.id.textView2);
textView.setText(msg);
}
After running this program - after clicking the button- the message that's getting displayed is "HAPPY BIRTHDAY null". I don't know what's going wrong.
What is wrong?
The place where you are passing the put extra, is in getIntent(). This means that you are giving an intent extra to the previous activity.In this case, when you go back, and then get that extra, you will get it.But not on the next activity. Now you get null because that extra does not exist for that activity. So, it comes null.
How to solve?
Instead of using this
public void createBirthdayCard(View view) {
EditText text = (EditText)findViewById(R.id.nameInput);
String str = text.getText().toString();
Toast.makeText(this, "Button was clicked "+str,Toast.LENGTH_LONG).show();
getIntent().putExtra(MSG,str);
startActivity(new Intent(this,birthdaygreeting.class)) ;
ImageView image=(ImageView) findViewById(R.id.cake);
image.setImageResource(R.drawable.wish);
}
use this
public void createBirthdayCard(View view) {
EditText text = (EditText)findViewById(R.id.nameInput);
String str = text.getText().toString();
Toast.makeText(this, "Button was clicked "+str,Toast.LENGTH_LONG).show();
startActivity(new Intent(this,birthdaygreeting.class).putExtra(MSG,str)) ;
ImageView image=(ImageView) findViewById(R.id.cake);
image.setImageResource(R.drawable.wish);
}
How do I know it will help me?
It will help you because here in the new intent which you are giving, there the extra is being given. So, that value is being sent.
Create an intent then add your data in it like the code below.
public void createBirthdayCard(View view) {
EditText text = (EditText)findViewById(R.id.nameInput);
String str = text.getText().toString();
Toast.makeText(this, "Button was clicked "+str,Toast.LENGTH_LONG).show();
Intent intent = new Intent(this,birthdaygreeting.class);
intent.putExtra(MSG,str);
startActivity(intent) ;
ImageView image=(ImageView) findViewById(R.id.cake);
image.setImageResource(R.drawable.wish);
}
Related
After pressing the button I would like to go to the second activity, enter the data in the second activity and then return to the main activity, but having data in ListView. This is my code:
MainActivity:
public class MainActivity extends AppCompatActivity {
Button button;
ListView listView;
String name;
private static final int REQUEST_CODE = 1;
ArrayAdapter<String> adapter;
ArrayList<String> nameList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button2);
listView = findViewById(R.id.CarList);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i, REQUEST_CODE);
}
});
nameList = new ArrayList<String>();
nameList.addAll(Arrays.asList(name));
adapter = new ArrayAdapter<String>(this, R.layout.element, nameList);
listView.setAdapter(adapter);
}
protected void onActivityResult(int requestCode, int resultCode, Intent i){
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
name = i.getStringExtra("name");
}
}
}
And this is my SecondActivity:
public class SecondActivity extends AppCompatActivity {
EditText editText;
Button button2;
String name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editText = findViewById(R.id.editText);
button2 = findViewById(R.id.button);
}
public void finish() {
Intent i = new Intent();
name = editText.getText().toString();
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish();
}
}
What could I change to make the application work? Because now I only get the message that: app has stopped, but I don't receive information about any error.
After pressing the button I would like to go to the second activity, enter the data in the second activity and then return to the main activity, but having data in ListView.
What could I change to make the application work? Because now I only get the message that: app has stopped, but I don't receive information about any error.
This is simply because you've incorrectly thinking that Activity.finish() is always called whenever you close the activity. But it never be called by the Android. Take a look for this Lifecyle picture from Activity-lifecycle concepts:
you can see that only onStop() then onDestroy() is called when activity is closed.
You need to call the finish() method manually to send your intent. Or, the better way, create a method that only build and set the intent for the result. Something like this:
private void prepareResult(String name) {
Intent i = new Intent();
i.putExtra("name", name);
setResult(RESULT_OK, i);
}
then call it whenever you want to close your activity:
String name = editText.getText().toString();
prepareResult(name);
finish();
Or you can override the onBackPressed() to also handing the back pressed, like the following:
#Override
public void onBackPressed() {
Intent i = new Intent();
name = editText.getText().toString();
i.putExtra("name", name);
setResult(RESULT_OK, i);
// The default implementation simply finishes the current activity
// see the documentation.
super.onBackPressed();
}
I am making a Form App that collects information and sends it in an email. MainActivity collects phone #, then to a warning message activity, then to Name Activity etc. The issue that I am having is sending the data collected from the EditText fields to the final AgreementActivity to be sent as an email is not working for me. I have looked everywhere but I cannot figure out how to send the user to the next activity while sending the input data to the final Agreement activity so it can be sent in an email.
This is what I have so far.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the next button
final Button next1 = (Button) findViewById(R.id.button);
// Find the Edit Text Field to input Phone Number
final EditText phoneField = (EditText) findViewById(R.id.edit_text_phone);
// Set a click listener on the next button
next1.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the next1 Button is clicked on.
#Override
public void onClick(View view) {
// sends data collected from edit text box to be sent to final page so it can be
// sent in an email message.
Intent phoneNumber = new Intent(MainActivity.this, AgreementActivity.class);
phoneNumber.putExtra("phoneMessage", phoneField.getText().toString());
startActivity(phoneNumber);
//starts next activity
Intent nextButton = new Intent(MainActivity.this, Warning.class);
startActivity(nextButton);
}
});
}
}
This is the final Agreement Activity where it will send an email.
But the final result in the email subject line is "your phone number is null"
public class AgreementActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.agreement_activity);
// Find the submit button
final Button submit = (Button) findViewById(R.id.button6);
// This is the input from user for phone number field
final Bundle phoneNumber = getIntent().getExtras();
// Set a click listener on that View
submit.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the Submit Button is clicked on.
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.getBundleExtra("phoneMessage");
intent.putExtra(Intent.EXTRA_SUBJECT, "Your Phone Number is " + phoneNumber);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
});
}
}
In your AgreementActivity, get the phone number using:
final String phoneNumber = getIntent().getStringExtra("phoneMessage");
I would extend application class like this however you could find other ways. You do need to consider the backstack if someone navigates back arrow
i have my first activity with two EditText fields with hint's as first name and last name, when ever i go to my second activity and return to my first activity by a widget button the EditText fields in my first activity gets reset.
SIMPLY i want my EditText fields to remain same in my first activity as i re-visit my first activity from my second activity.
MY MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButton1Click(View v)
{
Intent intent = new Intent (this,MainActivity2.class);
startActivity(intent);
}
}
My MainActivity2.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
SOLUTION - i changed my second activity to this. just added reference to my Button and applied an onClickListener with onBackPressed(); code.
setContentView(R.layout.activity_main2);
Button button = (Button)findViewById(R.id.button3); //gave reference of button in second activity
button.setOnClickListener(new View.OnClickListener() { //and applied an onClickListener with code onBackpressed();
public void onClick(View v) {
onBackPressed();
// Code here executes on main thread after user presses button
}
});
}
Really Really thnx to #Narendra Sorathiya for his guidance.
Open secondActivity like below,
Intent i = new Intent(A.this,B.class);
startActivity(i);
finish();
to
Intent i = new Intent(A.this,B.class);
startActivityForResult(i,1);
do not open 1st Activity from 2nd Activity, just need to back press.
try this
declare all variavle at top of activity
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String FNAME = "fname";
public static final String LNAME = "lname";
SharedPreferences sharedpreferences;
write this code when you move to your next activity
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
// store login data in sharedpreferences
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(FNAME, edittext.getText());
editor.putString(LNAME, edittext2.getText());
editor.commit(); //save data in sharedpreferences
write this code in your oncreate method after you bind your controls
and get data from sharedprefrence like this
String fname= prefs.getString(FNAME, "");
String fname= prefs.getString(LNAME, "");
than set it to your editext like this
editext.setText(fname);
editext2.setText(lname);
let me know in case of any query
I have simple page to start up the app that has a spinner,editText, and a button. When the user clicks the button I want the app take the selection of the spinner and text and set them to text views in the new class. I tried using getters but when I try this the app crashes.
First Class:
public class MainActivity extends AppCompatActivity {
String pullerName;
String storeName;
Spinner spinner;
EditText etPuller;
public String getStoreName(){
return storeName;
}
public String getPullerName(){
return pullerName;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.store_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Button create = (Button) findViewById(R.id.btnCreate);
etPuller = (EditText) findViewById(R.id.editText);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pullerName = etPuller.getText().toString();
storeName = spinner.getSelectedItem().toString();
Intent i = new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
}
});
}
}
Second Class:
public class MainActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_main);
MainActivity main = new MainActivity();
TextView user = (TextView) findViewById(R.id.tvUser);
TextView store = (TextView) findViewById(R.id.tvStore);
user.setText(main.getPullerName());
store.setText(main.getStoreName());
When this code runs it crashes as soon as i click the button to move to the next activity.
To pass data to a new Activity you need to use Intent.
This is what you need to do:
MainActivity.class:
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pullerName = etPuller.getText().toString();
storeName = spinner.getSelectedItem().toString();
Intent i = new Intent(MainActivity.this,MainActivity2.class);
i.putExtra("puller-name", pullerName);
i.putExtra("store-name", storeName);
startActivity(i);
}
});
In the MainActivity2.class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_main);
TextView user = (TextView) findViewById(R.id.tvUser);
TextView store = (TextView) findViewById(R.id.tvStore);
Intent intent = getIntent();
if(intent != null){ //I always check this to avoid Exceptions
String pullerName = intent.getStringExtra("puller-name");
String storeName = intent.getStringExtra("store-name");
if(pullerName != null) user.setText(pullerName);
if(storeName != null) store.setText(storeName);
}
}
You was creating a new instance of MainActivity in MainActivity2. So, when you called the getters of this mainActivity will return null, because these values were never setters. If you want to use your method, you need to pass the same instance of the activity to MainActivity2, but this is not recommended. The official method to pass data to another activity is via intent. To learn more about Intent learn the official Android documentation: https://developer.android.com/training/basics/firstapp/starting-activity.html
You have used wrong way to get values from MainActivity to MainActivity2. Simply you can pass values using the technique below.
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
intent.putExtra("pullerName", pullerName);
To use this value pullerName in MainActivity2 class, just use the technique below.
String value = getIntent().getExtras().getString("pullerName");`
I have two activities that I would like to send data between. I would like to send two strings from the second activity to the first, where the strings will be manipulated and applied to the code. Here is the intent I have in the second activity to send the strings to the first activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
phoneNum = (EditText)findViewById(R.id.phoneNum);
msgCounter = (EditText)findViewById(R.id.msgCount);
apply = (Button)findViewById(R.id.butto);
save = (Button)findViewById(R.id.butt);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pass = new Intent();
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
startActivity(pass);
}
});
And here is the intent I have in the first acitivity to receive the data:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_messages);
request_code = 1;
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.but);
assert button != null;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(TextMessages.this,Settings.class);
startActivityForResult(intent, request_code);
Intent a = new Intent(TextMessages.this,Settings.class);
startActivityForResult(a, recode);
}
});
Intent pass = getIntent();
String address = pass.getStringExtra("value");
String string2 = pass.getStringExtra("cost");
However, the app always crashes when I start it. So I can not use any of the data. Thanks in advance to any help I may receive.
In the first activity you should get your strings from onActivityResult. You shouldn't use startActivity in second activity.
Checkout this link for more information
https://developer.android.com/training/basics/intents/result.html
In your second activity you should use setResult method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
phoneNum = (EditText)findViewById(R.id.phoneNum);
msgCounter = (EditText)findViewById(R.id.msgCount);
apply = (Button)findViewById(R.id.butto);
save = (Button)findViewById(R.id.butt);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pass = new Intent();
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
setResult(Activity.RESULT_OK,pass);
finish();
}
});
In your first activity you should get Intent data inside the onActivityResult method.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == request_code) {
if(resultCode == Activity.RESULT_OK){
String value=data.getStringExtra("value");
String cost=data.getStringExtra("cost");
//handle value and cost.
}
}
}
I think it's because you missed out to put the target Activity class when you instantiate your intent. It should be like this:
Intent pass = new Intent(TargetActivity.class);
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
startActivity(pass);
Hope it helps.
EDIT: But I agree with Abdullah. Since the first activity started the second activity, use onActivityResult to pass values to the first activity.
My guess is, the app starts into the second code block you posted. There is no value for String address = pass.getStringExtra("value"); or String string2 = pass.getStringExtra("cost"); when you start your app - only when the second activity sends these to the first activity. I would recommend finding a way to determine if its the second activity creating the first activity or creating a method to set these values in the first activity and call it in the second activity. Also, your second activity should be:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
phoneNum = (EditText)findViewById(R.id.phoneNum);
msgCounter = (EditText)findViewById(R.id.msgCount);
apply = (Button)findViewById(R.id.butto);
save = (Button)findViewById(R.id.butt);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pass = new Intent(SecondActivity.this, FirstActivity.class);
pass.putExtra("value", phoneNum.getText().toString());
pass.putExtra("cost",msgCounter.getText().toString());
startActivity(pass);
}
});