How to set the text at center in ListView android application?
Solution 1:
you are using built-in layout which is
android.R.layout.simple_list_item_1
this layout cannot be changed. However you can provide your own row layout.
your row should look like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_width="fill_parent"
android:textSize="20sp"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:gravity="center" />
remember that the id should be only android:id="@android:id/text1"
and now you can give this to your ArrayAdapter's constructor:
ArrayAdapter<String>(this,R.layout.my_custom_layout,conversionsadd);
Solution 2:
I didnot try, but I think, overriding getView in adapter method, and do the following:
String[] conversionsadd = getResources().getStringArray(R.array.conversions);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,conversionsadd)
{
public View getView(AdapterView adapterView, View convertView, int position, long id)
{
View view=super.getView(adapterView, convertView, position, id);
TextView txt=view.findViewById(android.R.id.textView1);
txt.setGravity(Gravity.CENTER);
return view;
}
});
getListView().setTextFilterEnabled(true);
getListView().setBackgroundResource(R.drawable.listback2);
Solution 3:
you should give the code of the getView
function of ArrayAdapter
or XML of R.layout.simple_list_item_1
, which is normally where the style of the listview item is set