-3

I am having the Adapter and Fragment.Fragment consists of image view .Inside image view click listener.I am trying to call an url.This is my code

public class TabFragment1 extends Fragment {
private List<Stores> storesList = new ArrayList<>();
private RecyclerView recyclerView;
private StoresAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab_fragment_1, container, false);
    ImageView imageView = (ImageView) view.findViewById(R.id.google);
    imageView.setBackgroundResource(R.drawable.animation);
    AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
    frameAnimation.start();
    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    recyclerView.setNestedScrollingEnabled(true);
    mAdapter = new StoresAdapter(storesList);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String url="www.google.com";
            Intent google=new Intent(Intent.ACTION_VIEW);
            google.setData(Uri.parse(url));
            startActivity(google);
        }
    });
    prepareStoresData();
    return view;
}
private void prepareStoresData() {
    Stores stores;
    stores = new Stores("Flipcart", "9 Offers", "Get upto 50% Cashback");
    storesList.add(stores);
    stores = new Stores("Amazon", "9 Offers", "No Cashback");
    storesList.add(stores);
    stores = new Stores("Paytm", "2 Offers", "Get upto 50%Cashback");
    storesList.add(stores);
    stores = new Stores("SnapDeal", "3 Offers", "Get upto 10% Reward Points");
    storesList.add(stores);
    stores = new Stores("Airtel Online Recharge", "2 Offers", "Get upto 0% Cashback");
    storesList.add(stores);
    stores = new Stores("Freecharge", "No Offers", "Get upto 0% Cashback");
    storesList.add(stores);
    stores = new Stores("Ebay", "No Offers", "Get upto 7% Cashback");
    storesList.add(stores);
    stores = new Stores("Shopclues", "No Offers", "Get upto 6% Cashback");
    storesList.add(stores);
    stores = new Stores("Panuval Book Store", "No Offers", "Get upto 5% Cashback");
    storesList.add(stores);
    stores = new Stores("Jabong", "2 Offers", "Get upto 6% Cashback");
    storesList.add(stores);
    stores = new Stores("Redbus", "1 Offers", "No Cashback");
    storesList.add(stores);
    stores = new Stores("FirstCry", "1 Offers", "Get upto ₹20 Cashback");
    storesList.add(stores);
    mAdapter.notifyDataSetChanged();
}

}

I had given my code help me in this.It is Adapter having imageview and it should implement the url pass.

Akila
  • 715
  • 2
  • 8
  • 13
  • imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String url="www.google.com"; Intent google=new Intent(Intent.ACTION_VIEW); google.setData(Uri.parse(url)); startActivity(google); } }); – Akila Jan 18 '17 at 10:35

3 Answers3

2

If you want to open a url in browser after imageview click try this here is the java file.

    public class TestFragment extends Fragment {

        private RecyclerView recyclerView;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.testxml, container, false);
            ImageView imageView = (ImageView) view.findViewById(R.id.google);


            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse("https://www.google.com"));
                    getActivity().startActivity(i);
                }
            });

            return view;
        }


}

Here is my xml

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/google"
    android:src="@drawable/register_kyc_icon_house"
    />

</LinearLayout>

This is the running code.please check.hope this helps.:)

Pratik Gondil
  • 689
  • 1
  • 6
  • 17
0

cant understand what calling a url means? if u mean to open url in browser on imageview click then add this code in your on clicklistener

Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url));
MyActivity.startActivity(intent);

MyActivity is the name of activity which consist your fragment

0

Follow this link

Sending an Intent to browser to open specific URL

 imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String url="https://www.google.com";
            Intent google=new Intent(Intent.ACTION_VIEW);
            google.setData(Uri.parse(url));
            startActivity(google);
        }
    });

url should include- https or http

Thank to All.Hope this helps for others.

Community
  • 1
  • 1
Akila
  • 715
  • 2
  • 8
  • 13