How to go to web page after clicking on custom banner in app?
I have created a custom banner that 开发者_如何学运维is a simple rectangle picture that sits on the bottom of the first activity screen after the app is opened. It is being displayed as an ImageView with clickable="true". I have a setOnClickListener ready to go. Just need help with the code that actually takes the user to my website when they click on my custom banner. Also, need to know what permission to ask for to cover this. I would assume it is android.permission.INTERNET. Thanks for your help in advance.
Ok, here is the code that ended up working. Thanks guys.
Inside the xml layout file for this activity:
<ImageView
android:src="@drawable/saas_banner"
android:clickable="true"
android:id="@+id/SaasBannerIMG"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
Inside the onCreate method for the activity:
// add a click listener to the SaaS Ad Banner
ImageView img = (ImageView) findViewById(R.id.SaasBannerIMG);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.websitetogoto.com"));
startActivity(intent);
}
});
Do you want it to open up the browser in order to view the webpage?
If you do, then you can try to do an implicit intent.
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yourwebsite.com"));
startActivity(intent);
Put that in your button handler and the browser should load with the webpage.
You just need to set up an intent to start the browser with the required link. See this answer for the details: Sending an Intent to browser to open specific URL .
You do not need a specific permission, since the browsing will be handled by a different app (which does need the INTERNET permission).
This is, of course, unless you want to load the page yourself inside a WebView.
精彩评论