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();
Related
So just to practice I've created a Pythagorean theorem calculator app. I want each answer that I obtain to be stored in another activity(a "history" page). I'm trying to use intents to send/receive the arraylist and items. I get the most recent one, but it is overwritten after each press of the button.
package com.example.hypnotenusecalculatorrebuild;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
//ToDo history button is broken. doesn't display listview when pressed.
public void toHistory(View view) {
// Intent intent = new Intent(getApplicationContext(), HistoryActivity.class);
//
// startActivity(intent);
}
public void findHypno(View view) {
Log.i("Info", "button pressed");
EditText editTextNumberA = (EditText) findViewById(R.id.editTextNumberA);
EditText editTextNumberB = (EditText) findViewById(R.id.editTextNumberB);
TextView textViewAnswer = (TextView) findViewById(R.id.textViewAnswer);
String message;
String cSquared;
if (editTextNumberA.getText().toString().isEmpty() || editTextNumberB.getText().toString().isEmpty()) {
message = "Please enter a number for each dimension";
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
} else {
Number numberA = new Number();
Number numberB = new Number();
Number numberC = new Number();
numberA.number = Double.parseDouble(editTextNumberA.getText().toString());
numberB.number = Double.parseDouble(editTextNumberB.getText().toString());
numberA.squareNumber();
numberB.squareNumber();
numberC.number = numberA.number + numberB.number;
numberC.squareRoot();
Log.i("test numberA", String.valueOf(numberA.number));
Log.i("test numberB", String.valueOf(numberB.number));
Log.i("test numberC", String.valueOf(numberC.number));
cSquared = Double.toString(numberC.number);
textViewAnswer.setText(cSquared);
ArrayList<String> historyList = new ArrayList<String>();
historyList.add("test");
Intent intent = new Intent(getApplicationContext(), HistoryActivity.class);
intent.putExtra("answer", cSquared);
intent.putStringArrayListExtra("arrayList", historyList);
startActivity(intent);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
package com.example.hypnotenusecalculatorrebuild;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import java.util.function.ToDoubleBiFunction;
public class HistoryActivity extends AppCompatActivity {
public void back(View view) {
finish();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
Intent getList = getIntent();
ArrayList<String> historyList = getList.getStringArrayListExtra("arrayList");
historyList.add(getList.getStringExtra("answer"));
ListView listViewHistory = (ListView) findViewById(R.id.listViewHistory);
ArrayAdapter historyListArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, historyList);
listViewHistory.setAdapter(historyListArrayAdapter);
// ToDo - intent isn't working as attended, only saves one item and gets overwritten each time
//Intent historyIntent = getIntent();
// get extra needs a name from put extra
// String testGetIntent = historyIntent.getStringExtra("answer");
// Log.i("testIntentString", testGetIntent);
// historyList.add(historyIntent.getStringExtra("answer"));
}
}
here is a different approach. make a public interface in a separate file named "extensions" or whatever and initialize your arraylist there like so:
public interface ext {
ArrayList<String> myArray = new ArrayList<String>();
}
this interface is created when the app runs and lives for as long as the app lives meaning you can always access and manipulate it anywhere in your app without worrying whether the activity is being killed or not. so you can add or remove elements to or from it wherever you want.
Access the array this way:
ext.myArray.add("new item")
I'm a beginner in android studio and i want to write a code with 3 activities.
1st is for starting the app.2nd is for showing an English word and 3rd is for showing the English word and a description of it in two texts.
I want to transfer data of English word itself and its description and the number of the word to show the words one by one.
I wrote the code with help of tutorial clips but it ain't work and in 3rd activity shows nothing.
these are my codes:
main activity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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 start(View view)
{
Intent i = new Intent(this,ActivityOne.class);
startActivity(i);
}
}
ActivityOne
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.TextView;
public class ActivityOne extends AppCompatActivity {
int a=0;
String E , P;
private Button show;
private TextView Eword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
Eword = (TextView) findViewById(R.id.Eword);
show = (Button) findViewById(R.id.show);
}
public void show(View view)
{
Intent intent = new Intent(this,ActivityTwo.class);
startActivity(intent);
int a = getIntent().getIntExtra("counter",0);
Eword.setText(Eng[a]);
E = Eword.getText().toString();
P = Fa[a];
a++;
intent.putExtra("counter",a);
intent.putExtra("EWord",E);
intent.putExtra("PWord",P);
}
private String[] Eng = {
"Abundance",
"Anxiety",
"Bruxism",
"Discipline",
"Drug Addiction"
};
private String[] Fa = {
"Abundance Description",
"Anxiety Description",
"Bruxism Description",
"Discipline Description",
"Drug Addiction Description"
};
}
ActivityTwo
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class ActivityTwo extends AppCompatActivity {
TextView Eng , Fa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Eng = (TextView) findViewById(R.id.eng);
Fa = (TextView) findViewById(R.id.fa);
// recieve data from activity one
String EWord = getIntent().getStringExtra("EWord");
String PWord = getIntent().getStringExtra("PWord");
int a = getIntent().getIntExtra("counter",0);
Eng.setText(EWord);
Fa.setText(PWord);
}
public void iknow(View view)
{
Intent myIntent = new Intent(this,ActivityOne.class);
startActivity(myIntent);
int a = getIntent().getIntExtra("counter",0);
myIntent.putExtra("counter",a);
}
public void idknow(View view)
{
Intent myIntentTwo = new Intent(this,ActivityOne.class);
startActivity(myIntentTwo);
int a = getIntent().getIntExtra("counter",0);
myIntentTwo.putExtra("counter",a);
}
}
And it shows this result:
It is the third activity and it can clear the pretext that was set in design page but can not replace EWord or PWord
Can someone help me?????
(1) To get you started, you need to put your variables into the intent before you start the activity. (2). I don't think its a good idea to have activity one start two, which then can start one. It is better to just close second activity which then automatically returns to one. (3) To pass data back to the activity you are returning you need to startActivityForResult https://developer.android.com/training/basics/intents/result.html
activity one starts activity two using startActivityForResult. Activity Two returns an intent (which is the result intent) and activity one recieves this in onActivityResult.
check out this: Android: how to make an activity return results to the activity which calls it?
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);
Can I ask if someone knows how to pass the data entered from one activity to another activity? The string to be passed to that activity will be compared to the images in the database of the Android phone..
Here is the code sirs:
AlphabetConversion.java
package com.mobilebasedsignlanguage;
import android.app.Activity;
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;
public class AlphabetConversionMenu extends Activity {
EditText et_conv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alphaconv);
Button alphaConv = (Button)findViewById(R.id.btn_alphaconv);
et_conv = (EditText)findViewById(R.id.et_convtxt);
alphaConv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String checkWord = et_conv.getText().toString();
Bundle word = new Bundle();
word.putString("key", checkWord);
Intent a = new Intent(AlphabetConversionMenu.this, AlphabetCompareClass.class);
a.putExtras(word);
startActivity(a);
}
});
}
}
Here is the java for the second activity
package com.mobilebasedsignlanguage;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class AlphabetCompareClass extends Activity {
String get;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alphabetcompare);
Bundle gotWord = getIntent().getExtras();
get = gotWord.getString("key");
TextView Word = (TextView)findViewById(R.id.textView1);
Word.setText(get);
for(int x = 0; x > gotWord.getString("key").length(); x++){
//if(gotWord.getString("key").charAt(0) == "SELECT * from " + TABLE_CONTACTS)
// Get Image from the Database
// Set it on Image View
// set timer 2 secs
}
};
}
The comments in the second activity I think I don't know how to code that. Really need someones help. Thank you very much.
Did you try to save information on the shared preferences?
Allows you you to save "key-value".
http://developer.android.com/reference/android/content/SharedPreferences.html
try this on AlphabetConversion.java
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String checkWord = et_conv.getText().toString();
Intent a = new Intent(AlphabetConversionMenu.this, AlphabetCompareClass.class);
a.putExtra("key",checkWord);
a.putExtras(word);
startActivity(a);
}
and in the onCreate of second activity
Bundle b;
b = this.getIntent().getExtras();
mssg = b.getString("key");
Select Statement will return the resultset object. If you want to compare it to the specific column then fetch the column name only. For comparing the string objects you can use String.compare method or simple == will be sufficient.
First, you should put it into extras in the first activity:
Intent intent = new Intent(this, AlphabetCompareClass.class);
intent.putExtra("STRING_KEY", yourString);
In the second activity you can take it from extras:
String yourString = getIntent().getExtras().getString("STRING_KEY");
Or you can use shared preferences to store information. It's more convenient if you should save info for more than one activity change.
In your first activity call like this :
Intent a = new Intent(AlphabetConversionMenu.this, AlphabetCompareClass.class);
a.putExtras("key", checkWord);
startActivity(a);
And in second activity get value like this
get = getIntent().getStringExtra("key",""); // here if no extra found then default "" return.
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.