How do i set .equals equal to the result of startActivityForResult - android

im kinda new to programming and would like to make some sort of like a login and register page. so from my login page i click register and it would go to the register page and i want the username/password i get from the register page to be used in the previous login page to login into the app. But i cant seem to set the username/password using the result. Only able to set the textview. pls help heres the code
public class MainActivity extends AppCompatActivity {
private EditText Name;
private EditText Password;
private TextView Info;
private Button Login;
private int counter = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name = (EditText)findViewById(R.id.etName);
Password = (EditText)findViewById(R.id.etPass);
Info = (TextView) findViewById(R.id.tvInfo);
Login = (Button)findViewById(R.id.btnLogin);
Info.setText("No Of Attempts Remaining: 5");
Login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validate(Name.getText().toString(),Password.getText().toString());
}
});
}
private void validate(String userName, String userPassword){
if(userName.equals("") && userPassword.equals("")){
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
startActivity(intent);
}
else{
counter--;
Info.setText("No Of Attempts Remaining: " + String.valueOf(counter));
if(counter == 0){
Login.setEnabled(false);
}
}
}
public void facebooklogin(View myview){
Intent intent = new Intent(this, MenuActivity.class);
startActivity(intent);
}
public void register(View myview) {
Intent i = new Intent(this, RegisterActivity .class);
startActivityForResult(i, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
String reginame = data.getStringExtra("NAME");
String regipass = data.getStringExtra("PASS");
Name.setText("" + reginame);
Password.setText("" + regipass);
}
}
}
How do i set the
private void validate(String userName, String userPassword){
if(userName.equals("") && userPassword.equals("")){
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
startActivity(intent);
}
to be equal to the onActivityResult reginame and regipass

your condition is wrong it should be like this:-
private void validate(String userName, String userPassword){
if(!userName.equals("") && !userPassword.equals("")){
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
startActivity(intent);
}

First Activity.
Send information
Intent send = new Intent(MainActivity.this, Main2Activity.class);
send.putExtra("login",editText.getText());
send.putExtra("password",editText1.getText());
startActivity(send);
Second Activity.
Get Information
if(getIntent()!=null){
Intent intent = getIntent();
editText.setText(intent.getStringExtra("login")) ;
editText1.setText(intent.getStringExtra("password")) ;
}

Related

My first project stuck on OnActivityforResult

I tried to make a simple app which can keep scores on a game with four players. When I press the plus or minus button it opens a new activity (dialog theme) where the user should enter their score. That score should be stored into a text view.
Some part of code looks like this:
Here I opened new activities
buttonPlus1.setOnClickListener(this);
buttonPlus2.setOnClickListener(this);
buttonPlus3.setOnClickListener(this);
buttonPlus4.setOnClickListener(this);
buttonMinus1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttPlusPlayer1:
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
break;
case R.id.buttPlusPlayer2:
Intent i2 = new Intent(this, ThirdPlusActivity.class);
startActivityForResult(i2, 2);
break;
case R.id.buttPlusPlayer3:
Intent i3 = new Intent(this, FourthPlusActivity.class);
startActivityForResult(i3, 3);
break;
case R.id.buttPlusPlayer4:
Intent i4 = new Intent(this, FifthPlusActivity.class);
startActivityForResult(i4, 4);
break;
case R.id.buttMinusPlayer1:
Intent i5 = new Intent(this, FirstMinusActivity.class);
startActivityForResult(i5, 1);
}
}
That's an example how activities send data back to main activity
public class SecondActivity extends AppCompatActivity {
private EditText mEditText;
private ImageButton mBackSpace;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
setTitle("ADD POINTS");
mEditText = findViewById(R.id.editTxtActivity);
//implementing backspace button
mBackSpace = findViewById(R.id.backSpaceButton);
mBackSpace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str = mEditText.getText().toString();
if (str.length() > 0) {
str = str.substring(0, str.length() - 1);
mEditText.setText(str);
}
}
});
}
#Override
public void onBackPressed() {
Intent i = new Intent();
i.putExtra("message", mEditText.getText().toString());
setResult(RESULT_OK, i);
Toast.makeText(this, "You added " + mEditText.getText().toString() + " points", Toast.LENGTH_LONG).show();
finish();
}
}
and "now" I receive the results from activities
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
String a = data.getStringExtra("message");
int i =Integer.parseInt(a);
String b = data.getStringExtra ("message5");
int z = Integer.parseInt(b);
Integer fResult = i - z;
String firstResult = fResult.toString();
mFirstScore.setText(firstResult);
}
if (requestCode == 2 && resultCode == RESULT_OK) {
mSecondscore.setText(data.getStringExtra("message2"));
}
if (requestCode == 3 && resultCode == RESULT_OK) {
mThirdScore.setText(data.getStringExtra("message3"));
}
if (requestCode == 4 && resultCode == RESULT_OK) {
mFourthscore.setText(data.getStringExtra("message4"));
}
the problem is on the last part of the code because I don't know how to receive data from activities and to calculate the result. I want when the user hit the plus button to add numbers and when minusButton is clicked to substract and the result to be stored on a TextView which, for player1, in my case is mFirstScore.
Is this possible or I did everything wrong and it is not possible to manipulate data received from another activity how I image
PS: on case buttMinusPlayer1 and on first if with requestCode 1 I've tried something ;
On SecondActivity put your key message2
#Override
public void onBackPressed() {
Intent i = new Intent();
i.putExtra("message2", mEditText.getText().toString());
setResult(RESULT_OK, i);
finish();
}
and get the data from SecondActivity by the same key message2
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2 && resultCode == RESULT_OK) {
mSecondscore.setText(data.getStringExtra("message2"));
}
}
Hope this helps.

