Issue
I am working on my java code as I want to change the font from normal to bold when I click on the unread button in the toolbar in the mainactivity.
When I try this:
public void Unread(int position) {
notifyItemChanged(position);
}
It will not change the font from normal to bold in the recyclerview for the adapter.
Here is the code for Adapter:
public class InboxAdapter extends RecyclerView.Adapter<InboxAdapter.ViewHolder> {
public InboxAdapter adapter;
public Fragment _fragment;
public RecyclerView recyclerView;
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
// change the font style depending on message read status
applyReadStatus(holder, inbox);
holder.actionView.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
/*GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.parseColor("#e8f0fd"));
gd.setCornerRadius(40);
gd.setStroke(40, Color.parseColor("#e8f0fd"));
holder.layout1.setBackgroundColor(Color.parseColor("#e8f0fd"));
holder.layout1.setBackgroundDrawable(gd);*/
int idx = holder.getAdapterPosition();
String id = mInboxes.get(idx).getId();
String short_name = holder.tvIcon.getText().toString();
String subject1 = mInboxes.get(idx).getSubject();
String from = mInboxes.get(idx).getFrom();
String from_email = mInboxes.get(idx).getEmail();
String datetime = mInboxes.get(idx).getDate();
String isRead = mInboxes.get(idx).getRead();
String isImportant = mInboxes.get(idx).isImportant();
// set the read status
setReadStatus(holder, isRead);
openEmailActivity(mailbox, id, idx, color, short_name, subject1, from, from_email, datetime, isImportant);
/* gd.setColor(Color.parseColor("#FFFFFF"));
gd.setCornerRadius(40);
gd.setStroke(40, Color.parseColor("#FFFFFF"));
holder.layout1.setBackgroundColor(Color.parseColor("#FFFFFF"));
holder.layout1.setBackgroundDrawable(gd);*/
}
});
}
public void applyReadStatus(ViewHolder holder, inbox inbox) {
if (inbox.getRead().equals("read")) {
holder.from.setTypeface(null, Typeface.NORMAL);
holder.subject.setTypeface(null, Typeface.NORMAL);
holder.from.setTextColor(ContextCompat.getColor(mContext, R.color.subject));
holder.subject.setTextColor(ContextCompat.getColor(mContext, R.color.message));
holder.time.setTextColor(ContextCompat.getColor(mContext, R.color.timestamp));
holder.time.setTypeface(null, Typeface.NORMAL);
} else {
holder.from.setTypeface(null, Typeface.BOLD);
holder.subject.setTypeface(null, Typeface.BOLD);
holder.from.setTextColor(ContextCompat.getColor(mContext, R.color.from));
holder.subject.setTextColor(ContextCompat.getColor(mContext, R.color.subject));
holder.time.setTextColor(ContextCompat.getColor(mContext, R.color.timestamp));
holder.time.setTypeface(null, Typeface.BOLD);
}
}
public void Unread(int position) {
notifyItemChanged(position);
}
public void openEmailActivity(String mailbox, String id, int idx, int color, String short_name, String subject1, String from, String from_email, String datetime, String isImportant) {
Intent intent = new Intent(mContext, MainActivity5.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("mailbox", mailbox);
intent.putExtra("id", id);
intent.putExtra("idx", idx);
intent.putExtra("bg_color", color);
intent.putExtra("short_name", short_name);
intent.putExtra("subject", subject1);
intent.putExtra("from_sender", from);
intent.putExtra("from_email", from_email);
intent.putExtra("datetime", datetime);
intent.putExtra("is_important", isImportant);
_fragment.startActivityForResult(intent, MainActivity2.REQ_EMAIL_OPEN);
}
}
Here is the code for InboxFragment:
public class InboxFragment extends Fragment {
public static final int REQ_EMAIL_OPEN = 3456;
private RecyclerView mRecyclerView;
private SwipeRefreshLayout mSwipeLayout;
private InboxAdapter mInboxAdapter;
private List<inbox> mInbox = new ArrayList<>();
private inbox Inbox;
private Call<List<inbox>> call;
private Window window;
private Toolbar toolbar;
private FloatingActionButton fab;
private FloatingActionButton fab1;
private String contacts = null;
private String mailbox = null;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_inbox, container, false);
mRecyclerView = (RecyclerView) root.findViewById(R.id.recyclerView);
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQ_EMAIL_OPEN){
if(resultCode == Activity.RESULT_OK && data!= null){
boolean isEmailDeleted = data.getBooleanExtra("isEmailDeleted",false);
boolean isEmailUnread = data.getBooleanExtra("isEmailUnread",false);
boolean isEmailRead = data.getBooleanExtra("isEmailRead",false);
int emailId = data.getIntExtra("emailId",-1);
if (isEmailDeleted && emailId != -1) {
mInboxAdapter.removeItem(emailId);
}
else if (isEmailUnread && emailId != -1) {
mInboxAdapter.Unread(emailId);
}
else if (isEmailRead && emailId != -1) {
}
}
}
}
}
Can you please show me an example how I can change the font from normal to bold for these textview of from
, subject
and time
??
Solution
You are controlling the text style under this condition, inbox.getRead().equals("read")
, but you are not actually changing the value of your read
value in the inbox object.
What you should do is, you should change the value of read
property of your inbox
object at the particular position, and then call notifyItemChanged(position);
.
You should do something like this,
public void Unread(int position) {
this.list.get(position).setRead("unread");
notifyItemChanged(position);
}
This should change the read
in your inbox
object, and should work for you.
Answered By - Priyansh Kedia