CountryCodePicker Full Number Support to EditText - android

I'm trying to use country code using CountryCodePicker library
https://github.com/hbb20/CountryCodePickerProject
And when i choose country from the list, i want that country code to display in EditText.
I dont know in which method to pass it.
Simple question, don't be angry :)
else {
String code = ccp.getSelectedCountryCodeWithPlus();
String number = edtPhone.getText().toString().trim();
String phoneNumber = "+"+ code + number;
Intent verify = new Intent (getActivity(), Verify.class);
verify.putExtra("phonenumber", phoneNumber);
verify.putExtra("phone", edtPhone.getText().toString());
verify.putExtra("name",edtName.getText().toString());
verify.putExtra("password",edtPassword.getText().toString());
verify.putExtra("edtSecureCode",edtSecureCode.getText().toString());
startActivity(verify);
}
It should pass EditText number to a second activity (full number included with country code, this is for firebase phone authentication, so it needs to know right number)

If I understand correctly, you want to set country code to your edittext; try to set yourEditText like in your code;
else {
String code = ccp.getSelectedCountryCodeWithPlus();
=> yourEditText.setText(""+code);
String number = edtPhone.getText().toString().trim();
String phoneNumber = "+"+ code + number;
Intent verify = new Intent (getActivity(), Verify.class);
verify.putExtra("phonenumber", phoneNumber);
verify.putExtra("phone", edtPhone.getText().toString());
verify.putExtra("name",edtName.getText().toString());
verify.putExtra("password",edtPassword.getText().toString());
verify.putExtra("edtSecureCode",edtSecureCode.getText().toString());
startActivity(verify);
}

String code = codepicker.getSelectedCountryCodeWithPlus();
// Although it is written with plus but it has only the country code except the plus.
// So, we need to add the "+" manually
editText.setText(""+code); // displays the code in the edit text
String number = phoneNo.getText().toString().trim(); //phoneNo that the user entered
String phoneNumber = "+" + code + number; // complete phone number
Intent verify = new Intent (getActivity(), Verify.class);
verify.putExtra("phonenumber", phoneNumber);
verify.putExtra("phone", edtPhone.getText().toString());
verify.putExtra("name",edtName.getText().toString());
verify.putExtra("password",edtPassword.getText().toString());
verify.putExtra("edtSecureCode",edtSecureCode.getText().toString());
startActivity(verify);
finish();
}

Related

Is it possible to share multiple text by using share intent?

