I have 2 EditTexts. EditText1 is in MainActivity, EditText2 is in SecondActivity.
EditText1 is to login (password), EditText2 is to change password.
My code looks like this:
EditText editText1 = findViewById(R.id.login);
editText2 = findViewById(R.id.changePassword); // declared in SecondActivity
if (editText1.getText().toString().equals(editText2.getText().toString())
{
Intent intent = new Intent (MainActivity.this, SecondActivity.class);
startActivity(intent);
}
else
{
Toast.makeText(MainActivity.this, "Password incorrect", Toast.Length_Long).show;
}
When I press Button to login, it shows me an error. I know it has to be initialized in a different way but how?
I tried another code with a Dialog and everything worked perfectly:
changePasswordDialog = new Dialog(MainActivity.this);
changePasswordDialog.setContentView(R.layout.activity_second_activity);
editText2 = changePasswordDialog.findViewById(R.id.changePassword);
So it works perfectly with Dialog, but how does it work without Dialog?
Try Like this
Store edit text 2 pass
String pass = editText2.getText().toString().trim();
SharedPreferences.Editor editor = getSharedPreferences(My_Prefs,Context.MODE_PRIVATE).edit();
editor.putString("pass", pass);
editor.apply();
Now Retrive the stored passwod in MainActivity
SharedPrefrences prefrences = getSharedPrefrences(My_Prefs,Context.MODE_PRIVATE);
String pass = prefrences.getString("pass","");
if (editText1.getText().toString().equals(pass)
{
Intent intent = new Intent (MainActivity.this, SecondActivity.class);
startActivity(intent);
}
You can use intent put extra and intent get extra for this
/// you can use this as per your requirement or you can use sharedprefrence///
// In main activity ///
Intent intent = new Intent (MainActivity.this, SecondActivity.class);
intent.putExtra("editText1",editText1.getText().toString());
startActivity(intent);
/// In second activity//
String passWord = getIntent().getExtras().getString("editText1");
Log.d("password : ",passWord);
Related
I am new to android!.
I have made spinner in activity 1 (one time open Activity) and want to transfer selected value from spinner by user to activity 2.I have used sharedPreference for one time open activity .Its only working 1st time but second time it cant transfer data.I am stuck here please help me
SharedPreferences Preferences=getSharedPreferences("PREFERENCE",MODE_PRIVATE);
String FirstTime=Preferences.getString("FirstTimeInstall","");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String parent=sp_parent.getSelectedItem().toString();
String child=sp_child.getSelectedItem().toString();
String college=sp_college.getSelectedItem().toString();
Intent intent=new Intent(getApplicationContext(),Dashboard.class);
intent.putExtra("parent",parent);
intent.putExtra("child",child);
intent.putExtra("college",college);
startActivity(intent);
finish();
}
});
if (FirstTime.equals("Yes")){
String parent=sp_parent.getSelectedItem().toString();
String child=sp_child.getSelectedItem().toString();
String college=sp_college.getSelectedItem().toString();
Intent intent=new Intent(getApplicationContext(),Dashboard.class);
intent.putExtra("parent",parent);
intent.putExtra("child",child);
intent.putExtra("college",college);
startActivity(intent);
finish();
}else {
SharedPreferences.Editor editor=Preferences.edit();
editor.putString("FirstTimeInstall","Yes");
editor.apply();
}
When starting a new activity you can send data through intent.
Intent intent = new Intent( this, Activity2.class );
intent.putExtra( "key", "value" );
startActivity(intent)
And in second activity do like this:
getIntent().getStringExtra( "key" );
or if you are sending int for example
getIntent().getIntExtra( "key", 0 );
I created a Method for my MainActivity to pass a String to my SecondActivity.
public void convertCurrency(View view)
{
intent.putExtra("NO", "My String");
startActivity(intent);
}
But in my SecondActivity in my OnCreate Method
Button back = (Button) findViewById(R.id.back);
TextView t = (TextView) findViewById(R.id.first_text);
Intent intent = new Intent(this, MainActivity.class);
String g = intent.getStringExtra("NO");
t.setText(g);
Nothing happens. But Why?
get Your variable in this way:
String yourVriable = getIntent().getStringExtra("NO")
don't new Your intent
Your are creating new intent.
In onCreate method call
String data = getIntent().getStringExtra(key);
replace Intent intent = new Intent(this, MainActivity.class);
by Intent intent = getIntent();
When you do intent.getStringExtra("NO"); your intent Object is new, so his Extra is empty.
You need to change the code in both the activities . You need to create intent and put your string into it, so your code will be.
public void convertCurrency(View view) {
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("NO","My String");
startActivity(intent);
}
Note that in the intent constructor the first argument is the current activity and the second argument will be the activity that you are starting.So in the second activity you need to recieve the string. Code for that will be,
Button back = (Button) findViewById(R.id.back);
TextView t = (TextView) findViewById(R.id.first_text);
String g = getIntent().getStringExtra("NO")
t.setText(g);
I resolve the problem myself. There occurs an error if you have two Intent objects with the same name even if they are within separate methods.
I want to transfer my "result" data from my first (Main) acitivity to
my Custaddress activity, which has edit texts for customer details, and then this is sent to an email. The email/edit texts work perfectly - but I want to
add in "result.toString" into email body string. How do I transfer "result" to the second activity? I believe its something to do with arg?
Here's my code from first activity..
DecimalFormat decimalFormat = new DecimalFormat(COMMA_SEPERATED);
result.append("\nTotal: £"+decimalFormat.format(totalamount));
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setMessage(result.toString());
alertDialogBuilder.setTitle("YOUR ORDER");
alertDialogBuilder.setPositiveButton("Accept",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
//do what you want to do if user clicks ok
//Intent intent = new Intent(context, Custaddress.class);
// startActivity(intent);
Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
startActivity(custaddress);
}
});
alertDialogBuilder.setNegativeButton("Decline",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//do what you want to do if user clicks cancel.
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
Write it in activity that passing data
Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
custaddress.putExtra("key",value);
startActivity(custaddress);
Write below code in activity that catching data
Intent intent=getIntent();
String mString=intent.getStringExtra("key");
hope this will help you
You should use intent (click here) :
Intent intent = new Intent(getBaseContext(), CustAdrresActivity.class);
intent.putExtra("text", mytext);
startActivity(intent);
You just need to replace your lines:
Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
startActivity(custaddress);
with these three lines:
Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
custaddress.putExtra("result", result.toString());
startActivity(custaddress);
and then, when you open the new activity (in your case the Custaddress Activity), you should do the following to get your result
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("result");
}
You should add Extra in the Intent object which you are passing in the startActivity(intent) method.
Example
String value = "String i want to send to next activity"
Intent intent = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
intent.putExtra("KEY", value);
startActivity(intent);
In the Custaddress.java Activity class you need to get the data from the Bundle object that you get as a parameter in the onCreate(Bundle bundle) method
Bundle extras = getIntent().getExtras();
if (extras != null) {
// get data via the key
String valueFromPreviousActivity = extras.getString("KEY");
if(valueFromPreviousActivity != null){
// do something with the data
}
}
Check out this official doc Starting Another Activity.
Through the below code we can send the values between activities
use the below code in parent activity
Intent myintent=new Intent(Info.this, GraphDiag.class).putExtra("<StringName>", value);
startActivity(myintent);
use the below code in child activity
String s= getIntent().getStringExtra(<StringName>);
My app requires a username to be added to function properly.
The mainActivity displays the username on the top which it retrieves form the database.
The mainActivity also has a button which leads to 'addusername' activity through startActivityForResult() method; when the user actually enters a username then only username on the main activity gets refreshed and displayed.
There is a submit button on the 'addusername' activity which adds the username and does setResult(RESULT_OK);
Everything works fine if the username is entered by clicking the 'addusername' button.
Now, I have added a dialogue in mainActivity which gets displayed when the app starts only if no username exists on the database. The dialogue gives an option to add username, which if clicked, leads to the 'addusername' activity. But upon pressing the 'submit' button the mainActivity does get called but the username is not refreshed (though, the database does get updated)
Here is the code for 'addusername' activity:
public void submitusername(View view) {
userdatabase name = new userdatabase(this);
name.open();
String user = name.getusername();
name.close();
EditText cinone = (EditText) findViewById(R.id.username);
username = cinone.getText().toString();
if(user.equals("")) {
userdatabase entry = new userdatabase(Editprofile.this);
entry.open();
entry.createEntry(username, null, null, null);
entry.close();
Context context = getApplicationContext();
CharSequence text = "Added new user!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else {
userdatabase update = new userdatabase(Editprofile.this);
update.open();
update.updateUsername(username);
update.close();
Context context = getApplicationContext();
CharSequence text = "Username updated!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
setResult(RESULT_OK);
finish();
}
Here is the code for Dialog:
public class NouserFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.nouseralert)
.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(getActivity().getBaseContext(), Editprofile.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.ignore, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
I understand this is happening because the Dialog does not call startActivityForResult() method. But even if I change my code, like this:
Intent intent = new Intent(getActivity().getBaseContext(), Editprofile.class);
startActivityForResult(intent, 0);
It still does not help. Probably because, onActivityResult() method does not lie in the same class from which startActivityForResult() has been called, but I don't know how to get after that.
EDIT 1: Tried using
Intent intent = new Intent(getActivity(), Editprofile.class);
startActivityForResult(intent, 0);
No use.
Have you tried this way:
Intent intent = new Intent(getActivity(), Editprofile.class);
startActivityForResult(intent, 0);
When you create your the Intent object you use the BaseContext, instead of using the reference to the activity itself.
Using
Intent intent = new Intent(getActivity(), Editprofile.class);
getActivity().startActivityForResult(intent, 0);
worked!
I am programming in android for a Samsung tablet, and I have 2 activities, one of this is a list of football teams, and the other activity is their twitter, but i have a problem to pass the parameter for the first activity to the second one. I want to pass their url like a string, but i cant get it.
Thanks!
You have to use Intent:
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("extraURL", "http://myUrl.com");
startActivity(i);
Then, to retrieve it in SecondActivity, in the onCreate method do:
Intent receivedIntent = getIntent();
String myUrl = receivedIntent.getStringExtra("extraURL");
You typically launch the 2nd Activity from the first using an Intent. You can pass data to the 2nd Activity using the same Intent you use to launch it. For example,
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("url", "http://url.you.want.to.pass/");
startActivity(i);
In the 2nd Activity, in the onCreate, you can read the data using:
Intent i = getIntent();
String url = i.getStringExtra("url");
your First Activity on click of button
Intent intent = new Intent(this,ActivityTwo.class );
intent.setAction(intent.ACTION_SEND);
intent.putExtra("www.google.com",true);
startActivity(intent);
Receive it in ActivityTwo:-
Intent intent = getIntent();
String msg = intent.getStringExtra("www.google.com");