Tuesday, October 8, 2013

[android help] Set a String Array into Bold in Android


Set a String Array into Bold in Android



I have a ListView where I managed to display many texts on it. Some of the texts are formatted as BOLD. In order to make those texts bold, I used Spannable and it works! However, when I scrolled it down and up again, the index[0] goes multiple.


To avoid that scene, I used a holder to hold my TextViews. I managed to solve that problem in scrolling but for some reasons, the textstyle which is BOLD, gone in my ListView. How can I do that?


MainActivity.java



private void populateListView() {

String[] source = { "Lorem ipsum dolor sit amet", "consectetuer adipiscing elit",
"sed diam nonummy nibh " };

final SpannableString out0 = new SpannableString(source[0]);
final SpannableString out2 = new SpannableString(source[2]);

StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);

out0.setSpan(boldSpan, 6, 17, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
out2.setSpan(boldSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

ArrayAdapter adapter = new ArrayAdapter(this,
R.layout.items, // Layout to use (create)
source) { // Items to be displayed

public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);

try {

viewHolder.tv = (TextView) lv_procedures.getChildAt(0)
.findViewById(R.id.lbl_item);
viewHolder.tv_2 = (TextView) lv_procedures.getChildAt(2)
.findViewById(R.id.lbl_item);

viewHolder.tv.setText(out0);
viewHolder.tv_2.setText(out2);

} catch (Exception e) {
e.printStackTrace();
}

return v;
}
};


}


Class ViewHolder



private class ViewHolder {
TextView tv;
TextView tv_2;
}


Read more

stackoverflow.comm



No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...