Email intent in constant loop - java

Ive got my email intent working well as in it posts emails. Im now getting an issue where once I click 'send email' on my custom template, which sets the 'To', 'subject' and 'message' fields of the GMail template it loads fine but then when i click 'send' on the GMail template it loops back to the custom template. As shown:
This is my custom made email template:
Once 'send email' is clicked then the Gmail template is loaded:
Once the 'Send' button of the GMail template is cliked it loops back to the custom template and they loop between each other continuously. Hoping someone can give me an idea of how I can stop this looping!
Here is my code with my intent within the 'onClick':
package com.example.flybase2;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ContactsEmail extends Activity implements OnClickListener{
String emailPassed;
String emailAdd;
String emailSub;
String emailMess;
EditText setEmailAddress;
EditText setEmailSubject;
EditText setEmailMessage;
Button btnSendEmail;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emaillayout);
Bundle extras = getIntent().getExtras();
if (extras != null) {
emailPassed = extras.getString("passedEmailAdd");
}
setEmailAddress = (EditText) findViewById (R.id.inputEmailAddress);
setEmailAddress.setText(emailPassed);
setEmailSubject = (EditText) findViewById (R.id.inputEmailSubject);
setEmailMessage = (EditText) findViewById (R.id.inputEmailMessage);
btnSendEmail = (Button)findViewById(R.id.btnSendEmail);
btnSendEmail.setOnClickListener(this);
}
#Override
public void onClick(View sendEmailClick) {
emailAdd = setEmailAddress.getText().toString();
emailSub = setEmailSubject.getText().toString();
emailMess = setEmailMessage.getText().toString();
Intent sendEmailIntent = new Intent(Intent.ACTION_SEND);
sendEmailIntent.setType("plain/text");
sendEmailIntent.putExtra(Intent.EXTRA_EMAIL,new String[] {emailAdd});
sendEmailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSub);
sendEmailIntent.putExtra(Intent.EXTRA_TEXT, emailMess);
startActivity(Intent.createChooser(sendEmailIntent, "Send mail..."));
}
}

It looks like you simply want to call finish() at the end of onClick(). This will close ContactsEmail allowing the user to return to a more useful Activity from the GMail app.

Related

Android Studio: playing with Intents and sending data between activities

