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
            }
        });
    }
3
  • will you open url in a browser or webview ? Commented Jan 15, 2018 at 7:28
  • I'm trying to open in Browser Buddy :) Commented Jan 15, 2018 at 7:35
  • I have answered the question please check :) Commented Jan 15, 2018 at 7:40

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

7 Comments

In which file I have to use this?
@MCNaveen in adapter u have already made click listener for button
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
Build and Installation successful, But the App keeps crashing after opening. Here is the Logcat : pastebin.com/naAvZ7Z8
@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...
|
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

Comments

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

Comments

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);
         }
     }
 });

2 Comments

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
@MCNaveen sure it would, as you have to startActivity using the context, try my updated answer, also you can have a look here stackoverflow.com/a/32137097/1638739
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.