I need to pass a value from one activity to another. I want the passed value should appear in an EditText. But when i run the following code it gives me both the EditText as empty.
MainActivity
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(MainActivity.this,second.class);
i.putExtra("title", t);
i.putExtra("descrip", s);
startActivity(i);
MainActivity.this.finish();
}
});
second activity
et1 = (EditText) findViewById(R.id.fulltext);
et2 = (EditText) findViewById(R.id.editText2);
title = getIntent().getExtras().getString(title);
descp = getIntent().getExtras().getString(descp);
et2.setText(title, TextView.BufferType.EDITABLE);
et1.setText(descp, TextView.BufferType.EDITABLE);
Suggest a fix. Thanks in advance.
STEP-1: <Eg:: From Activity1>
Pass the telefone into a new activity with the help of intents
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("title",telefone);
i.putExtra("descrip",telefone);
startActivity(i);
STEP-2: <Eg:: From Activity2>
Receive the string passed in another activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
String title= extras.getString("title");
String descrip= extras.getString("descrip");
}
Hope it helps ! ..........Let me know if you need more info
Try this
title = getIntent().getStringExtra("title");
descp = getIntent().getStringExtra("descrip");
Related
I'm trying to make an app that makes a call based off two editText calls.
The instances are editText1 and editText2. Once filled out, they click add pin to phone. I am able to get it to go to the phone call but only:
*215*null*null
shows up.
So, my question is what am I doing wrong if I want to store whats typed in edit Text1 to be stored in firstNumber and editText2 to be stored in secondNumber?
Furthermore, how do I get the # character to display?
So, the result of what I want is:
*215*firstNumber*secondNumber#
to be displayed on phone call.
public class Main2Activity extends AppCompatActivity {
EditText firstNumber;
EditText secondNumber;
Button btnAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
firstNumber = (EditText)findViewById(R.id.editText2);
secondNumber = (EditText)findViewById(R.id.editText3);
btnAdd = (Button)findViewById(R.id.button5);
Toast.makeText(Main2Activity.this,
"before the call", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:*215*" + firstNumber + "*" + secondNumber + "#" ));
startActivity(intent);
} catch (Exception e) {
}
}
});
}
}
Now that We have it complete using this method `
public void onClick(View v {
try{
EditText et = (EditText) findViewById(R.id.editText2);
EditText et2 = (EditText) findViewById(R.id.editText5);
String text= et.getEditableText().toString();
String text2 =et2.getEditableText().toString();
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:*215*" + text + "*" + text2 + "#"));
startActivity(intent);
} catch (Exception e) {
}
}
`What i want done is add the Pawn sign or the hash tag at the end of output. For example
*215*text*text2 followed by the hashtag or pawn sign #
but it totally just tosses it out the window after text2.
so at the moment what i get is *215*text*text2 what i want is *215*text*text2#
Thank you so much. I took a different route and it worked this way. everything else was the same from the question.
thanks again
public void onClick(View v) {
try {
EditText et = (EditText) findViewById(R.id.editText2);
EditText et2 = (EditText) findViewById(R.id.editText5);
String text= et.getEditableText().toString();
String text2 =et2.getEditableText().toString();
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:*215*" + text + "*" + text2 + "#"));
startActivity(intent);
} catch (Exception e) {
}
}
To get the actual text in EditText, you need to use:
firstNumber.getText().toString();
secondNumber.getText().toString();
EDIT: Use the Uri.encode() method to show the # symbol as well
String phoneNumber = firstNumber.getText().toString() + secondNumber.getText().toString() + "#";
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:"+ Uri.encode(phoneNumber)));
startActivity(callIntent);
I have menu Menu 1 containing 20 buttons bt1, bt2,..btn when you click a button should go to the main activity Main1 where a title and a text should be displayed. Main1 is extended to Text1 where the titles and texts are.
What's needed is that when you click on button2 for example the Main1 should display Title2 and Text2 and so on.
I did this in the Menu 1:
#Override
public void onClick(View v) {
//do common code here
Bundle bundle1 = new Bundle();
bundle1.putString("somekey1", act1);
Intent i = new Intent(getApplicationContext(), Main1.class);
i.putExtras(bundle1);
startActivity(i);
str= v.getResources().getResourceName(v.getId());
act1= Integer.toString(Integer.parseInt(str.substring(str.indexOf("bt")+2 )));
in the Main1 I did this:
num = Integer.parseInt(getIntent().getExtras().getString("somekey1"));
stringId1 = getResources().getIdentifier("title"+num, "string", getPackageName());
stringId2 = getResources().getIdentifier("text"+num, "string", getPackageName());
if (stringId1 > 0) {
title=getString(stringId1);
text2=getString(stringId2);
}
in the Text1 I did this:
public class Text extends Activity {
public String
title1="some title",
text1="some text",
title2="some title",
text2="some text",
titl3="333",
tex3="kar3",
title4="xxxxx",
text4="xxxxx",
title5="",
text5="",
But all of that doesn't work, and about to shake my head on the wall, as the bundle doesn't transfer the data, and stringId1 = getResources().getIdentifier("title"+num, "string", getPackageName());
also returns 0.
Help please`
Pass the intent as shown below:
Intent intent = new Intent(getApplicationContext(),Main1.class);
intent.putExtra("Key1", "Value1");
intent.putExtra("Key2", "Value2");
startActivity(intent);
And in the oncreate() of your Main1.class :
Intent dataIntent = getIntent();
String value1 = dataIntent.getStringExtra("Key1");
String value2 = dataIntent.getStringExtra("Key2");
Please try the following
#Override
public void onClick(View v) {
//do common code here
Bundle bundle1 = new Bundle();
str= v.getResources().getResourceName(v.getId());
act1= Integer.toString(Integer.parseInt(str.substring(str.indexOf("bt")+2 )));
bundle1.putString("somekey1", act1);
Intent i = new Intent(getApplicationContext(), Main1.class);
i.putExtras(bundle1);
startActivity(i);
I am trying to modify an Android app to suit my need.
The original app has page 1 to display list of notes, and page 2 displays the detailed note.
What I'm trying to achieve is instead of having only 1 textbox in the detailed note page, I want it to have several textboxes, and persist it as well.
Here is how I thought it would be (but failed miserably of course).
This the transmitter on the page 2 (detailed page) activity:
private void saveAndFinish()
{
EditText et = (EditText) findViewById(R.id.eventTitle);
String eventTitle = et.getText().toString();
EditText et2 = (EditText) findViewById(R.id.eventDate);
String eventDate = et2.getText().toString();
EditText et3 = (EditText) findViewById(R.id.eventVenue);
String eventVenue = et3.getText().toString();
EditText et4 = (EditText) findViewById(R.id.eventLocation);
String eventLocation = et4.getText().toString();
EditText et5 = (EditText) findViewById(R.id.eventNote);
String eventNote = et5.getText().toString();
EditText et6 = (EditText) findViewById(R.id.eventAttendees);
String eventAttendees = et6.getText().toString();
Intent intent = new Intent();
// pass these to the main activity will ya?
intent.putExtra("key", data.getKey());
intent.putExtra("title", eventTitle); // eventTitle is the edited text!
intent.putExtra("date", eventDate);
intent.putExtra("venue", eventVenue);
intent.putExtra("location", eventLocation);
intent.putExtra("note", eventNote);
intent.putExtra("attendees", eventAttendees);
setResult(RESULT_OK, intent);
// work done, go back to calling activity
finish();
}
This the receiver on the page 1 (main page) activity:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode==DETAIL_ACTIVITY_REQ && resultCode==RESULT_OK)
{
DataItem event = new DataItem();
event.setKey(data.getStringExtra("key"));
event.setTitle(data.getStringExtra("title"));
event.setDate(data.getStringExtra("date"));
event.setVenue(data.getStringExtra("venue"));
event.setLocation(data.getStringExtra("location"));
event.setNote(data.getStringExtra("note"));
event.setAttendees(data.getStringExtra("attendees"));
datasource.update(event);
refreshDisplay();
}
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
// which list view item was selected?
DataItem data = eventsList.get(position);
//now, to navigate. which class should i go to next?
Intent intent = new Intent(this, DetailedActivity.class);
// and also, pass data to the next activity will ya?
intent.putExtra("key", data.getKey());
intent.putExtra("title", data.getTitle());
intent.putExtra("date", data.getDate());
intent.putExtra("venue", data.getVenue());
intent.putExtra("location", data.getLocation());
intent.putExtra("note", data.getNote());
intent.putExtra("attendees", data.getAttendees());
// go go go!
startActivityForResult(intent, DETAIL_ACTIVITY_REQ);
}
Apparently only the "title" is saved, everything else is not. Help?
modify your code in saveAndFinish method
Intent intent = new Intent(thisActivityname.this,secondActivityname.class);
startActivity(intent);
Above code is put data and send to second activity.
now get above data in second activity
Intent intent = getIntent();
String title = intent.getStringExtra("title");
String date = intent.getStringExtra("date");
String venue = intent.getStringExtra("venue");
String location = intent.getStringExtra("location");
String note = intent.getStringExtra("note");
String attendees = intent.getStringExtra("attendees");
You must use getIntent() to get the data from Intent:
data = getIntent();
use on ActivityResult:
data = getIntent();
I have an EditView where the user is supposed to input a phone number. I then want to take that phone number and call/text it in another activity. How could I do that?
This is part of my Main Activity:
EditText editText = (EditText) findViewById(R.id.editText1);
editText.setOnClickListener(this);
}
public void onClick(View src) {
Intent serviceIntent = new Intent(this, MyService.class);
switch (src.getId()) {
case R.id.button1:
Log.d(TAG, "onClick: starting service");
EditText editText = (EditText) findViewById(R.id.editText1);
if (editText.getText().toString()==""){
Toast.makeText(getApplicationContext(), "Please enter a phone number", Toast.LENGTH_SHORT).show();
} else {
pnumber = editText.getText().toString();
startService(serviceIntent);
serviceIntent.putExtra(number, pnumber);
break;
}
}
}
}
And then in my other Activity:
Bundle extras = this.getIntent().getExtras(); {
if(extras !=null)
{
String newnumber = extras.getString(number,pnumber);
Uri number = Uri.parse(newnumber);
Intent callIntent = new Intent(Intent.ACTION_CALL, number);
Intent textIntent = new Intent(Intent.ACTION_SENDTO, number);
}
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.button2:
//Log.d(TAG, "onClick: starting call");
startActivity(callIntent);
break;
case R.id.button3:
//Log.d(TAG, "onClick: start text message");
startActivity(textIntent);
break;
}
}
}
The Uri keeps parsing "newnumber" and giving a phone number made from the numbers that have those letters (For example, I would get 639686237 since n is on the 6 key, e is on the 3 key etc.) It also doesn't recognize the variables number and pnumber.
You can send your data in two activities like that:
Intent intent = new Intent(CurrentActivity.this, MyService.class);
intent.putExtra("number", pnumber);
startActivity(intent);
And you can get this value in MyService.class:
Intent intent = getIntent();
String yourNumber = intent.getStringExtra("number");
Or your can put your values in bundle:
In your current activity:
Bundle bundle = new Bundle();
bundle.putString("number", pnumber);
bundle.putStirng("name", "yourNameValue");
bundle.putInt("age", "ageValue");
Intent intent = new Intent(CurrentActivity.this, MyService.class);
intent.putExtras(bundle);
startActivity(intent);
And in your MyService activity:
Bundle bundle = getIntent().getExtras();
String number = bundle.getString("number");
String name = bundle.getString("name");
int age = bundle.getInt("age");
I hope that helps you.
Due to intent.putExtra(String name, Bundle extra) statement in first activity, to get the pnumber in second activity we requires the string name. There is no scope of number (name in your case) and pnumber variables in the second class.
So instantiate a variable having the value of number in the second class ValueOfNumber, this will serve as the key to get pnumber from the intent.
Use
pnumber=extras.getString(ValueOfNumber); //ValueOfNumber is the value of number variable in first activity.
Also call startService after Intent.putExtras.
Refer to This answer
Used SharedPreferences. There'a nice thread here for it. With it, you can save the number in a Preference file (visible only for the current application and not in the file system). Like this one:
private final String PREFS_NAME = "savefile";
private final String KEY_NUMBER = "num";
String strWithTheNumber = "00 123 456 789"
String strSavedValue = "";
SharedPreferences sharedPreferences = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
//To put the phone in the file
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(KEY_NUMBER, strWithTheNumber);
editor.commit();
//To retrieve the number
strSavedValue = sharedPreferences.getString("num", null);
I have a way that I know is not the best way of sending them over rite now but I'm sending them as strings and converting them to an Int on the reciver side, the problem is when I do the conversion it crashes on my phone. This is what I have on my sender side:
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, PayTracker.class);
// hourly wage send
EditText editText = (EditText) findViewById(R.id.hourly_wage);
String message1 = editText.getText().toString();
intent.putExtra(MESSAGE_1, message1);
// ot wage send
EditText editText1 = (EditText) findViewById(R.id.ot_wage);
String message2 = editText1.getText().toString();
intent.putExtra(MESSAGE_2, message2);
// hours per day send
EditText editText2 = (EditText) findViewById(R.id.hours_day);
String message3 = editText2.getText().toString();
intent.putExtra(MESSAGE_3, message3);
// start new activity
startActivity(intent);
And this is what is on my reciving side:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_tracker);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Receive messages from options page
Intent intent = getIntent();
String message1 = intent.getStringExtra(Options.MESSAGE_1);
String message2 = intent.getStringExtra(Options.MESSAGE_2);
String message3 = intent.getStringExtra(Options.MESSAGE_3);
// convert string to integer
int HW = Integer.valueOf(message1);
int OTW = Integer.valueOf(message2);
int HPD = Integer.valueOf(message3);
Ive tested everything and its the conversion that is causing the app to crash, i was hoping somebody could help me make it not crash or give me a whole new way sending an int to my second activity insted of sending a string and converting it.
Thank you!
=======================================================================
Here is my new code with all your help!
Sending:
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, PayTracker.class);
// Gather text from text boxes
EditText editText = (EditText) findViewById(R.id.hourly_wage);
EditText editText1 = (EditText) findViewById(R.id.ot_wage);
EditText editText2 = (EditText) findViewById(R.id.hours_day);
//Create String from text
String message1 = editText.getText().toString();
String message2 = editText1.getText().toString();
String message3 = editText2.getText().toString();
//Convert String to Int
int HW = 0, OTW = 0, HPD = 0;
try{
HW = Integer.valueOf(message1);
OTW = Integer.valueOf(message2);
HPD = Integer.valueOf(message3);
}
catch(NumberFormatException nfe){
//do something else here
//for e.g. initializing default values to your int variables
}
// Send Integers to PayTracker.java
intent.putExtra(MESSAGE_HW, HW);
intent.putExtra(MESSAGE_OTW, OTW);
intent.putExtra(MESSAGE_HPD, HPD);
// start new activity
startActivity(intent);
}
Receiving side:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_tracker);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Receive messages from options page
Intent intent = getIntent();
int HW = intent.getIntExtra(Options.MESSAGE_HW, 0);
int OTW = intent.getIntExtra(Options.MESSAGE_OTW, 0);
int HPD = intent.getIntExtra(Options.MESSAGE_HPD, 0);
// set textview
TextView textView = (TextView) this.findViewById(R.id.yourpay);
textView.setText(String.valueOf(HW));
}
You don't need to pass Integers as Strings to be converted back in the receiving Activity. Intents can hold Integers as well as Strings.
Simply add your data like you normally would:
int foo = 5;
Intent intent = new Intent(this, bar.class);
intent.putExtra("foobar", foo);
And then retrieve your int from the intent in the receiving Activity as follows.
Intent intent = getIntent();
int foo = intent.getIntExtra("foobar", 0);
Intents can hold more data than just Strings. In fact, take a look at the documentation. You can see that Intents can hold Longs, Doubles, Floats, Parcelables, etc.
Pass data from one activity to another activity "String" value.useing intent
Activityone
Intent intent = new Intent(Activityone.this,Activitytwo.class)
intent.putExtra("TYPE", type);
startActivity(intent);
Activitytwo
Intent intent =getIntent();
Receivevalue =intent.getExtras().getString("TYPE");
(OR)
Receivevalue = getIntent().getExtras().getString("TYPE");
Pass data from one activity to another activity "Integer" value.useing intent
Activityone
Intent intent = new Intent(Activityone.this,Activitytwo.class)
intent.putExtra("TYPE", type);
startActivity(intent);
Activitytwo
Intent intent = getIntent();
value = intent.getIntExtra("TYPE", 0);
// Type = intent.getIntExtra(name, defaultValue);
Try something like this on the send side:
String message1 = editText.getText().toString();
intent.putExtra("MESSAGE_1", message1);
String message2 = editText1.getText().toString();
intent.putExtra("MESSAGE_2", message2);
String message3 = editText2.getText().toString();
intent.putExtra("MESSAGE_3", message3);
Receive side:
if (getIntent() != null) {
if (getIntent().getExtras() != null) {
String mess1 = getIntent().getExtras().getString("MESSAGE_1");
String mess2 = getIntent().getExtras().getString("MESSAGE_2");
String mess3 = getIntent().getExtras().getString("MESSAGE_3");
}
}
You can achieve your goal using following code
In Your Calling Activity
Intent value = new Intent(YourCallingActivity.this,DestinationActivity.class);
value.putExtra("integerone", integeronevalue);
value.putExtra("integertwo", integertwovalue);
value.putExtra("integerthree", integertwovalue);
startActivity(value);
In Destination Activity
int integerone = getIntent().getExtras().getInt("integerone");
int integertwo = getIntent().getExtras().getInt("integertwo");
int integerthree = getIntent().getExtras().getInt("integerthree");
Hope it helps
Encapsulate the conversion part within try/catch block, because your strings may not be convertible into integer values.
int HW, OTW, HPD;
try{
HW = Integer.valueOf(message1);
OTW = Integer.valueOf(message2);
HPD = Integer.valueOf(message3);
}
catch(NumberFormatException nfe){
//do something else here
//for e.g. initializing default values to your int variables
}
*Or more appropriately, pass the integer values in your sending part and receive them as it is.