I'm new to Android Studio and Java, currently I'm learning about intents and sending data/text from one activity to another. On the second activity I get "Cannot resolve symbol 'intent'" even though it's in onCreate field. What I'm trying to do is to have two text fields for first and last name, sending the text from first activity to the second one which will read it, and all done with onButtonClick method. I'm only trying to do the first name and the code looks like this MainAcitivty
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClicked(View view) {
EditText nameText = (EditText)findViewById(R.id.firstName);
TextView messageText = (TextView) findViewById(R.id.messageTextView);
if(nameText.getText().toString().trim().length() == 0){
messageText.setText("text here");
} else {
String textForSecondAcitivity = "your name is: " + nameText.getText();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, textForSecondAcitivity);
startActivity(intent);
`
The problem is the second Activity that gives me the error "Cannot resolve symbol 'intent'". And here is the code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent scIntent = getIntent();
String messageText = intent.getStringExtra("EXTRA_TEXT");
TextView messageView = (TextView)findViewById(R.id.textView);
messageView.setText(messageText);
}
It wont show the error here but it's int this line
String messageText = intent.getStringExtra("EXTRA_TEXT");
I apologize in advance because surely I messed up something bad, I'm a beginner and if you have some advice on what's the best way to do what I'm actually trying to accomplish feel free to tell me.
The app should look like this in the end 1.The first activity
1.The second one showing first name and last name
You have no local variable named intent
Replace it with scIntent
Here you are making it wrong
intent.putExtra(Intent.EXTRA_TEXT, textForSecondAcitivity);
instead you use this
intent.putExtra("EXTRA_TEXT", textForSecondAcitivity);

Sugar ORM how do i add data to a row in the table

Good day all i am using android studio, I have a fairly good idea of how to use sugar ORM at this point, but there's one thing that I just cant find or figure out.
So i have an activity named UserProfiles.java in which i ask the user for various things and then i store them in the database. So at this point i have about 20 records/rows in the database. The problem however is when i send the user to the next activity (UserPins.java) i ask them to set some additional information now how do i add this information to the same line as the data that has the User Profiles information?
//this is my UserProfiles.java
package com.chika.mia;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.chika.mia.models.Mia;
public class UserProfile extends AppCompatActivity implements View.OnClickListener {
private EditText Name, PhoneNumber, Email, Password, Address;
private Button SaveUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
Name = (EditText)findViewById(R.id.Name);
PhoneNumber = (EditText)findViewById(R.id.PhoneNumber);
Email = (EditText)findViewById(R.id.Email);
Password = (EditText)findViewById(R.id.Password);
Address = (EditText)findViewById(R.id.Address);
SaveUser = (Button)findViewById(R.id.SaveUser);
SaveUser.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Mia m = new Mia();
m.setName(register.g);
m.setPhoneNumber(Integer.parseInt(PhoneNumber.getText().toString())) ;
m.setEmail(register.i) ;
m.setPassword(register.h);
m.setAddress(Address.getText().toString()) ;
m.setLocation("MiaL");
m.setWipePhone("MiaWP");
m.setDispMessage("MiaDM");
m.setAlarm("MIAA");
m.setShutdown("MIASD");
m.save();
Toast.makeText(this, " " + m.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(UserProfile.this, UserPins.class);
startActivity(intent);
}
}
And this is my Userpins.java
package com.chika.mia;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import com.chika.mia.models.Mia;
import com.orm.query.Condition;
import com.orm.query.Select;
public class UserPins extends AppCompatActivity implements View.OnClickListener {
private EditText UserLocation,UserWipePhone,UserDisplayMsg,UserAlarm,UserShurdown;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_pins);
UserLocation = (EditText)findViewById(R.id.UserLocation);
UserWipePhone = (EditText)findViewById(R.id.UserWipePhone);
UserDisplayMsg = (EditText)findViewById(R.id.UserDisplayMsg);
UserAlarm = (EditText)findViewById(R.id.UserAlarm);
UserShurdown = (EditText)findViewById(R.id.UserShutdown);
}
#Override
public void onClick(View v) {
Mia m = new Mia();
Select.from(Mia.class).where(Condition.prop("Name").eq(register.i)).list();
m.setLocation(UserLocation.getText().toString());
m.setWipePhone(UserWipePhone.getText().toString());
m.setDispMessage(UserDisplayMsg.getText().toString());
m.setAlarm(UserAlarm.getText().toString());
m.setShutdown(UserShurdown.getText().toString());
m.update();
Intent intent = new Intent(this,AppSettings.class);
startActivity(intent);
}
}
What am aiming to do is something like this, So for which ever user logs in say "Tom", i already have the login and registration page sorted out. It will go through the database and look for the name "Tom" or email "tom#gmail.com" and then add the userpins information to the appropriate column in that table.
When you create a new record in Sugar ORM, you get a unique ID back from the save(). On your first activity, get the ID, and pass it to the next activity:
long new_user_id = m.save();
...
Intent intent = new Intent(UserProfile.this, UserPins.class);
intent.putExtra("NEW_USER_ID", new_user);
startActivity(intent);
Then you can grab it in the new activity with:
long new_user_id = getIntent().getLongExtra("NEW_USER_ID",0);
You may then use the ID to get the record to update:
Mia m = Mia.findById(Mia.class, new_user_id);

Android save EditText and recall it next time the app is opened

I have code in which it has an editText box and a button. I want to save the text entered in the textbox and recall it the next time the app is opened.
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText txtLink;
Button btnOpenLink;
String defaultLink;
String secondLink;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
defaultLink = "http://";
secondLink = ".whatver.com";
txtLink = (EditText) findViewById(R.id.editText);
btnOpenLink = (Button) findViewById(R.id.button);
btnOpenLink.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String server = txtLink.getText().toString();
if(!TextUtils.isEmpty(server)){
Intent intent=new Intent(MainActivity.this,webactivity.class);
intent.setData(Uri.parse(defaultLink+server+secondLink));
startActivity(intent);
}else{
Toast.makeText(MainActivity.this, "Please enter your server name.", Toast.LENGTH_LONG).show();
}
}
});
}
}
How can this be accomplished? I have tried numerous things that I have found through google but none have seemed to work right, I know it is something that I am doing most likely, I just can not make it work right.
Use the preferences....
Example
SharedPreferences spref = getSharedPreferences("your_prefs_name", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = spref.edit();
editor.putString("myTextViewValue", prefVal); //
editor.commit();
and somewhen latter when the app starts again read it back:
Example:
SharedPreferences preferences = getPreferences(Activity.MODE_PRIVATE);
String storedPreference = preferences.getStr("myTextViewValue", null);
so validate what you store and check if the preference you get is null, that means nothing was stored before...

How to forward edit text box to intent url in ADT?

So I'm fairly new to Java coding and I'm trying to create a simple code to have an edit text value act as a url for an intent internet command. I'm using this in Eclipse and ADT. The following is my java code.
import android.widget.EditText;
import android.widget.Button;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.Intent;
import android.net.Uri;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText UR_L=(EditText) findViewById(R.id.ur_l);
String sUR_L= new String(UR_L.getText().toString());
Intent brwsrintnt = new Intent(Intent.ACTION_VIEW, Uri.parse(sUR_L));
startActivity(brwsrintnt);
}
});
In my layout I have a 2 buttons and an edit box but the edit box reflects a missing input type error. I have no idea how to fix this and I've looked many places.
Thanks,
David
I think your problem might be that the EditText is never intialized until you have already hit the button, so when you go to get text from it, it doesn't know the user has already entered something. Try moving
EditText UR_L=(EditText) findViewById(R.id.ur_l);
to just before you set the OnClickListener and see if anything changes

Cannot set Gmail 'subject' and 'text' fields from an intent

I am calling an intent that sets the 'To' field no problem of a GMail template from a String array, but I cannot get it to set the 'subject' and 'text' fields.
Hopefully someone can see where I'm going wrong
Here's my code:
package com.example.flybase2;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ContactsEmail extends Activity implements OnClickListener{
String emailPassed;
String emailAdd;
String emailSub;
String emailMess;
EditText setEmailAddress;
EditText setEmailSubject;
EditText setEmailMessage;
Button btnSendEmail;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emaillayout);
Bundle extras = getIntent().getExtras();
if (extras != null) {
emailPassed = extras.getString("passedEmailAdd");
}
setEmailAddress = (EditText) findViewById (R.id.inputEmailAddress);
setEmailAddress.setText(emailPassed);
setEmailSubject = (EditText) findViewById (R.id.inputEmailSubject);
setEmailMessage = (EditText) findViewById (R.id.inputEmailMessage);
emailAdd = setEmailAddress.getText().toString();
emailSub = setEmailSubject.getText().toString();
emailMess = setEmailMessage.getText().toString();
btnSendEmail = (Button)findViewById(R.id.btnSendEmail);
btnSendEmail.setOnClickListener(this);
}
#Override
public void onClick(View sendEmailClick) {
Intent sendEmailIntent = new Intent(Intent.ACTION_SEND);
sendEmailIntent.setType("plain/text");
sendEmailIntent.putExtra(Intent.EXTRA_EMAIL,new String[] {emailAdd});
sendEmailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSub);
sendEmailIntent.putExtra(Intent.EXTRA_TEXT, emailMess);
startActivity(Intent.createChooser(sendEmailIntent, "Send mail..."));
}
}
Try this.
Actually When activity create you get the value of these editext. At that time value of these all are empty. so you need to write these in onclick method.
When you click on button then you will get the current edit text value.
I hope this will solve your problem.
#Override
public void onClick(View sendEmailClick) {
emailAdd = setEmailAddress.getText().toString();
emailSub = setEmailSubject.getText().toString();
emailMess = setEmailMessage.getText().toString();
Intent sendEmailIntent = new Intent(Intent.ACTION_SEND);
sendEmailIntent.setType("plain/text");
sendEmailIntent.putExtra(Intent.EXTRA_EMAIL,new String[] {emailAdd});
sendEmailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSub);
sendEmailIntent.putExtra(Intent.EXTRA_TEXT, emailMess);
startActivity(Intent.createChooser(sendEmailIntent, "Send mail..."));
}
and remove these three lines from onCreate() method.
emailAdd = setEmailAddress.getText().toString();
emailSub = setEmailSubject.getText().toString();
emailMess = setEmailMessage.getText().toString();

Categories