How can I get and show content from url by pressing a button in android?
The button is in xml, and I need to show display information below.
Button --> information from http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=1
My wrong code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bovalpo);
Button ButtonOne = (Button)findViewById(R.id.btn);
ButtonOne.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent viewIntent =
new Intent("android.intent.action.VIEW",
Uri.parse("http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=1"));
startActivity(viewIntent);
}
});
Your Intent is wrong. Try using:
Intent intent = new intent(Intent.ACTION_VIEW, Uri.parse("http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=1"));
startActivity(intent);
If you want to present information from link in the same view in which you press the Button, then don't use Intent, because it gets you to another view.
What you might want to use is AsyncTask (about) that would get informations via internet in background, and then present it below button, or anywhere else on screen.
To make a web connection use:
url = new URL("http://...");
urlConnection = url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
And to get web data, try code shown in this topic.
Hope this helps.
Related
I'm trying to access the subtitle settings, so I can change them.
But I'm not managing to find a way to access these settings.
Some advice?
You can use an Intent object like this to open Caption Settings:
NOTE: Minimum API level is 19
Intent intent = new Intent(Settings.ACTION_CAPTIONING_SETTINGS);
startActivity(intent);
For example, if you have a button to open Caption Settings on click.
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_CAPTIONING_SETTINGS);
startActivity(intent);
}
});
You can return by pressing back button on device.
I would like to create a charity application where if the users were to press the 'Donate' button, their browser would open up a link to a 3rd-party crowdfunding website where they would donate some amount for that project (or choose to exit the app or whatever!). How do I get to know what amount they have donated (or not!) from the browser so that this data can be used for that member's stats, etc.
Yes it is possible. You can use webView
Button button = (Button) findViewById(R.id.b);
button.setOnClickListener(new View.OnClickLister()
#overide
public void onClick(View v) {
WebView mWebView=(WebView)_findViewById(R.id.webView);
mWebview .loadUrl("url for third party app");
setContentView(mWebview );
});
or you can use intent
Button button = (Button) findViewById(R.id.b);
button.setOnClickListener(new View.OnClickLister()
#overide
public void onClick(View v) {
WString url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
});
I want to be able to switch between my app and twitters app to show a search for a pre-determined hashtag.
I have an on click listener & sending the user to the twitter app... loading the tweets and allowing the discussion of that hashtag. There is the logic but how can I do this? What I get now is just my feed and a drop down list of previous searches.
Here is the listener & intent code:
final Button button = (Button) view.findViewById(R.id.C_Twitter_Button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("twitter://search?f=realtime&q=%23KeyWord"));
startActivity(intent);
}catch (Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://twitter.com/search?f=realtime&q=%23KeyWord")));
}
}
});
Can anyone offer me a solution?
Thanks
The search query was wrong :(
The solution is to use the following url to search the twitter app:
twitter://search?query=%23hashtag
Cheers
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);
}
});