Linkify properly an email address with subject - android

I want to linkify a email address and put a static subject to the email.
In the layout I have this:
<TextView
android:id="#+id/textView5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:autoLink="email"
android:text="#string/email" />
How can I add the subject to the email? I should do this in the Activity with the addLinks() method, am I right? I tried some combinations but the email was being appended to the subject...
I checked those sources but without luck:
Email from a link and Android Intents - bring it home
Android Text Links Using Linkify
Linkify your Text!

I found a solution:
<TextView
android:id="#+id/textView5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:clickable="true"/>
TextView mailTV = (TextView) findViewById(R.id.textView5);
mailTV.setText(Html.fromHtml("<a href=\"mailto:"+getString(R.string.email)+"?subject="+getString(R.string.email_subject)+"\" >"+getString(R.string.email)+"</a>"));
mailTV.setMovementMethod(LinkMovementMethod.getInstance());

Related

Android Textview with clickable email and link

I have textview with clickable email and link. My text has link and email
my xml is:
<TextView
android:id="#+id/description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColorLink="#color/colorPrimeTopBar"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:text="#string/sms_deposit_information"
android:textColor="#color/sms_deposit_text"
android:textSize="16sp"
android:autoLink="email"
app:layout_constraintLeft_toLeftOf="#id/smsNumberField"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/dividerBottom" />
But I have a problem. When I put android:autoLink="email" it shows email and makes it clickable and not link. If I remove it shows link clickable and not email.
What should I do?
Can you try adding attributes as:
android:autoLink="email|web"
android:linksClickable="true"
I think you need to set the android:linksClickable="true" attribute.
If set to false, keeps the movement method from being set to the link movement method even if autoLink causes links to be found.
Set the sms_deposit_information in strings.xml in the form of
<string name="sms_deposit_information"><![CDATA[<p>abc#xyz.com</p>]]></string>
Then do the below programmatically
TextView textView = findViewById(R.id.description);
textView.setText(Html.fromHtml(getString(R.string.sms_deposit_information)));
textView.setMovementMethod(LinkMovementMethod.getInstance());
put a String in xml
<resources>
<string name="test">Support: click here</string>
</resources>
TextView
<TextView
android:layout_width="fill_parent"
android:id="#+id/text"
android:layout_height="wrap_content"
android:autoLink="web"
android:gravity="center"
android:linksClickable="true"
android:text="#string/test" />
EDIT
add this code too:
TextView t2 = (TextView) findViewById(R.id.text);
t2.setMovementMethod(LinkMovementMethod.getInstance());

how to make clickable url link in TextView on android without using java

Here I used clickable URL on Textview and it works. But how can I set clickable and highlighted Text with Open URL with a browser. It's possible from Android XML OR kotlin without using java code like setText(Html.fromHtml("")).
String value = "<html>Visit Web mysite View</html>";
TextView text = (TextView) findViewById(R.id.text);
text.setText(Html.fromHtml(value));
text.setMovementMethod(LinkMovementMethod.getInstance());
<TextView
android:textSize="18sp"
android:autoLink="web"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="http://www.example.com"
tools:ignore="HardcodedText" />
It's No required declear layout id and no code from java side. it's works from xml
Use Autolink
For website android:autoLink="web"
For call android:autoLink="phone"
For email android:autoLink="email"
For map android:autoLink="web"
For all android:autoLink="all"
This property works for you
TextView: android:autoLink="web"
here is an example Layout
layout.xml
<TextView
android:id="#+id/txtLostpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:autoLink="email"
android:gravity="center"
android:padding="20px"
android:text="#string/lostpassword"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/txtDefaultpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:autoLink="web"
android:gravity="center"
android:padding="20px"
android:text="#string/defaultpassword"
android:textAppearance="?android:attr/textAppearanceSmall" />
string.xml
<string name="lostpassword">If you lost your password please contact support#cleverfinger.com.au</string>
<string name="defaultpassword">User Guide http://www.cleverfinger.com.au/user-guide/</string>
Just add this attribute to your TextView android:autoLink="web"
e.g
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:autoLink="web"
android:clickable="true"
android:text="https://www.google.co.in"/>
Add line to the TextView:
`android:autoLink="web"`
Like this
<TextView
................
................
android:autoLink="email"
.................
/>
as #Aakash Verma suggested use android:autoLink="web".
but it will change your textColor. To over-come this also add android:textColorLink="your_text_color" to you TextView.
Your TextView should look like.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="https://www.google.co.in/"
android:textColor="#color/colorPrimary"
android:textColorLink="#color/colorPrimary" />
If you still want to do in in java
myUrl= (TextView) rootView.findViewById(R.id.myUrl);
myUrl.setText("https://www.google.co.in/");
Linkify.addLinks(myUrl, Linkify.WEB_URLS);
myUrl.setLinkTextColor(Color.parseColor("#000000"));
EDIT:
Happy Coding
Use android:autoLink="web" in your TextView's xml. It should automatically convert urls click-able (if found in text)
Also, don't forget android:linksClickable="true". I don't think that you need to set html tag like you did, just the <a href> tag...
In case you are using text from strings.xml resource, it seems that the html tags gets stripped out.
So you have to use <![CDATA[**your string with click links**]]> in the strings.xml to convert it to html using Html.fromHtml(string)

Sending Data From Android Without Email Or Server?

Answer Found:
How to send email in background in Android ?
I'm working on an android app and one of the features requested is the ability to sign up for a news letter, which is simply sending a name and an email address. The client however does not have a server to host an application to receive this information and I would like to do it without sending it as an email. I'd like it just to say "You've successfully signed up for our newsletter" or something along those lines.
Is there anyway I can do this? If so, example code would be appreciated as my background is in C# and I'm working in Java.
Edit: Even sending a hidden e-mail without asking the client to login would be acceptable.
I think the notification method you might be looking for is Toasts. However, the validation of whether the customer has been registered will have to occur elsewhere.
Example:
Toast toast = Toast.makeText(getApplicationContext(), "You've successfully signed up for out newsletter", Toast.LENGTH_LONG);
toast.show();
It looks like you just want to display the message locally without server for demo purpose, right?
Well first of all, I don't really think you should learn Android in C# because of many reasons. First of all, you need Java to do Android development.
Here the simple scenario.
You have one activity called MainActivity where you have two text boxes called name and email.
You will now have a button called submit
Once user enters the name and email address and presses Submit button, it will take the user to a new activity called WelcomeActivity.
In android, you need an xml file that sets up the layout of your activity. I'll call this activity_email.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.maxphone.LoginActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Signup for email"
android:id="#+id/welcomeTextView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="40dp"
android:layout_marginTop="20dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_below="#+id/welcomeTextView"
android:layout_marginTop="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"
android:id="#+id/usrnameTextView"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/userEditText"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:maxLines="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Email Address"
android:id="#+id/emailTextView"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/emailEditText"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:focusableInTouchMode="true"
android:maxLines="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/email_signed_up_smg"
android:id="#+id/loginErrorMsg"
android:layout_gravity="center_horizontal|end"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:singleLine="false"
android:textColor="#ffff0000"
android:visibility="invisible" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/signupConfirmBtn"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="40dp" />
</LinearLayout>
</RelativeLayout>
Now in your MainActivity,
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_login);
TextView txt = (TextView) findViewById(R.id.welcomeTextView);
txt.setTextSize(40);
final EditText usrname = (EditText) findViewById(R.id.userEditText);
final EditText email = (EditText) findViewById(R.id.emailEditText);
final TextView errorMsg = (TextView) findViewById(R.id.emailConfirmMsg);
final Button submitBtn = (Button) findViewById(R.id.emailConfirmBtn);
// Login Up button behavior
submitBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Staring MainActivity
Intent i = new Intent(getApplicationContext(), WelcomeActivity.class);
startActivity(i);
finish();
}
}
}
}
This class will display the Views (such as text views, edit texts, etc) and listen to user behavior. Once submitBtn is clicked, it will create an Intent (please do a research) and take the user to a new activity that intent defined.
and you can do similar work for WelcomeActivity to display welcome messages like Thank you for signing up! and such.
This is all locally done and does not need any kind of web activity. So this is basically for demo purpose.
Good luck!
I found the answer here, unfortunately it requires the hard coding of an e-mail and password which is highly insecure. I will have to make more considerations.
How to send email in background in Android ?

