-1

i want to know how can i change my default text color when i click on text it set default color BLUE and text that i have not click it show me with BLACK color.

Also when i reopen my application it will be show me with Clicked text BLUE and non-Clicked text with BLACK.

I have already add text BlUE color but when i re-open application it reset As it is.

So please tell me how can i do this.

FileName: RecyclerViewAdepter.java

package com.ejobbox.ejobbox;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.ArrayList;

public class RecyclerViewAdapter extends RecyclerView.Adapter{
    private final int VIEW_TYPE_ITEM = 0;
    private final int VIEW_TYPE_LOADING = 1;
    private OnLoadMoreListener onLoadMoreListener;
    private boolean isLoading;
    private Activity activity;
    private ArrayList<Model> dataset;
    private Context mContext;
    private int visibleThreshold = 5;
    private int lastVisibleItem, totalItemCount;

    public RecyclerViewAdapter(RecyclerView recyclerView, ArrayList<Model> mlist, Context context) {
        this.dataset=mlist;
        this.mContext=context;
        this.activity=activity;


        final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                totalItemCount = linearLayoutManager.getItemCount();
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                    if (onLoadMoreListener != null) {
                        onLoadMoreListener.onLoadMore();
                    }
                    isLoading = true;
                }
            }
        });
    }

    public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
        this.onLoadMoreListener = mOnLoadMoreListener;
    }

    @Override
    public int getItemViewType(int position) {
        return dataset.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.postdetails,parent,false);
            return new ImageTypeViewHolder(view);
        } else if (viewType == VIEW_TYPE_LOADING) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemload,parent,false);
            return new LoadingViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ImageTypeViewHolder) {
            final Model object=dataset.get(position);
            final ImageTypeViewHolder imageTypeViewHolder=(ImageTypeViewHolder) holder;
            imageTypeViewHolder.title.setText(object.title);
            imageTypeViewHolder.subtitle.setText(object.subtitle);
            imageTypeViewHolder.link.setText(object.link);
            imageTypeViewHolder.date.setText(object.date);

            // Set On Click Listner
            imageTypeViewHolder.title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    imageTypeViewHolder.title.setTextColor(Color.BLUE);

                    Intent intent=new Intent(mContext, WPPostDetails.class);
                    intent.putExtra("itemPosition",position);

                    intent.putExtra("link",object.link);

                    mContext.startActivity(intent);
                }
            });


        } else if (holder instanceof LoadingViewHolder) {
            LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
            loadingViewHolder.progressBar.setIndeterminate(true);
        }
    }

    public static class ImageTypeViewHolder extends RecyclerView.ViewHolder{
        TextView title,subtitle,link,date;
        ImageView imageView;

        public ImageTypeViewHolder(View itemView){
            super(itemView);
            this.title=(TextView)itemView.findViewById(R.id.title);
            this.link=(TextView)itemView.findViewById(R.id.link);
            this.subtitle=(TextView) itemView.findViewById(R.id.subtitle);
            this.imageView=(ImageView) itemView.findViewById(R.id.icon);
            this.date=(TextView)itemView.findViewById(R.id.date);
        }
    }

    @Override
    public int getItemCount() {
        return dataset == null ? 0 : dataset.size();
    }

    public void setLoaded() {
        isLoading = false;
    }

    private class LoadingViewHolder extends RecyclerView.ViewHolder {
        public ProgressBar progressBar;

        public LoadingViewHolder(View view) {
            super(view);
            progressBar = (ProgressBar) view.findViewById(R.id.progressBar1);
        }
    }


}
ejobbox
  • 103
  • 1
  • 1
  • 9
  • You need to store somewhere which text was already clicked and initialize the colors accordingly on restart. – Henry Jan 16 '18 at 11:18
  • I recommend to use some persistent storage to start which text you have clicked and on opening to check that storage. Not sure what kind of data storage best suits your case though. – Mohammad Tabbara Jan 16 '18 at 11:19
  • if response is coming from server end you can keep a field in response for checking checked items or else if it is in local and static then you can use [SharedPreferences](https://developer.android.com/training/data-storage/shared-preferences.html) to save the position of clicked item and then retrieve it just before setting text color – N.Moudgil Jan 16 '18 at 11:20
  • you can use either shared Preferences , but is you have large no of text whose color to be changed then this is not good way. Another way is you can use SQLite DB. – jitendra purohit Jan 16 '18 at 11:20
  • so please tell me how can i temporary store it. – ejobbox Jan 16 '18 at 11:20

1 Answers1

1

you can take a look at the SharedPreferences provided by android for app development. It allows you to store/edit data about your app that persists for the user. below is a link to a tutorial that has helped me out before:

https://www.journaldev.com/9412/android-shared-preferences-example-tutorial

Karan Shishoo
  • 2,402
  • 2
  • 17
  • 32