How to get the numeric value from intent extra String - java

I'm using intent to pass the string value to next Activity where I use getStringExtra to get the Value. The value is is User Contact no. in 10 digit which is in numeric value. when I setText in textview then It display the correct format but when I use intent on onclicklistener to open dialpad then instead of no, some random no. appears and in 4 digit also
Here I use to pass string
intent.putExtra(MOBILE, g.getSellermobile());
and I receive it by
tutor_mobile = intent.getStringExtra(MOBILE);
when I set it in textview as
TextView contact = (TextView) findViewById(R.id.tutor_near);
contact.setText(tutor_mobile);
It display correct format eg 9868336847
but when I use intent to open dialpad as
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+String.valueOf(tutor_mobile)));
startActivity(intent);
I'm using below code to get the mobile no.
etMobile = (EditText) findViewById(R.id.user_mobile);
String sellermobile = etMobile.getText().toString();
and In class file I have
package com.nepalpolice.filmyduniya.maincategory;
public class location {
public String sellermobile;
// public Uri uri;
public location( String sellermobile) {
this.sellermobile = sellermobile;
// this.uri = uri;
}
public String getSellermobile() { return sellermobile;}
}
}
Then in activity I have
public static final String MOBILE = "other_mobile";
and
intent.putExtra(MOBILE, g.getSellermobile());
and in next activity I have
tutor_mobile = intent.getStringExtra(MOBILE); and
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+String.valueOf(tutor_mobile)));
startActivity(intent);
Then random no. in 4 digit appears.
Then in activity I have
Please Help.

Your tutor_mobile is a String value, but thr you rearrange it toString:
intent.setData(Uri.parse("tel:"+String.valueOf(tutor_mobile)));
May be it's not a error, but it's strange. And what do you want to display in dialpad?

try {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:" + "string format number"));
startActivity(callIntent);
} catch (Exception e) {
Log.d("Response", e.toString());
}

Related

How Do i Get all the Values When Sending String Values from one Activity to Another in Android Studio 3.4

Please am designing an android app, i want to send (5)String values from one activity to another activity to use in different TextViews, i have tried virtually all the code i could find online on the topic, but i keep getting just one value(the last value i send in the putExtra()). please i am new to Android Studio and will appreciate every help.
I have used the putExtra() to send one data to another activity and it worked perfectly, while trying to do the same with multiple data i keep getting just one of the data sent.
I have also tried using a bundle object, to receive the data from the other(recieving) activity.
I expect getting all this data ( intent.putExtra("surname", "Jerry").
intent.putExtra("middlename", "chris"). intent.putExtra("lastname", "Enema")) in another activity, but i keep getting just "Enema" alone
this is my code;
//in the firstActivity
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String sFirstname = firstname.getText().toString();
String sLastname = lastname.getText().toString();
Intent intent = new Intent(MainActivity.this, ReceiveActivity.class);
intent.putExtra("surname" ,sFirstname);
intent.putExtra("lastname", sLastname);
startActivity(intent);
}
});
//And In the second Activity
firstname = findViewById(R.id.firstname);
lastname = findViewById(R.id.firstname);
Intent intent = getIntent();
Bundle bundle = getIntent().getExtras();
String ssurname = bundle.getString("surname");
String slastname = bundle.getString("lastname");
firstname.setText(ssurname);
lastname.setText(slastname);
Try this.
In First Activity:
String sFirstname = "Tope";
String sLastname = "Adebodun";
Intent theIntent = new Intent(MainActivity.this, ReceivingActivity.class);
theIntent.putExtra("firstname", sFirstname);
theIntent.putExtra("lastname", sLastname);
Then in your second activity, do this in the onCreate method:
Intent intent = getIntent();
String thefirst = (String) intent.getExtras.getString("firstname");
String thelast = (String) intent.getExtras.getString("lastname");
I would prefer not to ue getExtras so you should have two getExtra
Like this :
Intent intent = getIntent();
String ssurname = intent.getExtra("surname");
String slastname = intent.getExtra("lastname");
firstname.setText(ssurname);
lastname.setText(slastname);

getArguments() in Activity Class?

I'm still learning AS & java and recently bought an app code but it has "getArguments()" inside of fragment which I'd like to convert it into activity. Help is appreciated.
This is the code :
String weburl = getArguments().getStringArray(MainActivity.FRAGMENT_DATA)[0];
String data = getArguments().containsKey(LOAD_DATA) ? getArguments().getString(LOAD_DATA) : null;
if (data != null) {
browser.loadDataWithBaseURL(weburl, data, "text/html", "UTF-8", "");
} else {
browser.loadUrl(weburl);
}
How do I write the same code in an activity?
I think what you are looking for is how to send info to an Activity, for that you need Intents
Please verify this info: https://developer.android.com/reference/android/content/Intent
Basically:
public static final String EXTRA_MESSAGE = "extra message";
...
Intent intent = new Intent(this, SecondActivity.class);
String message = "Hello this is an intent";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
To retrieve that data you have to do:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
I invite you to check this out: https://developer.android.com/training/basics/firstapp/starting-activity#java
Intent extras are the activity equivalent of fragment arguments.

Android Studio: Passing variable through another variable doesn't work

