I'm new to Android developing programming. I'm trying to pass through multiple information from my starting activity to a new activity and i can't figure out how to retrieve all of the values.
My MainActivity buttonSend event code is this:
public void sendInformation (View view) {
Intent intent = new Intent(this, DisplayInformation.class);
EditText toText = (EditText) findViewById (R.id.toText); //Finds the control 'toText' in the form and gets it by ID
EditText fromText = (EditText) findViewById (R.id.fromText); //Finds the control 'fromText' in the form and gets it by ID
EditText messageText = (EditText) findViewById (R.id.messageText); //Finds the control 'messageText' in the form and gets it by ID
EditText subjectText = (EditText) findViewById (R.id.subjectText); //Finds the control 'subjectText' in the form and gets it by ID
String toMessage = toText.getText().toString(); //Gets the text that you enter for the 'To'Field
String fromMessage = fromText.getText().toString(); //Gets the text that you enter for the 'From' field
String messageMessage = messageText.getText().toString(); //Gets the text that you enter for the 'Message' field
String subjectMessage = subjectText.getText().toString(); //Gets the text that you enter for the 'Subject' field
intent.putExtra(EXTRA_MESSAGE, toMessage);
intent.putExtra(EXTRA_MESSAGE, fromMessage);
intent.putExtra(EXTRA_MESSAGE, messageMessage);
intent.putExtra(EXTRA_MESSAGE, subjectMessage);
startActivity(intent);
}
EXTRA_MESSAGE is defined as public final static String EXTRA_MESSAGE = "com.example.Fun.MESSAGE"; I don't really know what this does to be honest the tutorial just told me to include it.
In my second activity page i have this code for the onCreate instance:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_information);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent intent = getIntent();
String toMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String fromMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String subjectMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String messageMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView toMessagetextView = new TextView(this);
toMessagetextView.setTextSize(40);
toMessagetextView.setText(toMessage);
TextView fromMessagetextView = new TextView(this);
fromMessagetextView.setTextSize(40);
fromMessagetextView.setText(fromMessage);
TextView subjectMessagetextView = new TextView(this);
subjectMessagetextView.setTextSize(40);
subjectMessagetextView.setText(subjectMessage);
TextView messageMessagetextView = new TextView(this);
messageMessagetextView.setTextSize(40);
messageMessagetextView.setText(messageMessage);
setContentView(toMessagetextView);
setContentView(fromMessagetextView);
setContentView(subjectMessagetextView);
setContentView(messageMessagetextView);
}
When I go to run my app it errors out and shuts down.
*Also if anybody could tell me how to put the TextView in the fragment.xml file for the second activity it would be greatly appreciated. I know how to declare the id and what not but i don't know to use it with retrieving data from another activity yet.
Thank you in advance for any help.
You are using same key EXTRA_MESSAGE for all extra values. Intent stores values like Map.
So here you are loosing all the value other than subjectMessage, because it was added at last.
You have to use different keys for each values. like
public final static String EXTRA_MESSAGE_TO = "com.example.Fun.MESSAGE_TO";
public final static String EXTRA_MESSAGE_FROM = "com.example.Fun.MESSAGE_FROM";
public final static String EXTRA_MESSAGE_SUBJECT = "com.example.Fun.MESSAGE_SUBJECT";
public final static String EXTRA_MESSAGE = "com.example.Fun.MESSAGE";
Related
Newbie android/java dev here. How can I pass custom lists via intent with loop?
The problem is the list of "Restaurants" is displayed in the firstactivity.
A "Details/Menu" button under the restaurant name will be clicked and I want to display the "Food Menu" and other relevant information of that specific restaurant via array.
It should be passed from firstactivity to the secondactivity with a loop (I think), so that it will use just few textviews.
The button should pass these data according to the Restaurant clicked.
Name of the Restaurant
URL of the restaurant drawable profile photo
URL of the restaurant drawable cover photo
The Title of the Food Category (*PROBLEM)
The lists of food in that specific category and its price (*PROBLEM)
My question is, how to pass the data automatically and how to make this code better and short? Thank you so much for the help!
Here is the sample of firstactivity.
First Activity Screenshot
Here is the java code of First Activity:
public void bluecafedetails (View dineview) {
Intent intent = new Intent(DineActivity.this, DineDetailsActivity.class);
String TitleofDine = "Blue Cafe";
String AddressofDine = "2nd Floor, VTC, Gogon, Virac, Catanduanes";
String OpenHoursofDine = "8AM - 10PM / Everyday";
String DineDelivery = "Yes";
String MenuTitle = "All Day Breakfast";
intent.putExtra("DineTitle", TitleofDine);
intent.putExtra("DineAddress", AddressofDine);
intent.putExtra("DineOpenHours", OpenHoursofDine);
intent.putExtra("DineDelivery", DineDelivery);
intent.putExtra("MenuTitle", MenuTitle);
intent.putExtra("image",R.drawable.blue_cafe_logo);
this.startActivity(intent);
}
Here is the sample of secondactivity.
Second Activity Screenshot
Here is the java code in the second activity so far:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dine_details);
TextView dineTitle = (TextView)findViewById(R.id.dinetitle);
TextView dineAddress = (TextView)findViewById(R.id.dineaddress);
TextView dineDelivery = (TextView)findViewById(R.id.acceptsdeliveryanswer);
TextView dineOpenHours = (TextView)findViewById(R.id.openhours);
TextView dineMenuTitle = (TextView)findViewById(R.id.menutitle);
Intent anotherActivityIntent = getIntent();
Bundle extras = getIntent().getExtras();
String DineTitle= anotherActivityIntent.getStringExtra("DineTitle");
String DineAddress= anotherActivityIntent.getStringExtra("DineAddress");
String DineOpenHours= anotherActivityIntent.getStringExtra("DineOpenHours");
String DineDelivery= anotherActivityIntent.getStringExtra("DineDelivery");
String MenuTitle= anotherActivityIntent.getStringExtra("MenuTitle");
int imageId = getIntent().getIntExtra("image", 0);
ImageView imageView = (ImageView) findViewById(R.id.dinelogo);
imageView.setImageResource(imageId);
dineTitle.setText(DineTitle);
dineAddress.setText(DineAddress);
dineDelivery.setText(DineDelivery);
dineOpenHours.setText(DineOpenHours);
dineMenuTitle.setText(MenuTitle);
this.setTitle(DineTitle);
I am trying to send data from one activity to another and also to show toast when the button is pressed I am trying to accomplish both at same time.The id of the button I have assigned is add_to_cart and I tried this code below and i didnt work
Button customToastButton = (Button) this.findViewById(R.id.add_to_cart);
customToastButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//get the LayoutInflater and inflate the custom_toast layout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.cart_toast, (ViewGroup)
findViewById(R.id.toast_layout_root));
//get the TextView from the custom_toast layout
TextView text = (TextView) layout.findViewById(R.id.toastText);
text.setText("Item as been added to cart");
//create the toast object, set display duration,
//set the view as layout that's inflated above and then call show()
Toast t = new Toast(getApplicationContext());
t.setDuration(Toast.LENGTH_SHORT);
t.setView(layout);
t.show();
Bundle intent = getIntent().getExtras();
if (intent != null) {
firstname= intent.getString("Gladwin");
secondname= intent.getString("james");
}
}
});
The toast part works fine but not the data sending part and I tried Implementing separately this code to send the data but was not responsive and below is the code I am using to receive the data sent
private String firstname;
private String secondname;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
TextView txtFirst=(TextView)findViewById(R.id.text1);
txtFirst.setText(firstname);
TextView txtSecond=(TextView)findViewById(R.id.textview1);
txtSecond.setText(secondname);
and this is the XML part of the receiving end
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text=""
android:textSize="20sp" />
<TextView
android:layout_below="#+id/text1"
android:id="#+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text=""
android:textSize="20sp" />
I know there a are lot of newbie mistakes sorry for that tho
So, let´s assume you have opened the Activity that should receive the intent before. The problem here is, that if you leaving this activity and does not finish it, it´s still alive as long as the system needs not much resources. So, then you are going to the next activity where you send the intent and start th receiving activity again. But here is the trick: the activity just comes back to screen, but had never died. So the intent is not received. Do it this way
Sending the intent:
public class SendingActivity extends Activity {
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
Intent testIntent = new Intent(SendingActivity.this, ReceivingActivity.class);
testIntent.putExtra("firstname","Eggsy");
testIntent.putExtra("lastname","Iggsy");
startActivity(testIntent);
}
});
}
Receiving intent in the activity and finish after leaving:
public class ReceivingActivity extends Activity {
//make TextView objects
private TextView txtFirst;
private TextView txtSecond;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activty_xml);
//initialize textViews <-- this has to be done with all views
txtFirst = (TextView)findViewById(R.id.yourFirstTextViewId);
txtSecond = (TextView)findViewById(R.id.yourSecondTextViewId);
Intent intent = getIntent();
String firstName = intent.getStringExtra("firstname");
if(firstName!=null){
txtFirst.setText(firstName);
}
String lastName = intent.getStringExtra("lastname");
if(lastName!=null){
txtSecond.setText(lastName);
}
}
#Override
onBackPressed(){
finish();
super.onBackPressed();
}
}
From updated question, I think you have a general problem with understanding the order of activities lifecycle. OnCreate() will be called on start of activity and the views are set. The String variables that you set to the textView will not changed automatically, you´ll have to do it in your button click method. So, above example should be added to your onClick if you don´t want to do it at the start directly:
#Override
public void onClick(View v){
Intent intent = getIntent();
String firstName = intent.getStringExtra("firstname");
if(firstName!=null){
txtFirst.setText(firstName);
}
String lastName = intent.getStringExtra("lastname");
if(lastName!=null){
txtSecond.setText(lastName);
}
}
To get data from other activity:
Intent intent = new Intent(currentactivity.this, targetactivity.class);
intent.putExtra("XXX", value);
You need a String value, in this case: text and a string name in this case: uid
Then to get the uid in another activity:
String uid = getIntent().getExtras().getString("XXX");
In data receiving side Do like this....
private String firstname;
private String secondname;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
firstname= bundle.getString("firstname");
secondname= bundle.getString("Secondname");
}
TextView txtFirst=(TextView)findViewById(R.id.text1);
txtFirst.setText(firstname);
TextView txtSecond=(TextView)findViewById(R.id.textview1);
txtSecond.setText(secondname);
I am building an app and on my main activity I have a spinner, now I want the selected value to be send to my other activity when I press a button.
I have done this with succes for a couple of EditText's. But for a spinner it seems a bit tricky.
I am following the documentation from de developer.android.com site but I don't really understand it. link: http://developer.android.com/guide/topics/ui/controls/spinner.html
My code where I fill the spinner with data from an array:
I do this code in the onCreate method:
//supply spinner with the array using an instance of ArrayAdapter
Spinner spinner = (Spinner) findViewById(R.id.timespan_spinner);
//create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.timespan_spinner, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
This is how I send the data drom the EditText's to my other activity:
This is in my sendForm method
public void sendForm(View view) {
//creating an Intent
Intent intent = new Intent(this, DisplayFormActivity.class);
//defining fields
EditText editTextFirstname = (EditText) findViewById(R.id.txtFirstname);
EditText editTextLastname = (EditText) findViewById(R.id.txtLastname);
EditText editTextBedrag = (EditText) findViewById(R.id.txtBedrag);
//getting the field values
String firstname = editTextFirstname.getText().toString();
String lastname = editTextLastname.getText().toString();
String bedrag = editTextBedrag.getText().toString();
//putting data in the intent
intent.putExtra(FIRSTNAME, firstname);
intent.putExtra(LASTNAME, lastname);
intent.putExtra(BEDRAG, bedrag);
startActivity(intent);
}
You can take a look at this answer, quoting:
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
So, for your code:
public void sendForm(View view) {
//creating an Intent
Intent intent = new Intent(this, DisplayFormActivity.class);
//defining fields
EditText editTextFirstname = (EditText) findViewById(R.id.txtFirstname);
EditText editTextLastname = (EditText) findViewById(R.id.txtLastname);
EditText editTextBedrag = (EditText) findViewById(R.id.txtBedrag);
Spinner spinner = (Spinner) findViewById(R.id.timespan_spinner);
//getting the field values
String firstname = editTextFirstname.getText().toString();
String lastname = editTextLastname.getText().toString();
String bedrag = editTextBedrag.getText().toString();
String chosenOption = spinner.getSelectedItem().toString();
//putting data in the intent
intent.putExtra(FIRSTNAME, firstname);
intent.putExtra(LASTNAME, lastname);
intent.putExtra(BEDRAG, bedrag);
intent.putExtra(CHOSEN_OPTION, chosenOption);
startActivity(intent);
}
I have two java classes, HelloAndroidActivity and GetTasks. I want to try to get the text from the Edit Text box from the first activity on clicking the button and get that value in the next activity GetTasks and display it in the text view. My code is as shown:
HelloAndroidActivity
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(new OnClickListener() {
public void onClick (View v) {
Intent i = new Intent(HelloAndroidActivity.this, GetTasks.class);
//i.setClass(HelloAndroidActivity.this, GetTasks.class);
EditText taskname = (EditText) findViewById(R.id.task_name);
String task_name = taskname.getEditableText().toString();
Log.d("Task Name", task_name + "");
i.putExtra("taskname", task_name);
startActivity(i);
}
});
GetTasks
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page_layout);
CharSequence task_name = (CharSequence) findViewById(R.id.task_name);
Log.d("Here", task_name + "");
Intent i2 = getIntent();
taskname = i2.getStringExtra("taskname");
TextView text = (TextView) findViewById(R.id.gettaskname);
text.setText(taskname);
}
Can you tell me what am I doing wrong? My application force closes itself. Instead of passing a variable, if I pass a string variable, I am able to see that in the text view? Does it have to do with the manifest file? I have an intent for both the activities. Any help regarding this would appreciated.
I believe that the line
CharSequence task_name = (CharSequence) findViewById(R.id.task_name);
is the most likely culprit. CharSequence should replaced with whatever type of a view the task_name element is...
Instead of (CharSequence), use (EditText), that is the type of layout object your are retrieving the data from, as defined your layout XML file.
In your GetTasks.onCreate method, you need to bring in the values that you passed from the intent in the HelloAndroidActivity.
You do that like this:
Bundle extras = getIntent().getExtras();
if (extras ==null) { return;}
String taskname = extras.getString("taskname");
See the following link for a good tutorial on using intents
I have an application that I am trying to get to got to a specific part of a website. When the user enter the text inside of the EditText and hits the Button I want the application to take that string and enter it into the specific part of the url that I want it to go into.
First Activity
private EditText text;
private Button search;
text = (EditText) findViewById (R.id.text);
search = (Button) findViewById (R.id.Search);
search.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent intent = new Intent (FirstActivity.this,SecondActivity.class);
intent.putExtra("string", text.getText().toString());
startActivity(intent);
}
});
Second Activity
WebView mWebView;
private EditText string;
private String string1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
string = (EditText) findViewById (R.id.string);
string.(getIntent().getExtras().getString("string"));
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.example.com/"+string);
}
}
Any help will be appreciated thanks.
You're almost there.
string1 = getIntent().getExtras().getString("string", "");
Later edit: the name of the String variable is string1, not string. You have to change your last line, too:
mWebView.loadUrl("http://www.example.com/"+string1);
Consider using less confusing, more descriptive variable names. It will help a lot on the long run.