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) {
}
});
Related
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.
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.
On click of a button , a check mark icon should be displayed on the leftmost corner of the button, when reclicked on the same button , the check mark icon should disppear. Could some on help me out in this case?
While this is already answered, here's an alternative solution: add a unicode checkmark symbol. There are two of them: \u2713 and \u2714. Just add them to your strings:
<string name="button_label_on">\u2713 on</string>
<string name="button_label_off">off</string>
Of course, you can put this directly into your layout code, too:
<Button
...
android:text="\u2713 on"
/>
You can add an ImageView (lets say tick.png) with visibility Gone, at the left of the Button. And set its visibilty. Here is the code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/iv_tick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:src="#drawable/tick"/>
<Button
android:id="#+id/btn_tick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press"/>
</LinearLayout>
Now, on Button click event you set its visibilty:
Button btn_tick = (Button)findViewById(R.id.btn_tick);
btn_tick.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
ImageView iv_tick = (ImageView)findViewById(R.id.iv_tick);
int visibility = iv_tick.getVisibility();
if(visibility == View.VISIBLE)
{
iv_tick.setVisibility(View.GONE);
}
else
{
iv_tick.setVisibility(View.VISIBLE);
}
}
});
Checkout the CheckBox widget.
I am creating a user form in android. I want to display an edit text box on click of a button. below that button, while simultaneously the contents originally present below that button to move more down.
How can this be done?
If you just want to "display an edit text box on click of a button" why don't you just..
Keep the EditText in your XML layout file for that activity below the Button where you want it..
XML set it's
android:visibility = "gone"
and making instance of that
EditText et=(EditText)findViewById(R.id.thatEditText);
in activity...in your button click event set
et.setVisibility(View.VISIBLE);
Define the view in your layout, then in code, show and hide it with
myView.setVisibility(View.VISIBLE) and myView.setVisibility(View.GONE).
//xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
<EditText
android:id="#+id/edtbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
//Activity
//oncreate
editText.setVisibility(View.GONE);
btn..setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
editText.setVisibility(View.VISIBLE);
}
});
If your layout is relative then addView(yourView, index) doesn't work. Suppose you want to add view after some other control and reference to that control.
e.g.
<RelativeLayout android:id="#+id/templayout">
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"/>
and you want to add edit text control after text View then on button click :
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.templayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.title);
EditText yourEditText = new EditText(this);
relativeLayout.addView(yourEditText, params);
Define your EditText in your xml and hide it. On button click, change its visibility to View.Visible.
YourEditText.setVisibility(View.GONE);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
YourEditText.setVisibility(View.VISIBLE);
}
});