Clickable Text can open the Market app - android

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.

Related

Button - Textview click event issue - Android studio

I have a list of buttons in my activity_main, Once i clicked on Button_A it is taking me to the next list of buttons layout for eg. Button_A1.
And once i clicked Button_A1 it should show me the text which i have written in text_view. But here its getting failed. Once i Clicked in Button_A1, it showing like "your app is stopped". Can you guys help me on this, as i am new on this platform.
**Button_A1.xml:**
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/textView2"
android:layout_gravity="center_vertical"
android:onClick="Button_A1" />
</LinearLayout>
**button_A1.java:**
public class Ov1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ov1);
}
}
Do i have to give activity entry in manifest.xml. Please help me on this..
Button_A ----> Button_A1 ----> Textview
Clicked in Button_A1, it showing like "your app is stopped"
Because Button_A1 method is not available in Ov1 class. add method as:
public void Button_A1(View view) {
////
}
NOTE: To make TextView click-able need to add android:clickable="true" attribute in TextView xml

Clickable TextView in Android\Eclipse

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.

What's wrong with this code of changing background of an imagebutton?

I am trying to change the image of an imagebutton using the following codes. Eclipse crashes but there is no error discovered… I've also used setBackgroundResource instead of setImageResource, but it didn't work. Nothing in Logcat, nither in errors. Please help me to find the right way.
I have two images for this imagebutton. - edit_on & edit_off
xml
Java
imageblockButton = (ImageButton)findViewById(R.id.buttonblock);
imageblockButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imageblockButton.setImageResource(R.drawable.edit_on);
Intent intent1 = new Intent (context, ActivityBlockList.class);
startActivity(intent1);
v.setClickable(true);
}
});
my xml looks like…
<ImageButton android:id="#+id/buttonblock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0sp"
android:scaleType="centerCrop"
android:background="#drawable/edit_off" />
In your xml
<ImageButton android:id="#+id/buttonblock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0sp"
android:scaleType="centerCrop"
android:src="#drawable/edit_off" />
use src instead of background
and
ImageButton btn = (ImageButton)findViewById(R.id.buttonblock);
btn.setImageResource(R.drawable.newimage);
Try this
imageblockButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.edit_on));

How to make a button redirect to another XML layout

I'm making a button in xml(res / layout / activity_home.xml), like this:
<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"
tools:context=".HomeActivity" >
<ImageView
android:id="#+id/imageView1"
android:src="#drawable/schkopwide"
android:contentDescription="#string/HTI"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="78dp"
android:onclick="Intent i = new Intent(activity_store.xml);
startActivity(i);"
android:text="#string/HTI" />
</RelativeLayout>
so what should I add into this xml to let it redirect to another xml page (res / layout / activity_store.xml)?
Thank you
If you Want to show two different layouts in Same Activity, then ViewSwitcher is best layout.
You can add multiple layouts in ViewSwithcher. And Replace them by using viewswitcher.next(); function.
<ViewSwitcher
android:id="#+id/viewswitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- Add Two View’s Here -- >
</ViewSwitcher>
You can take reference from this link: http://abhiandroid.com/ui/viewswitcher
Try out as below:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="78dp"
android:onclick="start"
android:text="#string/HTI" />
In your main activity :
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, ActivityStore.class);
startActivity(i);
}
});
Here is your ActivityStore Class code:
public class ActivityStore extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
}
}
Also add the activity into your mainfest file.
<activity
android:name=".ActivityStore"
android:label="#string/app_name"/ >
You can't add the launch of an Intent inside the onclick parameter in XML. You have to do it by code.
In your code:
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, ActivityStore.class);
startActivity(i);
}
});
And in the OnCreate of the ActivityStore class, put this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
}
NOTE: I supposed that yous activity_store class is called ActivityStore
You need to check on Android documentation :
Activity
OnClickListener
Intent
http://developer.android.com/training/index.html
Good luck.
Try this,
Statically include XML layouts inside other XML layouts. use include. Add the below code in your activity_store.xml
<include layout="#layout/activity_home"/>
Sure you will get solution.
A simple way would be to create an Activity with another xml attached to it and then use intent.

How to put links on a TextView?

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) {
}
});

Categories

Resources