I created a Method for my MainActivity to pass a String to my SecondActivity.
public void convertCurrency(View view)
{
intent.putExtra("NO", "My String");
startActivity(intent);
}
But in my SecondActivity in my OnCreate Method
Button back = (Button) findViewById(R.id.back);
TextView t = (TextView) findViewById(R.id.first_text);
Intent intent = new Intent(this, MainActivity.class);
String g = intent.getStringExtra("NO");
t.setText(g);
Nothing happens. But Why?
get Your variable in this way:
String yourVriable = getIntent().getStringExtra("NO")
don't new Your intent
Your are creating new intent.
In onCreate method call
String data = getIntent().getStringExtra(key);
replace Intent intent = new Intent(this, MainActivity.class);
by Intent intent = getIntent();
When you do intent.getStringExtra("NO"); your intent Object is new, so his Extra is empty.
You need to change the code in both the activities . You need to create intent and put your string into it, so your code will be.
public void convertCurrency(View view) {
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("NO","My String");
startActivity(intent);
}
Note that in the intent constructor the first argument is the current activity and the second argument will be the activity that you are starting.So in the second activity you need to recieve the string. Code for that will be,
Button back = (Button) findViewById(R.id.back);
TextView t = (TextView) findViewById(R.id.first_text);
String g = getIntent().getStringExtra("NO")
t.setText(g);
I resolve the problem myself. There occurs an error if you have two Intent objects with the same name even if they are within separate methods.

How do I transfer data from one activity to another in Android?

I want to transfer my "result" data from my first (Main) acitivity to
my Custaddress activity, which has edit texts for customer details, and then this is sent to an email. The email/edit texts work perfectly - but I want to
add in "result.toString" into email body string. How do I transfer "result" to the second activity? I believe its something to do with arg?
Here's my code from first activity..
DecimalFormat decimalFormat = new DecimalFormat(COMMA_SEPERATED);
result.append("\nTotal: £"+decimalFormat.format(totalamount));
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setMessage(result.toString());
alertDialogBuilder.setTitle("YOUR ORDER");
alertDialogBuilder.setPositiveButton("Accept",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
//do what you want to do if user clicks ok
//Intent intent = new Intent(context, Custaddress.class);
// startActivity(intent);
Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
startActivity(custaddress);
}
});
alertDialogBuilder.setNegativeButton("Decline",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//do what you want to do if user clicks cancel.
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
Write it in activity that passing data
Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
custaddress.putExtra("key",value);
startActivity(custaddress);
Write below code in activity that catching data
Intent intent=getIntent();
String mString=intent.getStringExtra("key");
hope this will help you
You should use intent (click here) :
Intent intent = new Intent(getBaseContext(), CustAdrresActivity.class);
intent.putExtra("text", mytext);
startActivity(intent);
You just need to replace your lines:
Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
startActivity(custaddress);
with these three lines:
Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
custaddress.putExtra("result", result.toString());
startActivity(custaddress);
and then, when you open the new activity (in your case the Custaddress Activity), you should do the following to get your result
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("result");
}
You should add Extra in the Intent object which you are passing in the startActivity(intent) method.
Example
String value = "String i want to send to next activity"
Intent intent = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
intent.putExtra("KEY", value);
startActivity(intent);
In the Custaddress.java Activity class you need to get the data from the Bundle object that you get as a parameter in the onCreate(Bundle bundle) method
Bundle extras = getIntent().getExtras();
if (extras != null) {
// get data via the key
String valueFromPreviousActivity = extras.getString("KEY");
if(valueFromPreviousActivity != null){
// do something with the data
}
}
Check out this official doc Starting Another Activity.
Through the below code we can send the values between activities
use the below code in parent activity
Intent myintent=new Intent(Info.this, GraphDiag.class).putExtra("<StringName>", value);
startActivity(myintent);
use the below code in child activity
String s= getIntent().getStringExtra(<StringName>);

Changing GPS for 2 to 1 location

I'm trying to change GPS from 2 locations to 1 location
The code for 2 locations is:
ismi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String CURRENT_LOCATION = "37.967775, 23.720689";
String DESTINATION_LOCATION = "37.925942, 23.938683";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="+ CURRENT_LOCATION +"&daddr="+DESTINATION_LOCATION)); //Added ampersand
startActivity(intent);
}
});
And to change to one location like: http://i.stack.imgur.com/zCvW5.png
I even changed on part but it takes from my location to the destination location
Code:
ismi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String DESTINATION_LOCATION = "37.925942, 23.938683";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=" +"&daddr="+DESTINATION_LOCATION)); //Added ampersand
startActivity(intent);
}
});
Does anyone know the code?
Thanks
To start an Intent that will take you to a single GPS location, use:
String DESTINATION_LOCATION = "37.925942,23.938683";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?q="+DESTINATION_LOCATION));
startActivity(intent);
The URI contains The GPS coordinates as you defined previously. Note that there should be no spaces between latitude and longitude values in your DESTINATION_LOCATION String.
Edit: As response to your first question in your comment, to give a name to the label:
String DESTINATION_LOCATION = "My location name";
String latit = "37.925942";
String longit = "23.938683";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:<"+latit+">,<"+longit+">?q=<"+latit+">,<"+longit+">("+DESTINATION_LOCATION+")")); //name the label
startActivity(intent);

Categories