Asesha George Asesha George - 1 month ago 29
Android Question

Hyperlink in RecyclerView textview

I am fetching data from JSON and displaying in RecyclerView layout. My textview is displaying whole link. For example, my TextView is install and the link is

http://mydomin.com?id=2054656
. In RecyclerView it should show only
install
text and when it is clicked then is should go to URL. I tried below code.

Viewholder.ImageUrlNameView.setText(getDataAdapter1.getImageUrlName());

ImageUrlNameView = (TextView) itemView.findViewById(tvAppLink) ;

ImageUrlNameView.setMovementMethod(LinkMovementMethod.getInstance());


It's working fine but showing whole link instead of text. I hope you understand.

please see my full code after changing textview to button

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;

import java.util.List;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;



public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

Context context;

List<GetDataAdapter> getDataAdapter;

ImageLoader imageLoader1;

public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){

super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);

ViewHolder viewHolder = new ViewHolder(v);

return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {

GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);

imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();

imageLoader1.get(getDataAdapter1.getImageServerUrl(),
ImageLoader.getImageListener(
Viewholder.networkImageView,//Server Image
R.mipmap.ic_launcher,//Before loading server image the default showing image.
android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
)
);

Viewholder.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1);

Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());

Viewholder.ImageUrlNameView.setText(getDataAdapter1.getImageUrlName());

}

@Override
public int getItemCount() {

return getDataAdapter.size();
}

class ViewHolder extends RecyclerView.ViewHolder{

public NetworkImageView networkImageView ;
public TextView ImageTitleNameView;
public Button ImageUrlNameView ;

public ViewHolder(View itemView) {

super(itemView);

networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1) ;
ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item);
//ImageUrlNameView = (Button) itemView.findViewById(R.id.bAppUrl) ;
//ImageUrlNameView.setMovementMethod(LinkMovementMethod.getInstance());
//Linkify.addLinks(ImageUrlNameView, Linkify.WEB_URLS);

itemView.findViewById(R.id.bAppUrl).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "ImageUrlNameView";

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


}


}

}


getting below error

10-27 20:58:43.896 12462-12462/com.android_examples.recyclerviewimagelistview_android_examplescom E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.android_examples.recyclerviewimagelistview_android_examplescom.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:73)
at com.android_examples.recyclerviewimagelistview_android_examplescom.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:29)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5801)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5037)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
10-27 21:03:43.956 12462-12462/? I/Process: Sending signal. PID: 12462 SIG: 9


please check the image what i am trying to get but getting error

enter image description here

Answer

Change the logic of click into onBindViewHolder this way, you have the URL of the current object:

 @Override
        public void onBindViewHolder(ViewHolder Viewholder, int position) {

            GetDataAdapter getDataAdapter1 =  getDataAdapter.get(position);

            imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();

            imageLoader1.get(getDataAdapter1.getImageServerUrl(),
                    ImageLoader.getImageListener(
                            Viewholder.networkImageView,//Server Image
                            R.mipmap.ic_launcher,//Before loading server image the default showing image.
                            android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
                    )
            );

            Viewholder.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1);

            Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());

            Viewholder.ImageUrlNameView.setText("Install");



Viewholder.ImageUrlNameView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String url = getDataAdapter1.getImageUrlName();

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

        }

        @Override
        public int getItemCount() {

            return getDataAdapter.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder{

            public NetworkImageView networkImageView ;
            public TextView ImageTitleNameView;
            public Button ImageUrlNameView ;

            public ViewHolder(View itemView) {

                super(itemView);

                networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1) ;
                ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item);
                ImageUrlNameView = (Button) itemView.findViewById(R.id.bAppUrl) ;
                //ImageUrlNameView.setMovementMethod(LinkMovementMethod.getInstance());
                //Linkify.addLinks(ImageUrlNameView, Linkify.WEB_URLS);




            }


        }