0

I'm fetching JSON feed using Volley, which contains Name, Rating and URL. I have textview on Android which will display Fetched Name, Rating and all. I have a Button but it's showing the URL directly because I set Text as Fetched URL.

Currently, I'm getting this:

I need to Set Text of Button as Register & If the Button is clicked it should open the URL Fetched from the JSON Feed.

Hereby I'm attaching the code

    @Override
    public void onBindViewHolder(CourseViewHolder holder, int position) {

        Course course = courseList.get(position);

        holder.textViewCoursename.setText(course.getCoursename());
        holder.textViewcoursedescshort.setText(course.getCoursedescshort());
        //holder.textViewcourseurl.setText(course.getCourseurl());
        holder.textViewcourserating.setText(course.getCourserating());

        Glide.with(mCtx)
                .load(course.getCourseimg())
                .into(holder.imageView);

        Button button = (Button) button.findViewById(R.id.textViewcourseurl);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                //Do stuff here
            }
        });
    }
MC Naveen
  • 191
  • 2
  • 12

5 Answers5

1

You can use Intent for this task.

//Just put Intent on your button click
button.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(course.getCourseurl()));
        mCtx.startActivity(i);
        //This will open url in browser if you have application in your device.
    }
});

If you want to load url in WebView than check below link

How to load external webpage inside WebView

Valentin Michalak
  • 1,777
  • 1
  • 12
  • 23
Lokesh Desai
  • 2,399
  • 13
  • 26
  • In which file I have to use this? – MC Naveen Jan 15 '18 at 07:41
  • @MCNaveen in adapter u have already made click listener for button – Lokesh Desai Jan 15 '18 at 07:43
  • Tried Replacing my Code with yours.. Now I'm getting the Following Error... Error:(64, 17) error: method startActivity in class ContextCompat cannot be applied to given types; required: Context,Intent,Bundle found: Intent reason: actual and formal argument lists differ in length – MC Naveen Jan 15 '18 at 07:55
  • Build and Installation successful, But the App keeps crashing after opening. Here is the Logcat : https://pastebin.com/naAvZ7Z8 – MC Naveen Jan 15 '18 at 08:05
  • @MCNaveen Button button = (Button) button.findViewById(R.id.textViewcourseurl); this code in your Adapter's ViewHolder as you have done findviewbyid for TextViews. Than use like holder.btn.setOnCliccklistner... – Lokesh Desai Jan 15 '18 at 08:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163174/discussion-between-lokesh-desai-and-mc-naveen). – Lokesh Desai Jan 15 '18 at 08:38
  • Possible to Open that URL by using PutExtra.. I have an Activity called WebActivity.java where I have a WebView. I Tried modifying the code. But It doesn't work. Here is the Modified code ` Intent intent= new Intent(Intent, WebActivity.class); intent.setData(Uri.parse(course.getCourseurl())); intent.putExtra("url", courseurl); mCtx.startActivity(intent);` – MC Naveen Jan 15 '18 at 15:49
0

In your adapter where you have declared button, you need to implement onClick method of button:

Button button = (Button) button.findViewById(R.id.textViewcourseurl);
button.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
        Uri uri = Uri.parse("http://www.google.com"); 
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        context.startActivity(intent);
    }
});

This will help you open the specific url in your available browsers. And if you want to open your url in webview, follow this link. How to open a url in webview on new screen based on button click

Ergin Ersoy
  • 895
  • 7
  • 25
Umair
  • 5,756
  • 15
  • 39
  • 47
0

I need to Set Text of Button as Register & If the Button is clicked it should open the URL Fetched from the JSON Feed

You should load the URL with Intent specifying an action. For example to load the URL:

String url = //get your url from the JSON feed ;

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

Basically, the code above will do the work of finding apps that can load the URL and loading the URL for you.

Do not forget to check if the URL is empty before you pass it to an intent

0

inside your adapter onBindViewHolder() method, it would be like that:

// get the course url
String courserURL = course.getCourseurl();
button.setOnClickListener(new Button.OnClickListener() {
     public void onClick(View v) {
         // make sure the course url value is not empty
         if(!courserURL.isEmpty()){
             // assign an ActionView to run that URL
             Intent i = new Intent(Intent.ACTION_VIEW);
             i.setData(Uri.parse(courserURL));
             holder.button.getContext().startActivity(i);
         }
     }
 });
Muhammed Refaat
  • 8,096
  • 11
  • 76
  • 108
  • Returns an error `Error:(69, 21) error: method startActivity in class ContextCompat cannot be applied to given types; required: Context,Intent,Bundle found: Intent reason: actual and formal argument lists differ in length` – MC Naveen Jan 15 '18 at 08:22
  • @MCNaveen sure it would, as you have to startActivity using the context, try my updated answer, also you can have a look here https://stackoverflow.com/a/32137097/1638739 – Muhammed Refaat Jan 15 '18 at 08:37
0

To change bottom name, you need to use ArrayList<String> to open link of bottom:

Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setData("YourLinkHere);
context.startActivity(browserIntent);
Ergin Ersoy
  • 895
  • 7
  • 25
A.nemati
  • 106
  • 7