I'm trying to create clickable textviews in Android without success, in layout android I've the correct output but not linkable.
Here's what I have right now:
<TextView
android:id="#+id/textTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:clickable="true"
android:textSize="16sp"
android:textStyle="bold" />
textTitle.setText(Html.fromHtml(response.toString()));
textTitle.setMovementMethod(LinkMovementMethod.getInstance());
In
Log.i("myApp1", response.toString());
I've
<a href=http://...>MyLink</a>
You just should register a listener to this TextView
findViewbyId(R.id.textTitle).setOnClickListener(new OnClickListener{
#Override
protected void onClick(View view){
String link = (TextView)view.getText().toString();
/* redirect to URL here for example: */
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
startActivity(intent);
}
});
Or:
Add android:autoLink="all" property to your TextView and set its text a HTML A element. for example:
<TextView
android:text="#string/mylink"
android:autoLink="all"/>
And in strings.xml:
<string name="mylink"><a href='http://www.google.com'>http://www.google.com</a></string>
XML view is absolutely correct, make sure you have pragmatically handled the onClickListener properly.
Related
Click on text view is not working
My xml is
<TextView
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_weight="3"
android:gravity="center"
android:textColor="#color/white"
android:text="click to download sevenstepstocollege"
android:textSize="15dp"
android:onClick="downloadLink"
android:clickable="true">
</TextView>
and My Activity code is
public void downloadLink(View v)
{
//String requestId = PurchasingManager.initiatePurchaseRequest(skuKye);
//String requestId=PurchasingManager.initiateItemDataRequest("DeveloperSKU-1234");
skuSet.add(skuKye);
final String requestId = PurchasingManager.initiateItemDataRequest(skuSet);
}
But it is not working.I am not able to click that link.please guide me
Well, I use the following code to make a TextView clickable.
First, add this at your activity.xml, to make TextView clikable:
<TextView
android:id=android:id="#+id/button2" <!-- id to get this TextView -->
(...)
android:onClick="onClick" <!-- Add the function onClick() -->
android:clickable="true" <!-- Set the boolean clickable to true -->
/>
Then, at your MainActivity.java, you add:
private TextView textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the TextView by id
textview = (TextView) findViewById(R.id.button2);
textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO when clicked on your link(TextView)
}
});
}
As I see now that you are trying to make a link from the TextView clickable, not just be able to click on the TextView I will let a link below to a similar question solved in Stack Overflow that you might find helpful.
The android developer reference to TextView:
https://developer.android.com/reference/android/widget/TextView.html
The similar question in Stack Overflow about how to make a links in a clickable that might be helpful:
How do I make links in a TextView clickable?
You may use like this
<TextView
android:id="#+id/topPaid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:clickable="true"
android:text="#string/topPaid"
android:textColor="#000000"
android:textColorLink="#33CC33" />
and At activity
TextView topPaid = (TextView) findViewById(R.id.topPaid);
Linkify.addLinks(topPaid, Linkify.ALL);
topPaid.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//write ur logic
}
}
TextView textview = (TextView) findViewById(R.id.text);
textview.setText(Html.fromHtml("<b>Text:</b> Text with a " + "link " + "Created in the Java source code using HTML."));
XML Code
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
JAVA Code
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(Html.fromHtml("google "));
textView.setMovementMethod(LinkMovementMethod.getInstance());
In your XML as the following:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="onTextViewPressed"
/>
In your attached activity
public void onTextViewPressed(View view) {
...
}
I would like to write a text in my activity like
Buy my app
I need to make this text clickable. So once one click on it, it opens the Market app and my app shows up.
What is the appropriate view? How can I add the web link?
Would you please help (provide a small code)?
Use this code to achieve your goal.
public void onCreate(Bundle request)
{
super.onCreate(request);
setContentView(R.layout.market);
Button buy = (Button)findViewById(R.id.btnBuyMyApp);
buy.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnBuyMyApp:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=your package name"));
startActivity(intent);
break;
}
}
In xml you can use this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="5dip" >
<Button
android:id="#+id/btnBuyMyApp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center"
android:padding="6dip"
android:text="Buy"
android:textColor="#F8B334"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
I hope it helps
Write following line of code in onClick() method
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=YourPackageName"));
startActivity(intent);
This will open default AndroidMarket application in android mobile and shows your application
You can simply use a TextView in your layout xml file with the text "But my app".
Find that TextView in the android code using the findViewById(). Then set a onClickListener() on the TextView. In the onClickListener() use the following code
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.rovio.angrybirds&hl=en")););
I have just used the angry birds app example, instead of that you can give the link of your own app.
Please use below code.
TextView myWebSite = (TextView)findViewById(R.id.myWebSite)
myWebSite.setText("Link is:- " + "market://details?id=your package name");
Linkify.addLinks(myWebSite , Linkify.WEB_URLS);
And please see below link for more information.
Android – Creating links using linkify
Check this:
<TextView
android:id="#+id/click"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Market"/>
in Activity:
TextView text=(TextView) findViewById(R.id.click);
text.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse("https://play.google.com"));
startActivity(intent);
}
});
Dont forget to include internet permission in manifest file.
I have three textviews and applied clickevent on them, but when i click on any of them the they cause Forceclose error in application. i have also tried changing ids textviews and then clean project and ran the project still that error is not removed.
My XML code is
for one textview
<LinearLayout
android:id="#+id/LL_fb"
android:layout_width="180px"
android:layout_height="27px"
android:layout_above="#+id/txt_msg_regs"
android:layout_alignLeft="#+id/LL_signup"
android:layout_marginBottom="25dp"
android:background="#drawable/facebook" >
<TextView
android:id="#+id/btn_txt_fb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect with facebook"
android:layout_gravity="center_vertical"
android:layout_marginLeft="23dp"
android:layout_marginTop="1dp"
android:clickable="true"
android:textColor="#FFFFFF" />
</LinearLayout>
for second textview
<LinearLayout
android:id="#+id/LL_signup"
android:layout_width="180px"
android:layout_height="29px"
android:layout_above="#+id/textView1"
android:layout_alignLeft="#+id/LL_login"
android:background="#drawable/lmyic_signup">
<TextView
android:id="#+id/btn_txt_sinup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="signup with email"
android:layout_gravity="center_vertical"
android:layout_marginLeft="25dp"
android:layout_marginTop="1dp"
android:clickable="true"
android:textColor="#FFFFFF" />
</LinearLayout>
for third one
<LinearLayout
android:id="#+id/LL_login"
android:layout_width="180px"
android:layout_height="29px"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="69dp"
android:background="#drawable/lmyic_login">
<TextView
android:id="#+id/btn_txt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log in"
android:layout_gravity="center_vertical"
android:layout_marginLeft="70dp"
android:layout_marginTop="1dp"
android:textColor="#FFFFFF"
android:clickable="true"/>
</LinearLayout>
this is my android code. This class name in intent are also correct and i verified them.
TextView img_login;
img_login = (TextView)findViewById(R.id.btn_txt_login);
img_login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == img_login.getId())
{
Intent i_lin = new Intent(LockmeifyoucanActivity.this,lmiyc_login.class);
startActivity(i_lin);
}
}
});
Please Tell me what is wrong with this. If logcat is needed then ask me for it....
Please post the complete XMl so that we can view the btn_txt_login in that.
It looks it is button because "findViewById(R.id.btn_txt_login);" would return null if this "btn_txt_login" id is not in xml.
you have some problem with textview in lmiyc_login activity.in lmiyc_login corrent line number 46 which maybe:
TextView txtview;
txtview = (TextView)yourlayout.findViewById(R.id.txtviewid);
findViewById(R.id.btn_txt_login);
Is this really a TextView or do you have a Button in your layout as well? I think you're trying to cast a Button to a TextView
Wrong resource ID...
img_login = (TextView)findViewById(R.id.btn_txt_login);
according to your XML should be
img_login = (TextView)findViewById(R.id.txt_login);
etc..
change ur code with this
TextView img_login;
img_login = (TextView)findViewById(R.id.txt_login);
img_login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId().equals(img_login.getId()))
{
Intent i_lin = new Intent(LockmeifyoucanActivity.this,lmiyc_login.class);
startActivity(i_lin);
}
}
});
To resolve this error quickly, set one break point at line:
View view = findViewById(R.id.btn_txt_login);
Then, see on locals tab which component is, looking at gen/R.java.
these are lines from 42-48
setContentView(R.layout.lmyic_login_page);
txtBack = (TextView)findViewById(R.id.ImgViewTxtBack); txtBack.setTypeface(null,Typeface.BOLD);
iv_login = (ImageView)findViewById(R.id.Txtlogin);
iv_login.setOnClickListener(this);
iv_sign_up = (ImageView)findViewById(R.id.TxtSignup);
I think problem is with iv_login = (ImageView)findViewById(R.id.Txtlogin); – Dheeresh Singh 1 min ago edit
because this is 46 line and it should be textview and you are castin it in Imageview – Dheeresh Singh just now edit
if you have duplicate resource Id, that will throw ClasscastException. So make sure you don't have duplicate element id throughout the whole project.
I want to put links on a TextView, i've put android:autoLink="web"
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text_prix"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:autoLink="web"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
and when i tap on the link i have the browser that it is opened correctly. But i want to dispaly on the textview a TAG for every link. How can i make this ?
Example:
For the link www.google.fr I want to jsut show on the ListView "Google".
Is that possible?
Thank you for your help.
You have to use Linkify !
http://developer.android.com/reference/android/text/util/Linkify.html
Or you can add Onclick action to your textview
Example :
Textview text;
text = (Textview)findViewById(R.id.textview1);
text.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
I know this is so easy (doh...) but I am looking for a way to run a method on tapping or clicking a TextView line of text in an Android App.
I keep thinking about button listeners and anonymous method listener calls, but it just does not seem to apply to TextView.
Can someone point me at some code snippet to show how clicking or tapping on a piece of text in a TextView runs a method?
You can set the click handler in xml with these attribute:
android:onClick="onClick"
android:clickable="true"
Don't forget the clickable attribute, without it, the click handler isn't called.
main.xml
...
<TextView
android:id="#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:textSize="55sp"
android:onClick="onClick"
android:clickable="true"/>
...
MyActivity.java
public class MyActivity extends Activity {
public void onClick(View v) {
...
}
}
This may not be quite what you are looking for but this is what worked for what I'm doing. All of this is after my onCreate:
boilingpointK = (TextView) findViewById(R.id.boilingpointK);
boilingpointK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ("Boiling Point K".equals(boilingpointK.getText().toString()))
boilingpointK.setText("2792");
else if ("2792".equals(boilingpointK.getText().toString()))
boilingpointK.setText("Boiling Point K");
}
});
OK I have answered my own question (but is it the best way?)
This is how to run a method when you click or tap on some text in a TextView:
package com.textviewy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class TextyView extends Activity implements OnClickListener {
TextView t ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t = (TextView)findViewById(R.id.TextView01);
t.setOnClickListener(this);
}
public void onClick(View arg0) {
t.setText("My text on click");
}
}
and my main.xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="#+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
<ListView android:id="#+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView>
<LinearLayout android:id="#+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
<TextView android:text="This is my first text"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:textStyle="bold"
android:textSize="28dip"
android:editable = "true"
android:clickable="true"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
from inside an activity that calls a layout and a textview, this click listener works:
setContentView(R.layout.your_layout);
TextView tvGmail = (TextView) findViewById(R.id.tvGmail);
String TAG = "yourLogCatTag";
tvGmail.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View viewIn) {
try {
Log.d(TAG,"GMAIL account selected");
} catch (Exception except) {
Log.e(TAG,"Ooops GMAIL account selection problem "+except.getMessage());
}
}
});
the text view is declared like this (default wizard):
<TextView
android:id="#+id/tvGmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/menu_id_google"
android:textSize="30sp" />
and in the strings.xml file
<string name="menu_id_google">Google ID (Gmail)</string>
Although you can resolve the problem by setting the listener to textview, it's recommended not to. You should use flat button as it is a subclass of Button and it provides many attributes which TextView doesn't.
To use flat button, add style="?android:attr/borderlessButtonStyle" attribute -
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DONE"
style="?android:attr/borderlessButtonStyle"/>
in textView
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:onClick="onClick"
android:clickable="true"
You must also implement View.OnClickListener and in On Click method can use intent
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://youraddress.com"));
startActivity(intent);
I tested this solution works fine.
To click on a piece of the text (not the whole TextView), you can use Html or Linkify (both create links that open urls, though, not a callback in the app).
Linkify
Use a string resource like:
<string name="links">Here is a link: http://www.stackoverflow.com</string>
Then in a textview:
TextView textView = ...
textView.setText(R.string.links);
Linkify.addLinks(textView, Linkify.ALL);
Html
Using Html.fromHtml:
<string name="html">Here you can put html <a href="http://www.stackoverflow.com">Link!</></string>
Then in your textview:
textView.setText(Html.fromHtml(getString(R.string.html)));
You can use TextWatcher for TextView, is more flexible than ClickLinstener (not best or worse, only more one way).
holder.bt_foo_ex.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// code during!
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// code before!
}
#Override
public void afterTextChanged(Editable s) {
// code after!
}
});