0

I currently have a Recycler View, When the user clicks a button from the view it goes to a new activity. I would like to set the Title in the Actionbar of the new activity to that of the colour in the recycler view. So in my Example below I would like to be able to change the 'London Overground' title to the same color as the Recycler view item. enter image description here

I have an array which holds the RGB color values, which is in my onBindViewHolder method. I'm able to pass the name through, just not the correct color. Please see my code below.

LineAdapter

public class LineAdapter extends RecyclerView.Adapter<LineAdapter.LineViewHolder> {


    private List<Line> lineList;

    public LineAdapter(List<Line> lineList) {
        this.lineList = lineList;
    }

    @Override
    public LineViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.line_list_row, parent, false);

        return new LineViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(LineViewHolder holder, int position) {
        holder.lineName.setText(lineList.get(position).getLineName());
        holder.lineStatus.setText(lineList.get(position).getLineStatus());



        Integer[] colors = {Color.rgb(179, 99, 5),Color.rgb(227, 32, 23),Color.rgb(255, 211, 0),Color.rgb(0, 120, 42),Color.rgb(0, 164, 167),Color.rgb(243, 169, 187),Color.rgb(160, 165, 169),Color.rgb(238, 124, 14),Color.rgb(155, 0, 86),Color.rgb(0, 0, 0),Color.rgb(0, 54, 136),Color.rgb(113, 86, 165),Color.rgb(0, 152, 212),Color.rgb(149, 205, 186)};
        holder.lineName.setBackgroundColor(colors[position]);
    }

    @Override
    public int getItemCount() {
        return lineList.size();
    }

    public class LineViewHolder extends RecyclerView.ViewHolder {
        public TextView lineName;
        public TextView lineStatus;

        public LineViewHolder(View view) {
            super(view);
            lineName = (TextView) view.findViewById(R.id.lineName);
            lineStatus = (TextView) view.findViewById(R.id.lineStatus);
        }
    }
}

My onCLick listener

 recyclerView.addOnItemTouchListener(new touchListener(getApplicationContext(), recyclerView, new clickListener() {
            @Override
            public void onClick(View view, int position) {

              String a = ( lineList.get(position).getLineName());

                // Toast.makeText(line_activity.this, a, Toast.LENGTH_SHORT).show();
               Intent intent = new Intent(line_activity.this, line_info.class);
                intent.putExtra("string", a);
                startActivity(intent);
             }
            }
           )
        );
astric mobiles
  • 468
  • 3
  • 14

2 Answers2

1

Warning: don't create method variable, if you want const array with colors make your array constant using static final int[] colors; Recommandation: don't store colors like this, put them in res/values/colors.xml like

<color name="color1">#FFFFFF</color>

And then use:

context.getColor(R.color.color1);

For using customizable actionbar, in your styles.xml make your AppTheme style's parent .NoActionBar instead of DarkActionBar or what you have now.

In your layout put before everything a toolbar that has match_parent for width and ?attr/actionBarSize for height.

In activity add this right after inflating layout:

Toolbar toolbar = findViewById(R.id.yourToolbarId);
setSupporrActionBar(toolbar);

Then if you want to modify title text color use

toolbar.setBackgroundColor(getColor(R.color.color1));
Dragos Rachieru
  • 602
  • 8
  • 21
0

There is another way to set text color via using Html.fromHtml(). You can customize your text. You may set your text color while using:

Bundle b=getIntent().getExtras();
String str=b.getString("string");
String color=b.getString("color");
getActionBar().setTitle(Html.fromHtml("<font color\""+color+"\">"+str+"</font>"));

This may help set your text color, and it works on almost every text in android. Therefore, you need to record the color of the row in the RecyclerView, then send it via intent.

TravorLZH
  • 302
  • 1
  • 9