0

I want to show webpage when we click an image in viewpager. Can you please help me in this. Also I want to know if multiple images are there in viewpager so how will I get the respective webpages of them.

Thanks in advance.

**MainActivity.java**  
package com.example.viewimage;  
import android.app.Activity;  
import android.content.Intent;  
import android.net.Uri;  
import android.os.Bundle;  
import android.support.v4.view.ViewPager;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.content.Context;  

public class MainActivity extends Activity {  
@Override  
public void onCreate(Bundle savedInstanceState) {  
  final Context context = this;  
  super.onCreate(savedInstanceState);  
setContentView(R.layout.activity_main);  

ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);  
ImageAdapter adapter = new ImageAdapter(this);  
viewPager.setAdapter(adapter);  
viewPager.setOnClickListener(new OnClickListener() {       
@Override   
public void onClick(View v){  
      // open the desired page  
        Intent browserIntent = new Intent("android.intent.action.VIEW",  

              Uri.parse("http://www.craftsvilla.com/anvi-s-classic-nawabi-earrings-studded-with-white-stones-and-emeralds.html"));  

                startActivity(browserIntent);  

}   
});  
}    
}

1 Answers1

0

If you want to show this webpage inside your app, you would have to add a WebView. Set its visibility to View.GONE. Add OnClickListener to your viewPager. On click set the visibility of the webView to be View.VISIBLE. To go back, override the functionality of 'back' key - and on its click hide the webpage. Or you can open other activity that holds the webView. You can also open the webpage with android's browser.

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri);
startActivity(browserIntent);

Edited : Add to your xml before closing your RelativeLayout

<LinearLayout
        android:id="@+id/cover_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"/>

instead of 'viewPager.setOnClickListener' write (findViewById(R.id.cover_layout))

Alex248
  • 427
  • 4
  • 15
  • Can you provide me a full source code...as i am new in this type of programming. – user2404515 Jun 04 '13 at 09:55
  • see my mainactivity.java file – user2404515 Jun 04 '13 at 11:18
  • -- O ya, I forgot that viewPager has some problems with onClick listener. You can find here some solutions: http://stackoverflow.com/questions/10243690/onclick-on-viewpager-not-triggered. I would put an other transparent view on this one, and then you can set the listener at this view. show me you 'activity_main' file – Alex248 Jun 04 '13 at 12:06
  • http://stackoverflow.com/questions/10243690/onclick-on-viewpager-not-triggered in this example i didn't understand what have used...what will that code do? – user2404515 Jun 04 '13 at 12:29