How to go to next activity after sending email in Android by clicking one button?

After sending email, I need to go other activity. But I'm going to next activity before sending email. the same question having answer that tells to use startactivityforresult.bt I'm new to Android. I don't know how to use that.
public class GetQuoteact extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private ProgressDialog loadDialog;
Button btsub;
private String mobilee, namee, emailide, statee, citye, pine, subjecte, streete, success,endresp;
EditText name_c, mobile_c, emailid_c, state_c, city_c, street_c, pin_c, subject_c;
public String batter_feat_id,Modelname,Batterytype = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
// requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_getquote2);
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
name_c = (EditText) findViewById(R.id.name);
mobile_c = (EditText) findViewById(R.id.mobile);
emailid_c = (EditText) findViewById(R.id.email_id);
state_c = (EditText) findViewById(R.id.state);
city_c = (EditText) findViewById(R.id.city);
street_c = (EditText) findViewById(R.id.street);
pin_c = (EditText) findViewById(R.id.pincode);
subject_c = (EditText) findViewById(R.id.subject);
btsub = (Button) findViewById(R.id.bt_sub);
Intent in1 = getIntent();
batter_feat_id = in1.getStringExtra("battery_featuer_idc");
Modelname = in1.getStringExtra("Model_name");
Batterytype = in1.getStringExtra("Battery_type");
btsub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
inilize();
try {
if (!validate()) {
Toast.makeText(GetQuoteact.this, "Enter necessary details!!!", Toast.LENGTH_SHORT).show();
} else {
showDialog();
onfetch(batter_feat_id);}
} catch (Exception e) {
Toast.makeText(GetQuoteact.this, "" + e, Toast.LENGTH_SHORT).show();
}
}
});
}
public void inilize() {
namee = name_c.getText().toString();
mobilee = mobile_c.getText().toString();
emailide = emailid_c.getText().toString();
statee = state_c.getText().toString();
citye = city_c.getText().toString();
pine = pin_c.getText().toString();
subjecte = subject_c.getText().toString();
streete = street_c.getText().toString();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
Intent intent=new Intent(getApplicationContext(),Lastpage.class);
startActivity(intent);
}
else{
Toast.makeText(GetQuoteact.this, "error on sending mail...", Toast.LENGTH_SHORT).show();
}
}
private void onfetch(String batter_feat_id) {
Intent ithh =new Intent(Intent.ACTION_SENDTO);
ithh.setData(Uri.parse("mailto:"));
String[] to={"abc#gmail.com"};
ithh.putExtra(ithh.EXTRA_EMAIL,to);
ithh.putExtra(ithh.EXTRA_SUBJECT,"Email From JC APP");
ithh.putExtra(ithh.EXTRA_TEXT,"Model Name :"+Modelname
+"\nBattery Type:"+Batterytype
+"\nName :"+namee
+"\nContact no :"+mobilee
+"\nmailid="+emailide
+"\nstate="+statee
+"\ncity="+citye
+"\narea="+streete
+"\npincode="+pine
+"\nsubject="+subjecte);
startActivityForResult(ithh.createChooser(ithh,"Sent!!!"),1);
}
else{
Toast.makeText(GetQuoteact.this, "error!!! ", Toast.LENGTH_SHORT).show();
}
}
}
change your onActivityResult to something like this
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//autocompleteFragment.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// success open your activity
}
}
}
I hope this helps

