0

i have a customized listview and i cannot figure out how to change listitems text color,style,size , etc i have tried editing in hte xml files but it's not helping. i am new to android, any help will be appreciated..

**layout**
********
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:background="@drawable/background"
android:layout_height="wrap_content"
>
<EditText 
 android:id="@+id/inputSearch"
 android:layout_height="wrap_content"
 android:layout_width="fill_parent"
 android:hint="Search"
 android:drawableRight="@drawable/search"
 android:inputType="textVisiblePassword"
 android:cacheColorHint="#00000000"
 android:background="#D8D8D8"
>
</EditText>
<ListView
    android:id="@+id/ListView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:dividerHeight="1dp"
    android:divider="#2D9E00"
    android:listSelector="@drawable/list_selector"
    android:cacheColorHint="#00000000"
    android:fastScrollEnabled="true">

  </ListView>

</LinearLayout>



**list_selector.xml**
*******************

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
 android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@drawable/gradient_bg" />

<item android:state_pressed="true"
    android:drawable="@drawable/gradient_bg_hover" />

<item android:state_selected="true"
 android:state_pressed="false"
    android:drawable="@drawable/gradient_bg_hover" />
 </selector>


**gradient_bg.xml
*********************
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
 <gradient
  android:startColor="#2D9E00"
  android:centerColor="#2D9E00"
  android:endColor="#2D9E00"
  android:angle="270" />

</shape>

**gradient_bg_hover.xml**
**************************
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
  android:startColor="#2D9E00"
  android:centerColor="#2D9E00"
  android:endColor="#2D9E00"
  android:angle="270" />

</shape>

my java code

public class MainActivity extends Activity {

private ListView list1;
private String array[] ;
EditText inputSearch; 
ArrayAdapter<String> adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    inputSearch = (EditText) findViewById(R.id.inputSearch); 
    list1 = (ListView) findViewById(R.id.ListView01);
    array = getResources().getStringArray(R.array.myArray);
    adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,        getResources().getStringArray(R.array.myArray));
    list1.setAdapter(adapter);
    inputSearch.addTextChangedListener(new TextWatcher() {
Anonymous
  • 1,389
  • 3
  • 11
  • 16

1 Answers1

0

You are using android.R.layout.simple_list_item_1 as the layout of the item in the ListView. If you want to customize the layout of the items, follow these steps:

Step 1) Create a custom XML layout (simple_list_item_custom.xml) as follows:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@android:id/text1" 
    style="?android:attr/listItemFirstLineStyle" 
    android:paddingTop="2dip"  
    android:paddingBottom="3dip"
    android:textColor="#FF0000"/>

In this above layout, add XML attributes to change the font size, color etc. (Just like I added android:textColor="#FF0000" to make the color red.

Step 2) Init the adapter with your custom layout:

adapter =new ArrayAdapter<String>(this, R.layout.simple_list_item_custom, getResources().getStringArray(R.array.myArray));

Everything else remains the same..

Amulya Khare
  • 7,718
  • 2
  • 23
  • 38