I am trying to change my font(color and size) and the back ground on my ListView. I want to change it with code lines not on xml. my list view looks like: the xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="18sp" android:text="@string/hello">
</TextView>
and my code is
public class NewsActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ArrayAdapter listItemAdapter = new ArrayAdapter( this,android.R.layout.simple_list_item_1, v_itemList );
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,ynetList));
View v=getListView() ;
ListView lv = getListView();
what next? please give me an example base on my code
Even better, you do not need to create separate android xml layout for list cell view. You can just use "android.R.layout.simple_list_item_1" if the list only contains textview.
private class ExampleAdapter extends ArrayAdapter<String>{
public ExampleAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
}
@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(0);
return view;
}
If you want to use a color from colors.xml , experiment :
public View getView(int position, View convertView, ViewGroup parent) {
...
View rowView = inflater.inflate(this.rowLayoutID, parent, false);
rowView.setBackgroundColor(rowView.getResources().getColor(R.color.my_bg_color));
TextView title = (TextView) rowView.findViewById(R.id.txtRowTitle);
title.setTextColor(
rowView.getResources().getColor(R.color.my_title_color));
...
}
You can use too:
private static final int bgColor = 0xAAAAFFFF;
public View getView(int position, View convertView, ViewGroup parent) {
...
View rowView = inflater.inflate(this.rowLayoutID, parent, false);
rowView.setBackgroundColor(bgColor);
...
}
If u want to set background of the list then place the image before the < Textview>
< ImageView
android:background="@drawable/image_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
and if u want to change color then put color code on above textbox like this
android:textColor="#ffffff"
use them in Java code like this:
color = getResources().getColor(R.color.mycolor);
The getResources()
method returns the ResourceManager class for the current activity, and getColor()
asks the manager to look up a color given a resource ID
Create a CustomAdapter and in that you have the getView() so there if you want to change the listview background color use this :
v.setBackgroundColor(Color.CYAN);
If you want to change the textColor then do this :
tv.setTextColor(Color.RED);
and for the textSize :
tv.setTextSize(20);
where 'v' is the listview and 'tv' is the textview
You can select a child like
TextView tv = (TextView)lv.getChildAt(0);
tv.setTextColor(Color.RED);
tv.setTextSize(12);
If you just need to change some parameters of the View and the default behavior of ArrayAdapter its OK for you:
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class CustomArrayAdapter<T> extends ArrayAdapter<T> {
public CustomArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
// Here all your customization on the View
view.setBackgroundColor(.......);
...
return view;
}
}
in android 6.0 you can change the colour of text like below
holder._linear_text_active_release_pass.setBackgroundColor(ContextCompat.getColor(context, R.color.green));
Source: Stackoverflow.com