Android intent.getStringExtra() return null

MainActivity
public class MainActivity extends AppCompatActivity {
private static final int REQ_CODE_TO_ADD = 123;
final ArrayList<Contact> allContact = new ArrayList();
ArrayList<String> name = new ArrayList();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent intent = new Intent(this,DetailActivity.class);
Button addbt = (Button)findViewById(R.id.addbt);
public void onClickAdd(View v){
Intent intent = new Intent(this,AddContactActivity.class);
startActivityForResult(intent,REQ_CODE_TO_ADD);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQ_CODE_TO_ADD){
if(resultCode == 0){
Intent intent = getIntent();
String name2 = intent.getStringExtra("namev");
String email2 = intent.getStringExtra("emailv");
String birthday2 = intent.getStringExtra("birthdayv");
Log.d("AAA",">>>:"+name2);
Contact person = new Contact(name2,email2,birthday2);
allContact.add(person);
}}
}
}
AddContactActivity
public class AddContactActivity extends AppCompatActivity {
private static final int REQ_CODE_TO_MAIN = 321;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
}
public void onClickOk(View v){
EditText name = (EditText)findViewById(R.id.nameet);
EditText email = (EditText)findViewById(R.id.email);
EditText birthdate = (EditText)findViewById(R.id.birthdate);
Intent intent = new Intent();
intent.putExtra("namev",name.getText().toString());
intent.putExtra("emailv",email.getText().toString());
intent.putExtra("birthdayv",birthdate.getText().toString());
setResult(0,intent);
finish();
}
}
AddContactActivity I already use intent.putExtra name.getText().toString() and send intent to MainActivity
Why onActivityResult() in MainActivity Log.d output is null?
if(resultCode == 0){
//Intent intent = getIntent();
String name2 = data.getStringExtra("namev");
String email2 = data.getStringExtra("emailv");
String birthday2 = data.getStringExtra("birthdayv");
Log.d("AAA",">>>:"+name2);
Contact person = new Contact(name2,email2,birthday2);
allContact.add(person);
}}
you need to use the data not getIntent()
You are using the Intent which originally launched the Activity. Use the Intent which was sent as a parameter instead.
There's no need for
Intent intent = getIntent();
Intent is already passed as argument i.e 'data'
Use this variable to extract data.
Hope this helps.
Intent intent = getIntent(); // This line is wrong
String name2 = intent.getStringExtra("namev");
String email2 = intent.getStringExtra("emailv");
String birthday2 = intent.getStringExtra("birthdayv");
Modify your code like this
if(requestCode==2 && resultCode==RESULT_OK){
Bundle bundle=data.getExtras();// here "data" is your intent
String string=bundle.getString("message");
Log.i(TAG,"onActivityResult Called..."+string);
}

