Reputation: 4559
Actually i want to change the text color of the listview that i had created for my app..actually when i change the bacground color of listview to white the text present in the listview is not appear as text color is also white..so is there any way to change the text color of the listview to black so that it is visible in the listView..i am sending my code u please check it..
XML code:
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_height="match_parent" android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:orientation="vertical">
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_width="match_parent">
<TextView android:text="All Contacts"
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#808080"
android:textColor="#000000">
</TextView>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_width="match_parent">
<ListView android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Java code:
private ListView lv1;
private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople","Android","iPhone"};
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.contact_activity);
lv1=(ListView)findViewById(R.id.ListView01);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
lv1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
String selecteditem = lv_arr[position];
Intent myIntent = new Intent(view.getContext(), ContactInfo.class);
myIntent.putExtra("item", selecteditem);
startActivity(myIntent);
}
});
}
Upvotes: 0
Views: 17429
Reputation: 128428
For changing the color/height/width or any look and appearance of listview item, you have to define your custom listview with custom adapter.
First, define listitem row file for your listview, in which take a Textview with the color whatever you want and then pass this row file to your custom adapter.
Here is my example: tinyurl.com/buq5wdx , go through this example and make customization as per your requirement.
I am totally agree with Pragna's answer, but if you still want to have listview with something like 2 textviews with 1 imageview, 1 imageview and 1 textview or any control, you have to customize the listview by defining Custom Adapter.
Upvotes: 3
Reputation: 3677
listplaceholder.xml having the list view and the textview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:dividerHeight="0.0px"
android:background="@android:color/white" />
<TextView
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading"
android:focusable="false"
android:clickable="true"
android:background="@android:color/white" />
</LinearLayout>
and the list_item.xml containing the textview is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">
<TextView android:id="@+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="2dp"
android:textSize="20dp"
android:textColor="@android:color/black"
android:background="@android:color/white" />
<TextView android:id="@+id/item_subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="12dp"
android:textColor="@color/grey"
android:background="@android:color/white" />
</LinearLayout>
in your xml containing the Textview put
android:textColor="@android:color/black"
android:background="@android:color/white"
Hope this will work
or else you can create a custom arrayadapter and override the getView method in it. You can then change the background color, font color as well as the fontFace of the text from here.
Typeface localTypeface1 = Typeface.createFromAsset(getAssets(), "fonts/arial.ttf");
TextView tt = (TextView) v.findViewById(R.id.item_title);
tt.setText(results.get(position).get("title"));
tt.setBackgroundColor(color);
tt.setTextColor(color);
tt.setTypeface(localTypeface1);
Upvotes: 2
Reputation: 10726
Duplicate of : put color in Listview dymically
You can not use android.R.layout.simple_list_item_1 if you want to do some customization.
You have to create a new layout, e.g. my_list_row.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="wrap_content"
android:textColor="yourcolor" />
and set in your Activity :
lv1.setAdapter(new ArrayAdapter<String>(this,R.layout.my_list_item , lv_arr));
Upvotes: 0
Reputation: 2250
Check these for more info:
http://www.anddev.org/view-layout-resource-problems-f27/changing-listview-text-color-t14527.html
Changing text color of list view in android
Change Text Color in ListView
Make a layout for your List items and bind that to a ListAdapter. See this site for an example of a custom listview: http://p-xr.com/android-tutorial-how-to-parseread-xml-data-into-android-listview/. ( There is an Eclipse project to the bottom of the post )
Upvotes: 0
Reputation: 1
The solution for this is customise the list-view with using Base Adapter or Array adapter.
lv1.setAdapter(Adapter Class Object).
In Adapter class contains getView() method. This method will construct the view for list-view lists. So add the inflated xml layout to that getView() as required.
Upvotes: 0