How to set text in the center of a ListView android application?

I implemented the application in ListView in my application, I used the getListView () method. I wrote the code as follows:

String[] conversionsadd = getResources().getStringArray(R.array.conversions); setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,conversionsadd)); getListView().setTextFilterEnabled(true); getListView().setBackgroundResource(R.drawable.listback2); 

when I write the above code using the onCreate method, I can show a list with some texts. from the above code, I would like to display the text in the center in the list, and also display the text in color.

+6
android text listview center get
Jan 19 2018-12-12T00:
source share
3 answers

you use the built-in layout which

 android.R.layout.simple_list_item_1 

This layout cannot be changed. However, you can provide your own line layout.

your line 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 identifier should only be android:id="@android:id/text1"

and now you can pass this to the ArrayAdapter constructor:

 ArrayAdapter<String>(this,R.layout.my_custom_layout,conversionsadd); 
+24
Jan 19 2018-12-12T00:
source share

I have not tried, but I think, overriding the getView method in the adapter and doing 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); 
+4
Jan 19 2018-12-12T00:
source share

you should specify the getView code of the ArrayAdapter or XML R.layout.simple_list_item_1 , which usually has a list item style style.

+1
Jan 19 2018-12-12T00:
source share



All Articles