how to properly use intent? Android beginner

I am a beginner in android and I am trying to learn how to use intent.
In my code, I am trying to send 2 integers to a different activity and perform some calculation and return back in the Main activity with the answer.
This is my main activity and with a click of a button it should send 10, 50 to my calculate class and from there I will click operator buttons like add, multiply, divide and send back the answer here in main activity.
So far I am able to received these numbers to my Calculate class and perform operation there but I am not sure how to get back here in main activity with the answers.
Main Activity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(getIntent() != null){
String msg = getIntent().getStringExtra("MSG");
int result = getIntent().getIntExtra("result", 0);
Toast.makeText(this, "" + msg + result, Toast.LENGTH_LONG).show();
}
}
public void onClick(View v){
Intent i = new Intent(this, Calculate.class);
i.putExtra("MSG", "Receive two numbers : ");
i.putExtra("second", 10);
i.putExtra("first", 50);
startActivity(i);
}
}
Calculate class
public class Calculate extends Activity{
private int calc;
private int first;
private int second;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent i = getIntent();
if(getIntent() != null){
String msg = i.getStringExtra("MSG");
first = i.getIntExtra("first", 0);
second = i.getIntExtra("second", 0);
Toast.makeText(this, "" + msg + first + " and " + second, Toast.LENGTH_LONG).show();
}
}
public void add(View v){
calc = first + second;
Intent i = new Intent(this, MainActivity.class);
i.putExtra("MSG", "Answer");
i.putExtra("result",calc);
}
}
you can use startActivityForResult and onActivityResult methods. fist in main activity you call startActivityForResult that means you want to get result from secondActivity (Calculate activity) and then override onActivityResult in main activity to get result.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(getIntent() != null){
String msg = getIntent().getStringExtra("MSG");
int result = getIntent().getIntExtra("result", 0);
Toast.makeText(this, "" + msg + result, Toast.LENGTH_LONG).show();
}
}
public void onClick(View v){
Intent i = new Intent(this, Calculate.class);
i.putExtra("MSG", "Receive two numbers : ");
i.putExtra("second", 10);
i.putExtra("first", 50);
startActivityForResult(i, 1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
}
public class Calculate extends Activity{
private int calc;
private int first;
private int second;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent i = getIntent();
if(getIntent() != null){
String msg = i.getStringExtra("MSG");
first = i.getIntExtra("first", 0);
second = i.getIntExtra("second", 0);
Toast.makeText(this, "" + msg + first + " and " + second, Toast.LENGTH_LONG).show();
}
}
public void add(View v){
calc = first + second;
Intent returnIntent = Intent(this, MainActivity.class);
returnIntent.putExtra("result",calc );
setResult(RESULT_OK,returnIntent);
finish();
}
}
public class Calculate extends Activity{
private int calc;
private int first;
private int second;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent i = getIntent();
if(getIntent() != null){
String msg = i.getStringExtra("MSG");
first = i.getIntExtra("first", 0);
second = i.getIntExtra("second", 0);
Toast.makeText(this, "" + msg + first + " and " + second, Toast.LENGTH_LONG).show();
}
}
public void add(View v){
calc = first + second;
Intent i = new Intent(this, MainActivity.class);
i.putExtra("MSG", "Answer");
i.putExtra("result",calc);
startActivity(i);
}
}
You need a startActivity(i); in add() function.
You can do this by
onActivityResult()
method of android intent.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(data.getExtras().containsKey("widthInfo")){
width.setText(data.getStringExtra("widthInfo"));
}
if(data.getExtras().containsKey("heightInfo")){
height.setText(data.getStringExtra("heightInfo"));
}
}
See these links for more info:
http://developer.android.com/training/basics/intents/result.html
http://www.java2s.com/Code/Android/UI/CheckActivityresultandonActivityResult.htm
http://www.mybringback.com/android-sdk/12204/onactivityresult-android-tutorial/
It looks like you should be using startActivityForResult() here.
So, in your MainActivity, your onClick() would be modified to call startActivityForResult():
public void onClick(View v){
Intent i = new Intent(this, Calculate.class);
i.putExtra("MSG", "Receive two numbers : ");
i.putExtra("second", 10);
i.putExtra("first", 50);
startActivityForResult(i, 100); //modified
}
Then in MainActivity, you would also add onActivityResult to get the result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100 && resultCode == RESULT_OK){
String msg = data.getStringExtra("MSG");
int result = data.getIntExtra("result", 0);
Toast.makeText(this, "" + msg + result, Toast.LENGTH_LONG).show();
}
}
Then, in the Calculate Activity, you would calculate the result, and send it back to MainActivity in an Intent, which will send the the MSG and result to onActivityResult() in MainActivity:
public void add(View v){
calc = first + second;
Intent i = new Intent(); //modified
i.putExtra("MSG", "Answer");
i.putExtra("result",calc);
setResult(RESULT_OK,i); //added
finish(); //added
}
Note that the reason it would be better for you to use startActivityForResult() here is that it would prevent having to add multiple Activities onto the back stack.
Say for example, you do two calculations (two trips from MainActivity to Calculate and back).
If you just used startActivity(), your back stack would then look like this:
MainActivity->Calculate->MainActivity->Calculate->MainActivity
And, it would just keep growing the more calculations you do.
Using startActivityForResult(), you are always just adding an instance of Calculate to the back stack, and then popping it and returning to the same instance of MainActivity. So, your back stack would always be just:
MainActivity
or...
MainActivity->Calculate
depending on which Activity you are currently in. As you can see, this is a huge improvement over the first option, which just keeps growing as you do more calculations.