I intend to share 6 text information, however it always shows the last information only, i.e.
share.putExtra(Intent.EXTRA_TEXT, Contact);
I also tried to use a string array to store all 6 information, i.e:
share.putExtra(Intent.EXTRA_TEXT, stringArray);
However, it still doesn't work. Can anyone help ? Thank you.
My code:
public class SingleJobActivity extends Activity {
// JSON node keys
private static final String TAG_POSTNAME = "PostName";
private static final String TAG_LOCATION = "Location";
private static final String TAG_SALARY = "Salary";
private static final String TAG_RESPONSIBILITY = "Responsibility";
private static final String TAG_COMPANY = "Company";
private static final String TAG_CONTACT = "Contact";
String PostName;
String Location;
String Salary;
String Responsibility;
String Company;
String Contact;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_job_json_parsing);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
PostName = in.getStringExtra(TAG_POSTNAME);
Location = in.getStringExtra(TAG_LOCATION);
Salary = in.getStringExtra(TAG_SALARY);
Responsibility = in.getStringExtra(TAG_RESPONSIBILITY);
Company = in.getStringExtra(TAG_COMPANY);
Contact = in.getStringExtra(TAG_CONTACT);
// Displaying all values on the screen
TextView lblPostName = (TextView) findViewById(R.id.PostName_label);
TextView lblLocation = (TextView) findViewById(R.id.Location_label);
TextView lblSalary = (TextView) findViewById(R.id.Salary_label);
TextView lblResponsibility = (TextView) findViewById(R.id.Responsibility_label);
TextView lblCompany = (TextView) findViewById(R.id.Company_label);
TextView lblContact = (TextView) findViewById(R.id.Contact_label);
lblPostName.setText(PostName);
lblLocation.setText(Location);
lblSalary.setText(Salary);
lblResponsibility.setText(Responsibility);
lblCompany.setText(Company);
lblContact.setText(Contact);
// listeners of our button
View.OnClickListener handler = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.share:
shareTextUrl();
break;
}
}
};
// our button
findViewById(R.id.share).setOnClickListener(handler);
}
// Method to share either text or URL.
private void shareTextUrl() {
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
// Add data to the intent, the receiving app will decide
// what to do with it.
share.putExtra(Intent.EXTRA_SUBJECT, "Job Information:");
share.putExtra(Intent.EXTRA_TEXT, PostName);
share.putExtra(Intent.EXTRA_TEXT, Location);
share.putExtra(Intent.EXTRA_TEXT, Salary);
share.putExtra(Intent.EXTRA_TEXT, Responsibility);
share.putExtra(Intent.EXTRA_TEXT, Company);
share.putExtra(Intent.EXTRA_TEXT, Contact);
startActivity(Intent.createChooser(share, "Share via"));
}
}
Could anyone help ?
Concatenate the six strings into one larger string, and share that larger string.
You can Concatenate the individual strings to larger string, and can get it
Intent iin = getIntent();
Bundle b = iin.getExtras();
if (b != null) {
String j = (String) b.get("name");
JSONObject name = new JSONObject(j);
Textv.setText(name.getString("postName"));
TextvDesc.setText(name.getString("location"));
}
What's wrong with your actual code
Let's first understand why you only get the last piece of data.
Your problem is by convention in Java (and this also applies to the android.content.Intent.html#putExtra(String, String[]) method you are using) the methods "putXXX" replace the actual value (if it exists) with the new one you are passing.
This is similar to java.util.Map.html#put(K, V) method.
First possible solution
For your current code to work, you would have needed to use a different key for your extra data each time, that is, something like that:
share.putExtra(SingleJobActivity.EXTRA_TEXT_NAME, PostName);
share.putExtra(SingleJobActivity.EXTRA_TEXT_LOCATION, Location);
share.putExtra(SingleJobActivity.EXTRA_TEXT_SALARY, Salary);
share.putExtra(SingleJobActivity.EXTRA_TEXT_RESPONSIBILITY, Responsibility);
share.putExtra(SingleJobActivity.EXTRA_TEXT_COMPANY, Company);
share.putExtra(SingleJobActivity.EXTRA_TEXT_CONTACT, Contact);
This would work fine (assuming you declare as public static final the keys used, and you respect the Android contract for extra data keys, such as using the full package name for the key (e.g. public static final EXTRA_TEXT_NAME = "com.yourpackage.EXTRA_DATA_NAME";).
Second possible solution
Another way of doing it is to pass one extra with a String[] (see method documentation).
String[] extraParams = new String[6];
extraParams[0] = PostName;
extraParams[1] = Location;
extraParams[2] = Salary;
extraParams[3] = Responsibility;
extraParams[4] = Company;
extraParams[5] = Contact;
share.putExtra(SingleJobActivity.EXTRA_TEXT, extraParams);
Then in your new activity you retrieve this array using android.content.Intent.html#getStringArrayExtra(String) method.
Intent intent = getIntent();
String[] extraParams = intent.getStringArrayExtra(SingleJobActivity.EXTRA_TEXT);

Cannot assign variable value to a string from extra.getString();

New to android, I'm currently trying to send a String value from one Activity to another. I have looked through several threads like How to use putExtra() and getExtra() for string data for an answer, but I cannot get it to work.
The string I want to send is:
public void golf(View view) {
Intent intent = new Intent(SearchSport.this, EventList.class);
intent.putExtra("type", "golf");
startActivity(intent);
and my receiver looks like
String type;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_list);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if ( extras != null)
type = extras.getString("type");
if (type == "golf"){
TextView eventName = (TextView) findViewById(R.id.EOName);
TextView eventTime = (TextView) findViewById(R.id.EOTime);
TextView eventLocation = (TextView) findViewById(R.id.EOLocation);
DatabaseOperations dop = new DatabaseOperations(ctx);
Cursor CR = dop.getInformation(1);
CR.moveToFirst();
eventName.setText(CR.getString(1));
eventTime.setText(CR.getString(7) + " " + CR.getString(8) + ". " + CR.getString(9) + " kl. " + CR.getString(10) + ":" + CR.getString(11));
eventLocation.setText(CR.getString(4));}
else {Toast toast = Toast.makeText(this, "Error in Type.", Toast.LENGTH_LONG);
toast.show();}
I have no Idea what's wrong here. The app displays the toast everytime I test it, instead of filling out the TextViews with the data from my database entry.
EDIT: Code has been changed based on answers, but the problem has not yet been solved. Writing if (type.equals("golf")){} instead of if (type == "golf"){} crashed the app.
EDIT 2: Problem solved! Fixed case sensitivity, used .equals instead of ==, and wrote the receiver as
if(getIntent().hasExtra("Type"))
type = getIntent().getStringExtra("Type");
In the end, it turns out is was the virtual device I used which crashed the app, for as of yet unknown reasons. When tested on an actual android phone, the app works as intented.
Thanks to everyone for their help!
try this
if(getIntent().hasExtra("Type"))
type = getIntent().getStringExtra("Type");
Change the key it is case sensitive :
type = extras.getString("Type");
Key is wrong when try to get String from Intent Extra :
type = extras.getString("type");
Replace with :
type = extras.getString("Type");
Note : Also use equals() instead == for comparing String
if (type.equals("golf")){
1) First keys are case sensitive
2) pass as
Bundle bundle = new Bundle()
bundle.putString("type","golf");
intent.putExtras(bundle);
after that get it using
Bundle received = i.getExtras();
received.getString("type");
As you are passing the value using Extra. You must have to catch that using getStringExtra(); and the Key must be case sensitive.
Intent i = getIntent();
type = i.getStringExtra("Type");

Phone number appending some unknown digit when I am going to call a phone

I am trying to call a phone number programatically, but number is appending some unknown digits like "2255" or "011". My phone number format is : (###) ###-####. I just dont know how to solve this issue, or may be I dont know phone number formatting, can anybody help me please.
To start a call:
// Requires the CALL_PHONE permission (it really starts a call).
final Intent tnt = new Intent(Intent.ACTION_CALL);
tnt.setData(Uri.parse("tel:" + Number));
tnt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(tnt);
OR (without starting the call)
/*
Doesn't require the CALL_PHONE permission.
It doesn't start the call, but brings you to the dialer window.
*/
final Intent tnt = new Intent(Intent.ACTION_DIAL);
tnt.setData(Uri.parse("tel:" + Number));
tnt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(tnt);
Number is a string like
String Number = "+493210210";
To filter a dirty number, use a filtering function, something like this:
private final String Filter(String Number)
{
String result = Number;
result = result.replace("#", "");
result = result.replace("(", "");
result = result.replace(")", "");
result = result.replace("-", "");
result = result.replace("*", "");
result = result.replace(".", "");
result = result.replace("/", "");
result = result.replace("\", "");
result = result.replace(" ", "");
result = result.replace(" ", "");
return result;
}
and use it like:
Number = filter(Number);

Problems with putExtra and putStringArrayList

I'm trying to send some data from one activity to another and it's sorta working but not like I want to work.
Problem 1-Things are getting mixed up. On the Next Activity part of the listitem is going to an incorrect textView and part to the correct textview.
Problem 2- I am only able to list 1 item on the new activity but I want to be able to send multiple listitems. I think the problem lies in combining different types of putExtra request to the same place like I do here.
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
Ant help would be appreciated.
Sending Data to next Activity
final TextView username =(TextView)findViewById(R.id.resultTextView);
String uname = username.getText().toString();
final TextView uplane =(TextView)findViewById(R.id.inputPrice);
String pick = uplane.getText().toString();
final TextView daplane =(TextView)findViewById(R.id.date);
String watch = daplane.getText().toString();
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",(CharSequence)watch)
.putExtra("Card Number",(CharSequence)uname)
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
);
finish();
This is the Next Activity
Intent is = getIntent();
if (is.getCharSequenceExtra("Card Number") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
setmsg.setText(is.getCharSequenceExtra("Card Number"));
}
Intent it = getIntent();
if (it.getCharSequenceExtra("date") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleTime);
setmsg.setText(it.getCharSequenceExtra("date"));
}
Intent id1 = getIntent();
if (id1.getCharSequenceExtra("inputPrice") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleName);
setmsg.setText(id1.getCharSequenceExtra("inputPrice"));
}
ArrayList<String> al= new ArrayList<String>();
al = getIntent().getExtras().getStringArrayList("list");
saleNotes= (TextView) findViewById(R.id.saleNotes);
saleNotes.setText(al.get(0));
Alright, a few things:
First of all you do not need to cast your strings as CharSequence.
Second thing,
Define intent, add your extras and only then call startActivity as below:
Intent intent = new Intent(MenuView1Activity.this,RecordCheckActivity.class);
intent.putExtra("date", watch);
startActivity(intent);
Third, when retrieving the intent create a bundle first as below:
Bundle extras = getIntent().getExtras();
String date = extras.getString("date");
EDIT:
Here is how you convert your entire array list to one single string and add it to your textview.
String listString = "";
for (String s : al)
{
listString += s + "\t"; // use " " for space, "\n" for new line instead of "\t"
}
System.out.println(listString);
saleNotes.setText(listString);
Hope this helps!
Try this, Don't use CharSequence just put string value
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",watch)
.putExtra("Card Number",uname)
.putExtra("inputPrice",pick)
.putStringArrayListExtra("list", listItems)
);
And get like this
Intent is = getIntent();
if (is.getCharSequenceExtra("Card Number") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
setmsg.setText(is.getStringExtra("Card Number"));
}

