I am trying to change the alignment of the text in ListView programmatically instead of changing the xml file.
Here is my row_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textViewRowList"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_width="fill_parent"
android:textSize="20sp"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:gravity="left"
android:textColor="#330033"
android:textStyle="bold"/>
And activity_main.xml has following ListView defined there
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="0dp"
tools:listitem="@android:layout/simple_list_item_1"
android:background= "#FF6600">
</ListView>
So in my mainactivity.java I am trying to change the alignment of the text in listview at runtime when user clicks a button on my activity
private ListView lv;
private TextView tvRowListView;
lv.setAdapter(new ArrayAdapter<String>(this,R.layout.row_listview, sampleStringArray));
tvRowListView = (TextView) findViewById(R.id.textViewRowList);
tvRowListView.setGravity(Gravity.CENTER);
The last line above is however throwing nullpointerexception. I am not sure why. Can someone throw some ideas/solutions here.
your textViewRowList exists for every item in that list, if you want to change it’s properties you will need to do it in the
AdapteringetViewwhich means you will need to make a custom adapter