i'm still new and totally noob so please carry on with me .
how to open a link when button clicked in android?
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
Edit: i'm using phonegap plugin . thanks
Write this code inside onClick :
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Also add the following line to your manifest.xml because you need to have Internet permission to actually use the internet :
<uses-permission android:name="android.permission.INTERNET" />
Read more about Android Permissions.
To read more about Uri refer this link. Also read how Intents work in Android.
Since you are using PhoneGap, un-comment either of the methods below:
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//super.loadUrl("www.google.com");
// or
//loadUril("www.google.com");
}
});
Related
I am little confused with this code am looking for somenthing similar i have a button that i want it to say rate and when click it takes you to the android market where the user can rate my application, please help
Button bRate = (Button) findViewById(R.id.button7);
bRate.setOnClickListener(new View.OnClickListener() {
//plus the code on the top here is my idea of the code
}
Elipse is saying something is wrong and I understand that iI need to put my market link but the button still doesn't work
I couldn't post it on the same page here is the link for the original question
Use application to rate it on market
You have just add your app package name...
https://play.google.com/store/apps/details?id=your packagename
And call using Intent.ACTION_VIEW
String str ="https://play.google.com/store/apps/details?id=com.zeustechnocrats.quickfoods";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(str)));
This was helpful for me to do the same thing using an image button. here the code I used.
final ImageButton mybutton = (ImageButton) findViewById(R.id.imageButton);
mybutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str ="https://play.google.com/store/apps/details?id=ws.crandell.newspaperpuzzles";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(str)));
}
});
Example code you can use.
Change your_package_name to your.package.name. E.g. com.example.myapp
Java File:
public void ratebutton(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW)
.setData(Uri
.parse("https://play.google.com/store/apps/details?id=your_package_name"));
startActivity(intent);
}
Layout file:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="ratebutton"
android:text="#string/rate"
android:layout_gravity="center_horizontal" />
strings.xml:
<string name="rate">Rate with 5 stars please</string>
I am developing an application for android in which I need to make a call with MMI code in the background of the application. But by default the call application of the android phone is activated and I have to press call button from there. so I want solution for this....
You can try the given simple code this is directly initiating a call to the no provided in code 123456789 and no call button is clicked for this. And yes dont forget to add permission in manifest file:
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Throwable e = null;
Log.e("helloandroid dialing example", "Call failed", e);
}
}});
}
If you would have provided some code than it would be helpful other wise have you declared in your app manifest file about the permission to modify phone state.
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"></uses-permission>
I have a small piece of code which is basically supposed to make a phone call when a button is pushed. I looked it up online and all the sources basically gave the same code. But for some reason this code doesn't work. It makes the app crash but the LogCat doesn't display anything (meaning the log is completely blank). I should also mention that in my manifest file I did add the following permission
<uses-permission android:name = "andriod.permission.CALL_PHONE" />
The code I have is as follows. Any help would be greatly appreciated!
phoneButton.setOnClickListener(new OnClickListener () {
public void onClick(View v) {
try {
final Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:232131232"));
ContactUs.this.startActivity(callIntent);
}catch (ActivityNotFoundException e){
Log.e("Dialing", "call Failed!", e);
}
}
});
You spelt android wrong the second time...
Sounds like you need to add the user-permission for making a phone call. I believe the permission is:
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
in your manifest file.
This is a snippet from an Activity class I am currently testing on my HTC Desire -
okButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + getPhoneNumber()));
startActivity(intent);
}
});
I suggest changing ContactUs.this.startActivity(callIntent); to startActivity(callIntent); and testing it again.
I have a simple app that I'm putting together for my company. I have 4 buttons that I've created but can't seem to get them to link correctly. One button should open our mobile site, another button to call us, another button to map to us, and the final button linked to our "News" site. Any help would be greatly appreciated!
On your buttons, you should set OnClickListener, and to do some required actions you could see the example below:
To Open a Map with Certain Location
mapButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + your-location-geo-address));
startActivity(i);
}
});
To call certain number
callButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + telephone-number));
startActivity(i);
}
});
To open a website
linkButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(website-address));
startActivity(i);
}
});
Change "location-address", "telephone-number", and "website-address" with your own String value.
I hope this helps.
anmustangs answer is very good, but one thing I would like to add for the button you are making for a link to your site, where anmustangs wrote (website-address) instead of just typing in a site, it needs to put formatted correctly. For example, you can use "http://www.google.com" and yes you do need to use the quotation marks I put in there. I know I am years late to this thread but who knows who my post may help.
is it possible to make an imageview link to a web page such that when user taps on the image, it takes them to a web page?
Just add a click listener to the image to open an URL:
ImageView img = (ImageView)findViewById(R.id.foo_bar);
img.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://casidiablo.net"));
startActivity(intent);
}
});
That's truly possible, at onClick handler you need to start an activity with intent specifying the uri. See How to open default browser for example.
another method we can even use is
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent redirect2=new Intent(getApplicationContext(),RedirectAct.class);
startActivity(redirect2);
}
});