Android - Hyperlink is not clickable

I have a few links in my application. One for a website, one for a phone number, and one for an email. The email and phone links are both working and clickable, however the website hyperlink is still not clickable for some reason. Any thoughts? Code below.
<string name="website" >XXXXXX Website</string>
<string name="email" >sales#XXXXXXX.com</string>
<string name="phone" >P: XXX.XXX.XXXX</string>
<string name="fax" >F: XXX.XXX.XXXX</string>
Above are my strings, and below is the xml file that displays them:
<TextView android:id="#+id/website"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/imageButtonTwitter"
android:gravity="center_horizontal"
android:padding="10dp"
android:autoLink="web"
android:clickable="true"
android:linksClickable="true"
android:text="#string/website" />
<TextView android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/website"
android:gravity="center_horizontal"
android:padding="10dp"
android:autoLink="email"
android:linksClickable="true"
android:text="#string/email" />
<TextView android:id="#+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/email"
android:gravity="center_horizontal"
android:padding="10dp"
android:autoLink="phone"
android:linksClickable="true"
android:text="#string/phone" />
<TextView android:id="#+id/fax"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/phone"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="#string/fax" />
Like I said.. the others are clickable and working. I've tested it on two emulators, as well as my Galaxy S4. Any thoughts why the website is not clickable?
You need to call this on your textview:
TextView tv = (TextView) findViewById(R.id. website);
tv.setMovementMethod(LinkMovementMethod.getInstance());
You need to remove from your TextView:
android:autoLink="web"
android:clickable="true"
android:linksClickable="true"
Add this line -
htmlContent.setMovementMethod(LinkMovementMethod.getInstance());
I'm not very experienced android programmer, but my small remarks which can be helpful (at least I hope):
Don't add those additional parameters to TextView. Just android:text and android:clickable="true" is enough for having html clickable link.
And next when setting text please use:
TextView myTextView = (TextView) findViewById(R.id.myId);
myTextView.setText(Html.fromHtml(htmlText));
More here: android.text.Html
I'm guessing you are missing that Html.fromHtml
I hope it helps.

Problem with ellipsesize

I have a TextView I use to display email addresses to my users
<TextView
android:id="#+id/profile_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dip"
android:textColor="#000000"
android:maxLines="1"
android:ellipsize="end" />
My issue is that many email addresses get cut off in the middle instead of having an ellipse at the end. Here is an example:
If my email address is
short.name#mydomain.com
everything works fine. However, If my email address is
really.longfreakennameforanemailaddressandscreenisntwideenough#mydomain.com
the only thing that will display is
really.
Any ideas?
As a workaround you can use :
android:scrollHorizontally="true"

Categories

Resources