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
Configure the edittextcreate an adapter for the listview and set the position as a tag for the edittext. Normally, when scrolling the item position will change. So, you have to get the edittext tag and set it into the edittext id. from that you can avoid the change of the item position.
Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With