Android. on button click, jump to an onActivity result function. IT IS POSSIBLE?

i have button which have attribute android:onClick="atnDuom".
There is that function
public void atnDuom(View view)
{
finish();
}
and there is onActivityResult function in the same activity.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
DOP = new DatabaseOperations(ctx);
Intent returnIntent = new Intent();
user_name = data.getStringExtra("tarpVard");
user_lastname = data.getStringExtra("tarpPav");
institucijos_pavadinimas = data.getStringExtra("tarpInst");
padalinio_pavadinimas = data.getStringExtra("tarpPad");
pareigos = data.getStringExtra("tarpPar");
mob_tel = data.getStringExtra("tarpMob");
el_pastas = data.getStringExtra("tarpEl");
setResult(RESULT_OK,returnIntent);
DOP = new DatabaseOperations(ctx);
if(newVard.equals("")||newPav.equals("")||newInst.equals("")||newPad.equals("")||newPar.equals("")||newMob.equals("")||newEl.equals(""))
{
Toast.makeText(getBaseContext(), R.string.prashome, Toast.LENGTH_LONG).show();
}
else
{
DOP.updateUserInfo(DOP, user_name, user_lastname, institucijos_pavadinimas, padalinio_pavadinimas, pareigos, mob_tel, el_pastas, newVard, newPav, newInst, newPad, newPar, newMob, newEl);
Toast.makeText(getBaseContext(), "Duomenys atnaujinti", Toast.LENGTH_LONG).show();
finish();
}
}
}
}
It is possible to execute function onActivityResult whithout doing anything in atnDuom function?
Finish() close activity and onActivityResult doesnt work :)
You are using data from the intent, if you want to go to onActivityResult from atnDuom you will need to create a new Intent and push all the data needed
Intent newIntent = new Intent();
newIntent.putExtras(...);
onActivityResult(REQUEST_CODE, RESULT_OK, newIntent);

Categories

Resources