Intent.EXTRA_SUBJECT multiple values in Android

I want to display multiple values in the text message body, however the following code below display no body message even when the textArray has values. Is there any way of adding values to a body of an email through a loop?
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/html");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Example");
int sizeOfArray = list.size();
String textArray [] = new String[sizeOfArray];
for(int i = 0;sizeOfArray > i;i++)
{
HashMap<String, String> arrayString = list.get(i);
String user = arrayString.get("user");
String book = arrayString.get("book");
textArray[i] = user + " - " + book;
}
sharingIntent.putExtra(Intent.EXTRA_TEXT, textArray);
startActivity(Intent.createChooser(sharingIntent,"Share using"));
}
});
It's difficult to get proper documentation on what Intent Receivers are expecting as extra values, but I'm pretty sure you need to pass a String and not a String[] to putExtra, since the Receiver will anyway end up converting the value to a String, so better to control that.
Thats being said, your implementation of the loop is weird. Do you really have a list of HashMap<String, String>as input ?
I would do :
StringBuffer sb = new StringBuffer();
for(HashMap<String, String> item: list){
String user = item.get("user");
String book = item.get("book");
sb.append(user + " - " + book+", ");
}
String value = sb.substring(0, Math.max(0,sb.length()-2));
Intent.EXTRA_TEXT expects CharSequence according to the documentation:
http://developer.android.com/reference/android/content/Intent.html#EXTRA_TEXT
I would guess as you are passing in an array, the receiving activity doesn't know what to do with it and just skips over it.
Trying joining your array values and passing them in as a String.
String arg = org.apache.commons.lang.StringUtils.join (textArray, '\n');
sharingIntent.putExtra(Intent.EXTRA_TEXT, arg);

Categories

Resources