How to Change Color and Font on Listview

How to change color and font on ListView

You need to create a CustomListAdapter.

public class CustomListAdapter extends ArrayAdapter <String> {

private Context mContext;
private int id;
private List <String>items ;

public CustomListAdapter(Context context, int textViewResourceId , List<String> list )
{
super(context, textViewResourceId, list);
mContext = context;
id = textViewResourceId;
items = list ;
}

@Override
public View getView(int position, View v, ViewGroup parent)
{
View mView = v ;
if(mView == null){
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = vi.inflate(id, null);
}

TextView text = (TextView) mView.findViewById(R.id.textView);

if(items.get(position) != null )
{
text.setTextColor(Color.WHITE);
text.setText(items.get(position));
text.setBackgroundColor(Color.RED);
int color = Color.argb( 200, 255, 64, 64 );
text.setBackgroundColor( color );

}

return mView;
}

}

The list item looks like this (custom_list.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/>
</LinearLayout>

Use the TextView api's to decorate your text to your liking

and you will be using it like this

listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList);
mListView.setAdapter(listAdapter);

How to change font color or size of Listview item

You can set an adapter and change the background color and item size like below:

lv = findViewById(R.id.listView);
lv.setAdaptater(adapter);
final ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, list) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the current item from ListView
View view = super.getView(position, convertView, parent);

// Set the text color of TextView (ListView Item)
TextView tv = view.findViewById(android.R.id.text1);
// Generate ListView Item using TextView
tv.setTextColor(Color.parseColor("#efefef"));
tv.setTextSize(20);
tv.setAllCaps(true);

// Set a background color for ListView
view.setBackgroundColor(Color.parseColor("#00afd8"));

return view;
}
};
lv.setAdapter(adapter);

You can even alternate background colors.

Android change TextView Font color in a ListView for a condition

Show Us db_msg this layout in that layout there is one TextView just get the name of that and replace with "tvIDFrom_db_msg_layout" this

adapter = new ArrayAdapter<String>(this,R.layout.db_msg,messaggi){
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view1 = super.getView(position, convertView, parent);
if (position % 2 == 0) { //Place the condition where you want to change the item color.
testo = messaggi.get(position);
TextView tvText = (TextView) view1.findViewById(R.id.tvIDFrom_db_msg_layout);
if(testo.substring(0,5).equals("27-09")){

tvText.setTextColor(Color.parseColor("#yourHexCode"));
} else {
//Setting to default color.
tvText.setTextColor(Color.WHITE);
}
return view1;
}
};

How to change the text color of a ListView item?

You will need to create a custom layout for your ListView items where you set your desired textcolor.

In this way, you do not even need a custom-adapter.

e.g. custom_textview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv"
android:textColor="@color/white"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="fill_parent"/>

Then, you can use your layout with the ArrayAdapter:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.custom_textview, aa);
lv.setAdapter(adapter);

How to Change the font color of a listview

You need to somehow gain access to the TextView object that holds your text.

You have a few options:

  • Use a custom adapter
  • Use a custom item layout
  • Go quick and dirty way:

    ListAdapter hoteladapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, Hotels) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    TextView tv = (TextView) view.findViewById(android.R.id.text1);
    tv.setTextColor(Color.RED);

    return view;
    }
    };

Can I change ListVIew Text Color?

Here, do these steps

  • Go to sdk folder \sdk\platforms\android-\data\res\layout
  • Copy simple_list_item_1 and paste it in your projects res\layout folder
  • Now open \res\layout\simple_list_item_1
  • Add color attribute there.
  • then change your

    adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1, country);

    to

    adapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.simple_list_item_1, country);

Android change listview item text color

You can override the getView method of Array adapter and change the color:

ArrayAdapter<String> adapter = 
new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,
myList) {

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);

if (flag == True) {
text.setTextColor(Color.BLACK);
}

return view;
}
};

android change listview font and color

I found out the solution :D

    public class Myadapter extends BaseAdapter {

AssetManager assetManager = getAssets();

LayoutInflater lif;
ImageView sideArrow;
TextView tv;


public Myadapter(Context ctx) {
lif = (LayoutInflater) ctx.getSystemService(LAYOUT_INFLATER_SERVICE);


}

@Override
public int getCount() {

return favarets.size();
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

View vi = convertView;
if (convertView == null)
vi = lif.inflate(R.layout.inflate, null);
sideArrow = (ImageView) vi.findViewById(R.id.imageViewsidemark);


tv = (TextView) vi.findViewById(R.id.textFav);
tv.setText(favarets.get(position));

final Typeface tvFont = Typeface.createFromAsset(assetManager, "OPTIMA.TTF");
tv.setTypeface(tvFont);
tv.setTextColor(Color.BLACK);

return vi;
}

}


Related Topics



Leave a reply



Submit